CS156: Intro to C, Part I

Spring 2018

Sizeof

See this page as a slide show

CS156 Sizeof

sizeof

sizeof() is a compile-time function that return the size, in bytes, of a variable or a type.

char c;
int i;
float f;
double d;
printf("%zu\n", sizeof(c));
printf("%zu\n", sizeof(i));
printf("%zu\n", sizeof(f));
printf("%zu\n", sizeof(d));
printf("%zu\n", sizeof(double));
1
4
4
8
8

The printf format for sizeof is %zu.

sizeof an array

int a[13];
printf("%zu\n", sizeof(a));
52
int a[13];
printf("%zu\n", sizeof(a)/sizeof(a[0]));
13

Array Function Argument

void bar(int b[]) {
    printf("b: %zu\n", sizeof(b));
}

int main() {
    int a[1000];
    bar(a);
    printf("a: %zu\n", sizeof(a));
}
c.c: In function 'bar':
c.c:2: warning: 'sizeof' on array function parameter 'b' will return size of 'int *'
c.c:1: note: declared here
b: 8
a: 4000

Array Function Argument

Array Function Argument

void bar(int *b) {
    printf("b: %zu\n", sizeof(b));
}

int main() {
    int a[1000];
    bar(a);
    printf("a: %zu\n", sizeof(a));
}
b: 8
a: 4000

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-03-08T14:01

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building