Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8.

Similar presentations


Presentation on theme: "Lecture 8."— Presentation transcript:

1 Lecture 8

2 Outline Sizeof Type casting Constants define

3 sizeof The sizeof keyword returns the number of bytes of the given expression or type returns an unsigned integer result sizeof variable_Identifier; sizeof (Data_Type); sizeof (variable_Identifier); Example:

4 Type Casting What is the casting?
When the type of variable and value are not the same Example: Assigning double value to integer variable

5 Implicit casting Implicit We don’t say it But we do it
carried out by compiler automatically char f2= ; /*cast from double to char */ int i= 98.01; /*cast from double to int */ float f = 65.6; int i = f; char c = i;

6 Explicit casting Explicit And we do it
We say it And we do it carried out by programmer using casting int i=(int)98.1;/*cast from double to int */ char c = (char) 90;/* cast from int to char */

7 Type Casting char c = 'A'; short s = 1; int i; float f; double d;
i = (c s); (int) (char) (short) (short) (int) d = ( c / i) ( f * d) ( f i ); (char) (int) (float) (double) (float) (int) int double float double

8 Casting effects Casting from small types to large types
There is not any problem No loss of data

9 Casting effects (cont’d)
Casting from large types to small types Data loss is possible Depends on the values

10 Casting effects (cont’d)
Casting to Boolean If value is zero  false If values is not zero  true bool bool b2 = 'a', b3 = -9, b5 = b4 = 4.5; //true //false 0, b6 = false; b7 = '\0';

11 Constant Variables Constants
Do not want to change the value Example: pi = 3.14 We can only initialize a constant variable We MUST initialize the constant variables (why?!)

12 Constant const [Data_Type] Identifier = constant_value;

13 Constant

14 Definitions Another tool to define constants
Definition is not variable We define definition, don’t declare them Pre-processor replaces them by their values before compiling

15 Constant #define Identifier constant_value #define PI 3.14 #define ERROR "Disk error " #define ONE 1 #define TWO ONE + ONE

16 Example

17 Example

18 Example

19 Example

20 Examples Precedence Rules

21 int num = 0, i = 0; for (i = 0; i < 5; i++) { printf("%d", ++num);
} printf("%d", num++); 12345 01234 _ 21

22 b=(a++>0) ? printf("%d\n",a):printf("%d\n",a);
int b,a = 0; b=(a++>0) ? printf("%d\n",a):printf("%d\n",a); b = (a>0)?printf("%d\n",++a):printf("%d\n",a); printf("%d\n" , a) ; b=++a+(a>0 )?printf("%d\n",a++):printf("%d\n",a); printf("%d\n" ,a) ; 1 _ 1 2 22

23 int a = 0; int b = 5; b = ( a++ > 0 ) ? ++a : ++b;
printf("%d %d\n" , a, b); int a = 5; int b = 13; a = a * b % b / a; b = (++a > 0 ) ? a : b++ ; 1 6 1 1 _ 23

24 int j,k,t=1,i=2; for(k=0;k<5;k++){ j = i++ + t; printf("%d",j); }
34567 _ 24

25 int a = 40, b = 30, c; c = !a <= !(c = b); printf("%d", c);
int i =1; int j = 3*(4+i++); printf("%d %d\n", i,j); 1 2 15 _ 25

26 int n = 5;int i = 0; while(i++ <= n) printf("%d " , i);
1 2 3 _ 1 2 3 26

27 printf("%d %d %d %d", i, j, k, m); printf("%d",1 + 5&4 == 3);
int i=-3, j=2, m, k=0; m=++i && ++j || ++k; printf("%d %d %d %d", i, j, k, m); printf("%d",1 + 5&4 == 3); m=++i && ++j && k++; _ 27

28 printf("%d %d %d", i, ++i, i++); int i = 0, j = 0; j = i++ + ++i;
printf("%d %d",i,j); 3 3 1 2 2 _ 28

29 int i=1; printf("%d", i++ + ++i); int i=3; printf("%d",++i + ++i);
printf("%d\n", i); 4 10 1 C has the concept of undefined behavior, i.e. some language constructs are syntactically valid but you can't predict the behavior when the code is run. _ 29


Download ppt "Lecture 8."

Similar presentations


Ads by Google