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 Examples

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 (variable_Identifier); sizeof (Data_Taype); Example: int x; printf("size of x = %d", sizeof x);//4 printf("size of long long = %d", sizeof(long long));//8 printf("size of x = %d", sizeof (x));//4

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= 50e6; /*cast from double to char */ int i= 98.01;/*cast from double to int */ float f = 65.6; int i = f; //f = (int)f; char c = i; // c = (char)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 */ int k, i = 7; float f = 10.14; char c = 'B'; k = (i + f) % 3; // error k = (int)(i + f) % 3;

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 int i; short s; float f; double d; s = 'A'; // s = 65 i = 'B'; // i = 66 f = 4566; // f = d = 5666; // d =

9 Casting effects (cont’d)
Casting from large types to small types Data loss is possible Depends on the values float double short f = 65536; d = 65536; s = 720; // // 720 char short c = (char) 65536; // c = 0 // s = 0 s = (short) 65536; int i = int i = 1.22; 1e23; // i = 1 // i = ???

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;
const p = 3; // const int p = 3; const p; p = 3.14; // compile error const p = 3.14; // p = 3 because default is int const float p = 3.14;

13 Constant const int STUDENTS = 38; const long int MAX_GRADE =20; int i; i = MAX_GRADE; STUDENTS = 39;//ERROR

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 #define STUDENTS 38 int main(void){ int i; i = STUDENTS STUDENTS = 90; //ERROR! What compiler sees: 38 = 90

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

16 #include <stdio.h> // (preprocessor )
#define PI // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("Enter Radius :\n"); scanf("%f", &float ); // calculating area of circle Area = PI * Radius * Radius; printf(“Area = %f", Area ); system("Pause"); return 0; }

17 Examples Precedence Rules

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

19 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 19

20 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 _ 20

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

22 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 _ 22

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

24 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++; _ 24

25 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 _ 25

26 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. _ 26


Download ppt "Lecture 8."

Similar presentations


Ads by Google