Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.

Similar presentations


Presentation on theme: "CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3."— Presentation transcript:

1 CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

2 CS115 FALL 2006-2007 Senem KUMOVA-METİN2 Variables and constant  Variables and constant are the objects that program manipulates…  In C, all variables must be declared, i.e., their data type, before they can be used int main(void) { int a,b,c ; /* declaration*/ float x= 3.14; /* declaration with initialization*/ …}

3 CS115 FALL 2006-2007 Senem KUMOVA-METİN3 Declarations and Expressions Declaration: tell the compiler to set aside an appropriate amount of space in memory to hold values associated with variable int x=1;   float y=1;   Expression: meaningful combinations of constants, variables, operators and function calls a+b sqrt(3.444), i=7 1 1.0

4 CS115 FALL 2006-2007 Senem KUMOVA-METİN4 Assignment SOME ASSIGNMENT EXAMPLES :  i=7;  x=x+1; /* x is assigned the old value of x plus 1 */  y+=4;  x +2 =0; /* WRONG */  0=1;/* WRONG */

5 CS115 FALL 2006-2007 Senem KUMOVA-METİN5 FUNDAMENTAL DATA TYPES 1 character char signed char unsigned char 2 integer shortintlong unsigned short unsigned long 3 float doublelong double

6 CS115 FALL 2006-2007 Senem KUMOVA-METİN6 FUNDAMENTAL DATA TYPES 1 character char signed char unsigned char 2 integer shortintlong unsigned short unsigned long 3 float doublelong double

7 CS115 FALL 2006-2007 Senem KUMOVA-METİN7 1. Characters and the char Data Type ( 1/4)  “char” is a 1-byte (1byte= 8 bits)  2 8= 256 distinct values Mostly used for representing characters Can also be used to represent also integers !!  signed char : have values between -128 and 127 ( in a two's complement machine)  unsigned char : have values between 0 and 255  Printable characters are always positive !!

8 CS115 FALL 2006-2007 Senem KUMOVA-METİN8 Some char constant and their integer values (ASCII Table in appendix D ) Character constant ‘a’ ‘b’ ‘c’ … ‘z’ Corresponding value 97 98 99 … 112 Character constant ‘A’ ‘B’ ‘C’ … ‘Z’ Corresponding value 65 66 67 … 90 Character constant ‘0’ ‘1’ ‘2’ … ‘9’ Corresponding value 48 49 50 … 57 Character constant ‘&’ ‘*’ ’+’ Corresponding value 38 42 43 1. Characters and the char Data Type ( 2/4)

9 CS115 FALL 2006-2007 Senem KUMOVA-METİN9 1. Characters and the char Data Type (3/4)

10 CS115 FALL 2006-2007 Senem KUMOVA-METİN10 EXAMPLE: char c = 'a'; /* ASCII code for 'a' is (01100001) 2 = (97) 10 */ printf("%c", c); /* a is printed */ printf("%d", c); /* 97 is printed */ printf("%c%c%c",c,c+1,c+2); /* abc is printed */  HOW CAN WE CONVERT a CHAR FROM LOWER CASE TO UPPER CASE??? 1. Characters and the char Data Type (4/4)

11 CS115 FALL 2006-2007 Senem KUMOVA-METİN11 FUNDAMENTAL DATA TYPES 1 character char signed char unsigned char 2 integer shortintlong unsigned short unsigned long 3 float doublelong double

12 CS115 FALL 2006-2007 Senem KUMOVA-METİN12  Represents integer values  Typically an int is stored in 2 bytes (16 bits) or in 4 bytes (32 bits)  Use sizeof() function 2. The Data Type int (1/4) #include main() {int x=350000; printf("x= %d\n”, x); printf("sizeof(x)= %d\n", sizeof(x));}

13 CS115 FALL 2006-2007 Senem KUMOVA-METİN13 EXAMPLE : IF a computer has 4 bytes for int THEN There are 2 32 distinct states for integers Half of the states can be used for positive numbers, the other half for negative numbers -2 31,-2 31 +1, …, -2, -1, 0, 1, 2, …, 2 31 -1 In this machine integers can vary between -2(~-2 31 )billion to +2 (~2 31 -1) billion IF an integer is assigned a value greater than 2 billions or less than -2 billions an integer overflow occurs.. 2. The Data Type int (2/4)

14 CS115 FALL 2006-2007 Senem KUMOVA-METİN14 2. The Data Type int (3/4)  long  4 bytes (2 32 distinct values)  short  2 bytes (2 16 distinct values)  int  On machines with 4byte words int has the same size with long On machines with 2byte words int has the same size with short  unsigned  has no sign (always positive and has the same size with int)

15 CS115 FALL 2006-2007 Senem KUMOVA-METİN15  Suffixes are used to specify type of integer  EXAMPLE long int x = 37L; unsigned long int y= 37UL; 2. The Data Type int (4/4)

16 CS115 FALL 2006-2007 Senem KUMOVA-METİN16 FUNDAMENTAL DATA TYPES 1 character char signed char unsigned char 2 integer shortintlong unsigned short unsigned long 3 float doublelong double

17 CS115 FALL 2006-2007 Senem KUMOVA-METİN17 3. The floating data types (1/4)  3 floating types : float (mostly 4 bytes) double (mostly 8 bytes) long double  Some examples -3.14159f = -314.159e-2F /* float */ 0e0 /* floating point zero 0.0 of type double */ 1. /* double 1.0 */ 0.1908 /* double 0.1908 */ SuffixTypeExample f or Ffloat3.7F l or L long double3.7L

18 CS115 FALL 2006-2007 Senem KUMOVA-METİN18 222.3333 e-10 = 222.3333 * 10 -10 Floating point constant parts for 222.3333e-10 IntegerFractionExponent 2223333e-10 3. The floating data types (2/4)

19 CS115 FALL 2006-2007 Senem KUMOVA-METİN19  Precision : Number of significant decimal places a floating value carries  Range : Limits of the largest & smallest positive floating values that can be represented by a variable of that type ------------------------------------------------------------ EXAMPLE : IF precision is 6 and range is 10 -38 to 10 +38 for type X THEN X can be 0.d 1 d 2 d 3 d 4 d 5 d 6 * 10 n and -38 < = n <=38 3. The floating data types (3/4)

20 CS115 FALL 2006-2007 Senem KUMOVA-METİN20  For double floating numbers : precision = 6, range = 10 -38 to 10 +38 0.d 1 d 2 d 3 d 4 d 5 d 6 * 10 n and -38 < = n <=38  For long floating numbers : precision = 15, range = 10 -308 to 10 +308 0.d 1 d 2 d 3 ….d 15 * 10 n and -308 < = n <=308 3. The floating data types (4/4)

21 CS115 FALL 2006-2007 Senem KUMOVA-METİN21 The use of typedef  Programmer can explicitly associate a type with an identifier #include main() { typedef char uppercase;/* associate type char with identifier “uppercase” */ uppercase u='A'; /* declare character “u” */ typedef int myint;/* associate type int with identifier “myint” */ myint x =90; /* declare integer “myint” */ printf("%c %d \n", u, x);}

22 CS115 FALL 2006-2007 Senem KUMOVA-METİN22 The sizeof operator  sizeof(object)  Returns an integer that represent the number of bytes needed to store an object in memory  EXAMPLE: int x=4; printf(“%d”, sizeof(x)); printf(“%d”, sizeof(x+3));  Check what sizeof() will return for int, double, char, unsigned … etc. ????

23 CS115 FALL 2006-2007 Senem KUMOVA-METİN23 getchar( ) & putchar( ) (1/2)  Macros defined in “stdio.h” that are used to read characters from keyboard and to print characters on screen #include int main(void) { int c; if((c = getchar())!= EOF) /* scanf(“%c”, &c);*/ { putchar(c); /* printf(“%c”, c);*/ putchar(c); } return 0; /* EOF = end-of-file =-1 */ }

24 CS115 FALL 2006-2007 Senem KUMOVA-METİN24 getchar( ) & putchar( ) (2/2) #include int main(void) { int c; while((c = getchar())!=EOF)/* scanf(“%c”, &c);*/ { putchar(c); /* printf(“%c”, c);*/ putchar(c); } return 0; /* EOF = end-of-file =-1 */ /* EOF  CTRL +Z }

25 CS115 FALL 2006-2007 Senem KUMOVA-METİN25 Mathematical functions, Printing tricks math.h  sqrt(), pow(), exp(),log(), sin() … #include int main(void) { double x =40000.0; printf("x=%12.4e\n", x); /* 4 digits for fraction part, totally 12 characters*/ printf("square root of x=%10.1e", sqrt(x)); /* 1 digits for fraction part, totally 10 characters*/ } OUTPUT : x= 4.0000e+004 square root of x= 2.0e+002

26 CS115 FALL 2006-2007 Senem KUMOVA-METİN26 Arithmetic Conversions : Review any_type op long double -> long double any_type op double -> double  int x; double y;  (x+y)  double any_type op float -> float any_type op unsigned long -> unsigned long …… Check the table (pg.133)

27 CS115 FALL 2006-2007 Senem KUMOVA-METİN27 CASTS : Forces explicit converions # include void main(void) { int x=3, y=10; double divv; divv=10/3; printf("10/3 = %f\n", divv); divv =y/x; printf("y/x = %f\n", divv); divv=(double)10/3; //FORCE TO STORE 10/3 IN DOUBLE printf("(double)10/3 = %f\n", divv);} OUTPUT :10/3 = 3.000000 y/x = 3.000000 (double)10/3 = 3.333333

28 CS115 FALL 2006-2007 Senem KUMOVA-METİN28 Hexadecimal and Octal constants (1/4) hexadecimal digit 123…9101112131415 decimal value 123…9ABCDEF Octal digit 1234567 decimal value 1234567

29 CS115 FALL 2006-2007 Senem KUMOVA-METİN29 EXAMPLE : A0F3C = A * 16 4 + 0 * 16 3 + F * 16 2 + 3 * 16 1 + C * 16 0 = 10 * 16 4 +0 * 16 3 + 15 * 16 2 +3 * 16 1 + 12 * 16 0 = 659260 Hexadecimal and Octal constants (2/4)

30 CS115 FALL 2006-2007 Senem KUMOVA-METİN30  In C source code, positive integer constants prefaced with 0 are integers in octal notation 0x are integers in hexadecimal notation  In printf() function %d --- decimals %o --- octals %x --- hexadecimals Hexadecimal and Octal constants (3/4)

31 CS115 FALL 2006-2007 Senem KUMOVA-METİN31 #include void main(void) { int x=056; int y= 0xA01 printf("%d \n", x); //46 printf("%o \n", x); //56 printf("%x \n\n", x); //2e printf("%d \n", y);//2561 printf("%o \n", y);//5001 printf("%x \n", y);//a01 } Hexadecimal and Octal constants (4/4)

32 CS115 FALL 2006-2007 Senem KUMOVA-METİN32 Exercise Write a program that prints the values of each digit in a 3-digit integer number. Sample output: input a positive integer number: 376 first digit of the number is: 3 second digit of the number is: 7 third digit of the number is: 6


Download ppt "CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3."

Similar presentations


Ads by Google