Skip to content
Laviyx Learn

Try

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.

Beginner8 min readUpdated
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#

TypeHoldsTypical sizeprintf
charA single character (a small integer)1 byte%c (character) or %d (number)
shortSmall whole numbers2 bytes%hd
intWhole numbers (the default choice)4 bytes%d
longLarger whole numbers4 or 8 bytes%ld
long longVery large whole numbers8 bytes%lld
floatDecimals, about 7 significant digits4 bytes%f
doubleDecimals, about 15 to 16 significant digits (the default for decimals)8 bytes%f (%lf in scanf)

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:

C
#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;
}
Output
char:   1
int:    4
float:  4
double: 8

sizeof(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):

C
char letter = 'A';
printf("%c %d\n", letter, letter);
printf("%c\n", letter + 2);
printf("%d\n", '7' - '0');
Output
A 65
C
7

Characters 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:

C
#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;
}
Output
int:          -2147483648 to 2147483647
unsigned int: 0 to 4294967295

limits.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):
C
unsigned char small = 255;
small = small + 1;
printf("%d\n", small);
Output
0
  • For signed types, overflow is undefined behaviour: the program may produce any result. Choose types large enough for your values, such as long long for big totals. See Undefined Behaviour.

Floating point#

C
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);
Output
49.99 0.180000
0.30000000000000004

Decimals 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):

C
#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;
}
Output
1 0

Fixed-width integers#

stdint.h provides types with exact sizes, important for file formats, networking and embedded hardware registers:

TypeSize
int8_t, uint8_t8 bits
int16_t, uint16_t16 bits
int32_t, uint32_t32 bits
int64_t, uint64_t64 bits
C
#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;
}
Output
200 9000000000

inttypes.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
C
#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;
}
Output
Revenue: 2500000000
Largest int: 2147483647

2,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?