Data Types & Format Specifiers in C

#c

An excellent reference for data types, format specifiers, and their options/flags in C.
Note: Memory sizes listed are on a 32-bit system. Use the script at the bottom of this page to find the correct sizes on your system.

Data Type Bytes Range Format Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 1.2^-38 to 3.4^38 %f
double 8 2.3^-308 to 1.7^308 %lf
long double 12 3.4^-4932 to 1.1^4932 %Lf

These format specifiers can also have additional options (also called flags) listed below. After the options you can specify the minimum width of the field. In the Output column an underscore (_) signifies empty space.

Option Description Use Output Application
(none) Right-justified with leading spaces ("%5d", 0) ____0 Integers, Floats, Strings
- Left-justified with trailing spaces ("%-5d", 0) 0____ Integers, Floats, Strings
0 Front-fill a number with zeros ("%05d", 0) 00000 Integers, Floats
+ Print positive numbers and zero with a plus sign ("%+5d", 0) ___+0 Integers, Floats
+ Print positive numbers and zero with a plus sign ("%+5d", 0) ___+0 Integers, Floats
(space) Print a space where plus would go for positive numbers and zero ("% -5d", 0) _0___ Integers, Floats

Use this script to find the range of data types on your system.

The min and max range of a signed type is given by the following equation:
-(2N-1) to 2N-1 - 1

The min and max range of an unsigned type is given by the following equation:
0 to (2N-1) + (2N-1 - 1)

N = sizeof(type) * 8    i.e. total bits used by the type

#include <stdlib.h>
#include <stdio.h>

void printUnsignedRange(size_t bytes)
{
	int bits = 8 * bytes;
	unsigned long long to = ((1LL << (bits - 1)) - 1) + (1LL << (bits - 1));
	printf("Range is from 0 to %llu\n", to);
}

void printSignedRange(size_t bytes)
{
	int bits = 8 * bytes;
	long long from = -(1LL << (bits - 1));
	long long to = (1LL << (bits - 1)) - 1;
	printf("Range is from %lld to %lld\n", from, to);
}

int main()
{
	printf("short int: ");
	printSignedRange(sizeof(short int));

	printf("unsigned short int: ");
	printUnsignedRange(sizeof(unsigned short int));

	printf("unsigned int: ");
	printUnsignedRange(sizeof(unsigned int));

	printf("int: ");
	printSignedRange(sizeof(int));

	printf("long int: ");
	printSignedRange(sizeof(long int));

	printf("unsigned long int: ");
	printUnsignedRange(sizeof(unsigned long int));

	printf("long long int: ");
	printSignedRange(sizeof(long long int));

	printf("unsigned long long int: ");
	printUnsignedRange(sizeof(unsigned long long int));

	printf("signed char: ");
	printSignedRange(sizeof(signed char));

	printf("unsigned char: ");
	printUnsignedRange(sizeof(unsigned char));

	return 0;
}

Additional resources:
GeeksforGeeks - Format specifiers in C
GeeksforGeeks - Data Types in C
ExternCode - Data Types in C Language with example Programs
Codeforwin - How to find range of data types in C programming?
Academia - Secrets of "printf"