C Data Types
C data types decide how much memory a variable uses and what kind of values it can hold. Choosing the right type makes your program correct and efficient.
Basic C Data Types
- char: one byte, a single character or small integer
- int: a whole number, usually 4 bytes (about -2.1 billion to 2.1 billion)
- float: a decimal number with about 6-7 digits of precision, 4 bytes
- double: a decimal number with about 15-16 digits of precision, 8 bytes
- short, long and long long: smaller or larger integers
- unsigned: an integer that holds only zero and positive values, doubling the positive range
Checking Sizes with sizeof
Sizes can differ between platforms, so the sizeof operator tells you how many bytes a type uses on your machine. Its result has type size_t, printed with %zu.
#include <stdio.h>
int main(void) {
printf("char: %zu\n", sizeof(char));
printf("int: %zu\n", sizeof(int));
printf("long long: %zu\n", sizeof(long long));
printf("float: %zu\n", sizeof(float));
printf("double: %zu\n", sizeof(double));
return 0;
}char: 1 int: 4 long long: 8 float: 4 double: 8
Float vs Double
Use double for most decimal math because it is more precise. Floating-point numbers are stored in binary, so some decimals can only be approximated.
#include <stdio.h>
int main(void) {
float f = 1.0f / 3;
double d = 1.0 / 3;
printf("%.10f\n", f);
printf("%.10f\n", d);
return 0;
}0.3333333433 0.3333333333
Booleans and Fixed-Width Types
C17 gets true and false from <stdbool.h>. For exact sizes, <stdint.h> gives types such as int32_t and uint8_t, which are popular in embedded code.
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
int main(void) {
bool isOpen = true;
uint8_t level = 255;
printf("%d %d\n", isOpen, level);
return 0;
}1 255
Frequently asked questions
What is the size of int in C?
The C standard only guarantees at least 16 bits, but on all modern desktop and mobile systems int is 4 bytes (32 bits). Use sizeof(int) to check.
Should I use float or double in C?
Use double by default because it is more precise and C math functions work with double. Use float only when memory is tight, such as large arrays or graphics.
Does C have a boolean type?
Yes. Include <stdbool.h> to use bool, true and false. In C23 they became built-in keywords.