1 cse1301 computer programming lecture 6 c primitives 3
Post on 19-Dec-2015
215 views
TRANSCRIPT
![Page 1: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/1.jpg)
1
CSE1301Computer Programming
Lecture 6C Primitives 3
![Page 2: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/2.jpg)
2
Topics
• Type
• Variables
• Keywords and Identifiers
• Assignments
• Constants
![Page 3: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/3.jpg)
3
Type
• The kind of a value is its “type”
• Not all values are of the same kind– For example: 7 + “cat” makes no sense
ValueVariable
![Page 4: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/4.jpg)
4
Type
• Built-in types: char, int, float• Type modifiers: long, short, const• User-defined types (arrays and records)
• What about “strings”?– Strings are arrays of char (discussed later)
![Page 5: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/5.jpg)
5
Character Representation
• Characters are stored as a small integer• Each character has a unique integer
equivalent specified by its position in the ASCII table (pronounced “as-key”)– American Standard Code for Information
Interchange– see Appendix E of King
![Page 6: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/6.jpg)
6
![Page 7: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/7.jpg)
7
Character Representation
• The ASCII values range from 0 to 127– value 0: special character ’\0’
(a.k.a. NUL character)– value 127: special character <DEL>– other special characters: ’\n’ ’\t’ ’\’’ ’\\’ etc.
– various “extended” sets from 128 to 255
![Page 8: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/8.jpg)
8
Variable• is a logical name for a container
– (an actual piece of computer memory for values)
• has a type associated with it– tells the computer how to interpret the bits
• must be declared before use:
int i; float result;
int i=0; char initial=’K’;
![Page 9: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/9.jpg)
9
Variable Declaration: Examples
int myID;
myID
![Page 10: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/10.jpg)
10
Variable Declaration: Examples
int myID;
char myInitial = ’J’;
Single “forward quotes” or apostrophe (’) rather than
“back quotes” (‘)
![Page 11: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/11.jpg)
11
Variable Declaration: Examples
int myID;
char myInitial = ’J’; 01001010myInitial
![Page 12: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/12.jpg)
12
Variable Declaration: Examples
int myID;
char myInitial = ’J’;
char myInitial = 74 ;
01001010myInitial
![Page 13: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/13.jpg)
13
Variable Declaration: Examples
short int myHeight = 152; /* cm */
int mySalary = 1000000000;
long int mySalary = 1000000000;
float commission = 0.05;
double chanceOfADate = 3e-500;
![Page 14: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/14.jpg)
14
short int myHeight = 152; /* cm */
int mySalary = 1000000000;
long int mySalary = 1000000000;
float commission = 0.05;
double chance_of_a_date = 3e-500;
Variable Declaration: Examples
“Keywords”
![Page 15: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/15.jpg)
15
Keyword
• has a special meaning in C
• is “case-sensitive”
• cannot be used as variable names
• Examples:int, char, long, main, float, double, const, while, for, if, else, return, break, case, switch, default, typedef, struct, etc. (see D&D 2/e Appendix A or King page 24)
![Page 16: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/16.jpg)
16
Variable Declaration: Examples
short int myHeight = 152; /* cm */
int mySalary = 1000000000;
long int mySalary = 1000000000;
float commission = 0.05;
double chanceOfADate = 3e-500;
“Identifiers”
![Page 17: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/17.jpg)
17
Identifier
• is a series of characters consisting of letters, digits and underscores ( _)
• cannot begin with a digit
• must not be a keyword
• is “case-sensitive”
• Examples:sUmoFA, x1, y2, _my_ID_, Main (careful!)
![Page 18: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/18.jpg)
18
Assignment
• Puts a specified value into a specified variable
• Assignment operator: =
<variable name> = <expression> ;
not to be confused with ==
![Page 19: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/19.jpg)
19
Assignment: Examplesfloat x = 2.5 ;
char ch ;
int number ;
ch = ’\n’ ;
number = 4 + 5 ;
/* current value of number is 9. */
number = number * 2;
/* current value of number is now 18. */
![Page 20: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/20.jpg)
20
Assignment
• Value must have a type assignable to the variable
• Value may be automatically converted to fit the new container
• Example:– various.c
![Page 21: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/21.jpg)
21
#include <stdio.h>
/* Do various assignment statements */
int main(){ int integer;
char character;
float floatingPoint;
integer = 33;
character = 33;
floatingPoint = 33;
integer = 'A';
character = 'A';
floatingPoint = 'A';
integer = 33.33;
character = 33.33;
floatingPoint = 33.33;
integer = floatingPoint;
floatingPoint = integer;
return 0;
}
various.c
![Page 22: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/22.jpg)
22
Constants
• don’t vary
• may not be assigned to
• must be initialized
const float Pi = 3.14159;
const int classSize = 100;
![Page 23: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/23.jpg)
23
Constants: Examples
const int myID = 192;
myID = 666; /* Error! */
const int passMark = 80;
const char pAsSgRaDe = ’P’;
const float pi = 3.1415926;
const double golden_ratio = 1.61803398874989;
![Page 24: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/24.jpg)
24
Converts an angle from degrees to radians
output “Enter angle in degrees”
input angleInDegrees
angleInRadians = / 180 * angleInDegrees
output angleInRadians
#include <stdio.h>
/* Converts an angle in degrees to radians. */
const float PI = 3.1415926;
int main(){ float angleInDegs; float angleInRads;
printf("Enter angle in degrees:"); scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads); return 0;}
Example: Constants
![Page 25: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/25.jpg)
25
Example: Constants
“Global” constant variable
“Local” variables
more on this later...
#include <stdio.h>
/* Converts an angle in degrees to radians. */
const float PI = 3.1415926;
int main(){ float angleInDegs; float angleInRads;
printf("Enter angle in degrees: "); scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads); return 0;}
![Page 26: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/26.jpg)
26
Topics
TypeVariablesKeword and IdentifiersAssignmentsConstant Variables
![Page 27: 1 CSE1301 Computer Programming Lecture 6 C Primitives 3](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3e5503460f94a1755a/html5/thumbnails/27.jpg)
27
Reading
• King– Chapter 2, 2.4 – 2.8– Chapter 4, 4.2 – 4.5– Chpater 7, 7.1 – 7.3
• D&D: – Chapter 9, 9.1 – 9.5, 9.11
• Kernighan & Ritchie– 2.4 – 2.8, 2.10, 2.12