Sizeof
struct {
    int i;
    char c1;
    char c2;
} s1;
struct {
    char c1;
    int i;
    char c2;
} s2;
struct {
    char c1;
    char c2;
    char c3;
} s3;
struct {
    double d;
    char c1;
    char c2;
} s4;
int main()
{
char* cp = "abcdef";
printf("%d\n", sizeof(cp));             //pointer to char -> 4
 
char ca[] = "abcdef";
printf("%d\n", sizeof(ca));             //abcdef + '\0" -> 7
 
char ca2[100] = "abcdef" ;
printf("%d\n", sizeof(ca2));           //100
 
int ia[100];
printf("%d\n", sizeof(ia));              //100 * 4 -> 400
 
char* cp2 = (char*) sizeof(100);        
printf("%d\n", sizeof(cp2));              //4      
printf("%d\n", sizeof(s1));             //8 
printf("%d\n", sizeof(s2));             //12       
printf("%d\n", sizeof(s3));             //3         
printf("%d\n", sizeof(s4));             //12    
return 0;
}
char f() {
return 'c';
}
int main()
{
int a = 0;
printf("%d\n", sizeof(a++) );         // sizeof integer -> 4
printf("%d\n", a);                            // 0    (instruction in sizeof won't be executed)
printf("%d\n", sizeof(f()) );            // 1    (sizeof(function()) returns the size of the return type of the function)
printf("%d\n", sizeof(f) );               // 1    (same)
}

sizeof vs strlen

#include <stdio.h>
#include <string.h>
 
int main()
{
    char *m = "abcde";
    printf("%d\n", strlen(m));
 
    char r[] = "abcde";
    printf("%d\n", sizeof(r));
}

Output:
5
6

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License