Summary
Primitive types in C
kind | type | size(bytes) | size(bits) | value range |
---|---|---|---|---|
byte | char | 1 | 8 | |
unsigned char | 1 | 8 | ||
integer | short int | 2 | 16 | |
unsigned short int | 2 | 16 | ||
int | 4 | 32 | ||
unsigned int | 4 | 32 | ||
long int | 8 | 64 | ||
unsigned long int | 8 | 64 | ||
floating-point | float | 4 | 32 | |
double | 8 | 64 | ||
long double | 16 | 128 |
may change depending on architecture, x86_64 vs 32-bit
Concept
Units
- byte = 8 bits
- nibble = 4 bits
- word = n bytes
Capacity
bits can represent up to values- to represent
values, bits are required
0 is counted as a value
Application
Sizes of different types
c
#include <stdio.h>
int main(void) {
printf("Size of 'char' (in bytes): %d\n", sizeof(char)); // 1
printf("Size of 'short int' (in bytes): %d\n", sizeof(short int)); // 2
printf("Size of 'int' (in bytes): %d\n", sizeof(int)); // 4
printf("Size of 'long int' (in bytes): %d\n", sizeof(long int)); // 8
printf("Size of 'float' (in bytes): %d\n", sizeof(float)); // 4
printf("Size of 'double' (in bytes): %d\n", sizeof(double)); // 8
printf("Size of 'long double' (in bytes): %d\n", sizeof(long double)); // 16
return 0;
}
when run on a 64-bit Arch Linux
Interpretation of different types
c
int num = 65;
printf("num (in %%d) = %d\n", num); // prints 70
printf("num (in %%c) = %c\n", num); // prints A
char ch = 'F';
printf("ch (in %%c) = %c\n", ch); // prints F
printf("ch (in %%d) = %d\n", ch); // prints 70
char
decodes/encodes the value using the ASCII convention