sizeof():
sizeof() helps to find size of a data type or a variable.
Syntax:
sizeof(variable/value);
Argument to sizeof() can be variable or value.
Example:
int a;
printf(“%d”,sizeof(int)); // o/p : 2 (Turboc3)
printf(“%d”,sizeof(a));// o/p : 2 (Turboc3)
printf(“%d”,sizeof(int)); // o/p : 4 (CodeBlocks)
printf(“%d”,sizeof(a));// o/p : 4 (CodeBlocks)
float x;
printf(“%d”,sizeof(float)); // o/p : 4
printf(“%d”,sizeof(x));// o/p : 4
/* size_1.cpp */ #include<stdio.h> int main() { int a; float b; char c; double d; printf("1. size of int %d\n",sizeof(a)); printf("2. size of int %d\n",sizeof(int)); printf("3. size of float %d\n",sizeof(b)); printf("4. size of float %d\n",sizeof(float)); printf("5. size of double %d\n",sizeof(d)); printf("6. size of double %d\n",sizeof(double)); printf("7. size of char %d\n",sizeof(c)); printf("8. size of char %d\n",sizeof(char)); return(0); }
/* Output */ 1. size of int 4 2. size of int 4 3. size of float 4 4. size of float 4 5. size of double 8 6. size of double 8 7. size of char 1 8. size of char 1
/* size_2.cpp */ #include<stdio.h> int main() { char n1[15]="abc"; char n2[20]="hello"; char n3[]="hello"; printf("1. size = %d\n",sizeof(3)); printf("2. size = %d\n",sizeof(3.5)); printf("3. size = %d\n",sizeof(34.656)); printf("4. size = %d\n",sizeof(34.65e6)); printf("5. size = %d\n",sizeof('A')); printf("6. size = %d\n",sizeof('5')); printf("7. size = %d\n",sizeof("abc")); printf("8. size = %d\n",sizeof("Hello World")); printf("9. size = %d\n",sizeof("abc 12")); printf("10. size = %d\n",sizeof(n1)); printf("11. size = %d\n",sizeof(n2)); printf("12. size = %d\n",sizeof(n3)); printf("13. size = %d\n",sizeof(3.5f)); printf("14. size = %d\n",sizeof(34.65e6f)); return(0); }
/* Output */ 1. size = 4 2. size = 8 3. size = 8 4. size = 8 5. size = 1 6. size = 1 7. size = 4 8. size = 12 9. size = 7 10. size = 15 11. size = 20 12. size = 6 13. size = 4 14. size = 4