LANGUAGE » C

Data types

Types

Check a full list of format specifiers here.

TypeSize (bytes)Format specifier
char signed char unsigned char1%c
short short int2%hd %hi
int2, 4%i %d
unsigned int2, 4%u
long long int4, 8%ld %li
long long long long int8+%lld %lli
unsigned long unsigned long int4+%lu
unsigned long long unsigned long long int4+%llu
float4%f
double8%lf
long double10+, 12, 16%Lf

Force type sign:

c
signed TYPE
unsigned TYPE

Constant or static variable:

c
const TYPE
static TYPE

Examples

Creating numbers.

c
float exponential_notation = 1e3;  // same as 1 * 10^3 = 1000
const float PI = 3.14159;
int octal_num = 021, hexadecimal_num = 0xff;
float avg = (float) 5 / 2;  // Type casting

Character literals use ' while string literals use ".

c
char c = 'c';
char name[] = "Sora";

Get the variable size at runtime with sizeof():

c
printf("Size of PI is %lu bytes.\n", sizeof(PI));

Print ASCII value of a char:

c
char c = 'c';
printf("Character %c has code %d", c, c);
return 0;