ch2-sizeof (Size of data types, strings and arrays)
Chapter_2 Exercise_2-1 intlimits | computed Exercise_2-2 |
sizeof.c download
#include <stdio.h> // for printf(), putchar()
#include <string.h> // for strlen()
int main()
{
printf("sizeof(char) = %lu\n", sizeof(char));
printf("sizeof(unsigned char) = %lu\n", sizeof(unsigned char));
printf("sizeof(short) = %lu\n", sizeof(short));
printf("sizeof(int) = %lu\n", sizeof(int));
printf("sizeof(long) = %lu\n", sizeof(long));
printf("sizeof(long long) = %lu\n", sizeof(long long));
printf("sizeof(float) = %lu\n", sizeof(float));
printf("sizeof(double) = %lu\n", sizeof(double));
printf("sizeof(long double) = %lu\n", sizeof(long double));
putchar('\n');
printf("strlen(\"Hello\") = %lu\n", strlen("Hello")); // string length
printf("sizeof(\"Hello\") = %lu\n", sizeof("Hello")); // count ending '\0'
char hello[10] = "Hello";
printf("strlen(hello) = %lu\n", strlen(hello));
printf("sizeof(hello) = %lu\n", sizeof(hello));
return 0;
}
/*
gcc sizeof.c -o sizeof
./sizeof
sizeof(char) = 1 // 1 byte
sizeof(unsigned char) = 1
sizeof(short) = 2
sizeof(int) = 4
sizeof(long) = 8
sizeof(long long) = 8
sizeof(float) = 4
sizeof(double) = 8
sizeof(long double) = 16
strlen("Hello") = 5
sizeof("Hello") = 6
strlen(hello) = 5
sizeof(hello) = 10
*/
Note: See also C_Strings_Functions on W3Schools, the C_Tutorial, chapter C_Strings.
Chapter_2 Exercise_2-1 intlimits | BACK_TO_TOP | computed Exercise_2-2 |
Comments
Post a Comment