data representation


Summary

Primitive types in C

kindtypesize(bytes)size(bits)value range
bytechar18
unsigned char18
integershort int216
unsigned short int216
int432
unsigned int432
long int864
unsigned long int864
floating-pointfloat432 to
double864
long double16128

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