C course menu
C Data Types: int, char, float, double, Sizes, Ranges and sizeof
Learn the C data types: char, short, int, long and long long, signed and unsigned, float and double, _Bool and stdbool.h, fixed-width types from stdint.h, size_t, the sizeof operator, typical sizes and ranges, and integer overflow.
On this page
Every variable in C has a type that decides how many bytes it uses and how those bytes are interpreted. Choosing the right type matters more in C than in most languages, because C does not grow numbers automatically or check for overflow.
The basic types#
The C standard only sets minimum sizes. long is 4 bytes on 64-bit Windows but 8 bytes on 64-bit Linux and macOS, which is why portable code often uses the fixed-width types below.
sizeof#
sizeof tells you how many bytes a type or variable uses. Its result has the 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("float: %zu\n", sizeof(float));
printf("double: %zu\n", sizeof(double));
return 0;
}char: 1
int: 4
float: 4
double: 8sizeof(char) is always 1 by definition. The others are what you will see on almost every modern desktop system.
Characters are small integers#
A char stores a number, its character code (ASCII for basic English letters):
char letter = 'A';
printf("%c %d\n", letter, letter);
printf("%c\n", letter + 2);
printf("%d\n", '7' - '0');A 65
C
7Characters use single quotes ('A'); strings use double quotes ("A"). '7' - '0' converts a digit character to its numeric value, a common trick.
signed and unsigned#
Integer types are signed by default (they can be negative). unsigned types cannot be negative, but can hold larger positive values:
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("int: %d to %d\n", INT_MIN, INT_MAX);
printf("unsigned int: 0 to %u\n", UINT_MAX);
return 0;
}int: -2147483648 to 2147483647
unsigned int: 0 to 4294967295limits.h defines these limits for each type.
Overflow#
Going past the limit does not produce an error:
- For unsigned types, the value wraps around (this is well defined):
unsigned char small = 255;
small = small + 1;
printf("%d\n", small);0- For signed types, overflow is undefined behaviour: the program may produce any result. Choose types large enough for your values, such as
long longfor big totals. See Undefined Behaviour.
Floating point#
double price = 49.99;
float rate = 0.18f; /* the f suffix makes a float literal */
printf("%.2f %f\n", price, rate);
printf("%.17f\n", 0.1 + 0.2);49.99 0.180000
0.30000000000000004Decimals are stored in binary, so some values are approximate, as 0.1 + 0.2 shows. Prefer double over float unless memory is very tight. For money, many programs store whole paise or cents in integers.
Booleans#
C89 had no boolean type; 0 means false and any other value means true. C99 added _Bool, and stdbool.h provides the friendlier names bool, true and false (which are keywords in C23):
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool is_member = true;
int age = 17;
bool can_vote = age >= 18;
printf("%d %d\n", is_member, can_vote);
return 0;
}1 0Fixed-width integers#
stdint.h provides types with exact sizes, important for file formats, networking and embedded hardware registers:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main(void) {
uint8_t sensor = 200;
int64_t big = 9000000000;
printf("%u %" PRId64 "\n", sensor, big);
return 0;
}200 9000000000inttypes.h provides portable printf macros such as PRId64.
Practice#
Practice exercise
A shop sells 1,000,000 items at ₹2,500 each. Calculate the revenue using long long and print it, then show what happens if you store the same product in an int (print INT_MAX to explain).
Show solution
#include <stdio.h>
#include <limits.h>
int main(void) {
long long items = 1000000;
long long price = 2500;
long long revenue = items * price;
printf("Revenue: %lld\n", revenue);
printf("Largest int: %d\n", INT_MAX);
return 0;
}Revenue: 2500000000
Largest int: 21474836472,500,000,000 is larger than INT_MAX, so it does not fit in an int. Computing it with int values would be signed overflow, which is undefined behaviour.
Key takeaway#
Finished this lesson?