C
Beginner
1 min read
Fundamental Data Types and sizeof
Example
#include <stdio.h>
#include <stdint.h> /* fixed-width types */
int main(void)
{
/* Basic types */
char c = 'A';
short s = 32000;
int i = -42;
long l = 1000000L;
float f = 3.14f;
double d = 2.718281828;
/* Fixed-width types from <stdint.h> */
uint8_t byte = 255;
int32_t word = -100000;
uint64_t big = 18446744073709551615ULL;
/* sizeof returns the size in bytes */
printf("char: %zu bytes\n", sizeof(char));
printf("short: %zu bytes\n", sizeof(short));
printf("int: %zu bytes\n", sizeof(int));
printf("long: %zu bytes\n", sizeof(long));
printf("float: %zu bytes\n", sizeof(float));
printf("double: %zu bytes\n", sizeof(double));
printf("int32_t: %zu bytes\n", sizeof(int32_t));
printf("c=%c s=%d i=%d l=%ld\n", c, s, i, l);
printf("f=%.2f d=%.9f\n", f, d);
printf("byte=%u word=%d\n", byte, word);
return 0;
}