Skip to content

Module 2 · Variables, Types, Input and Operators — topic 3 of 6

Data Types

C's built-in types — char, int, float, double and their short, long, signed and unsigned variants — decide how many bytes a value occupies and what range it can hold.

Beginner 16 min read·Lesson 7 of 37 in C Programming

Learning objectives

  • List the basic C types with typical sizes and ranges
  • Use sizeof and <limits.h> to inspect types
  • Explain signed vs unsigned and integer overflow

Definition

A data type defines a set of values, their in-memory representation and size, and the operations that may be performed on them. C's basic types are the integer types (char, short, int, long, long long, _Bool) and the real floating types (float, double, long double).

Introduction

Every value in memory is just bits. The type tells the compiler how to interpret those bits: as a whole number, a character code or a floating-point number, and how many bytes to read.

The C standard only fixes minimum ranges — for example an int must hold at least −32767 to 32767. On today's 64-bit desktops an int is 4 bytes, but on some microcontrollers it is 2. Good C code checks sizes rather than assuming them.

Why it matters

Choosing a type too small causes overflow — a classic source of real-world failures. Choosing floating point for money causes rounding errors. Knowing the types lets you pick the right one deliberately.

Questions such as “what is sizeof(char)?”, “what happens on unsigned overflow?” and “why is 0.1 + 0.2 != 0.3?” are staples of placement tests.

Detailed explanation

TypeTypical size (64-bit)Typical rangeprintf
char1 byte−128…127 or 0…255%c / %d
short2 bytes−32 768…32 767%hd
int4 bytes−2 147 483 648…2 147 483 647%d
long8 bytes (Linux/macOS), 4 bytes (Windows)platform dependent%ld
long long8 bytesabout ±9.2 × 10¹⁸%lld
unsigned int4 bytes0…4 294 967 295%u
float4 bytes~7 significant digits%f
double8 bytes~15–16 significant digits%f / %lf in scanf
_Bool / bool1 byte0 or 1%d

sizeof gives the size of a type or object in bytes as a size_t; print it with %zu. By definition sizeof(char) is 1.

Unsigned types cannot be negative. Arithmetic on them wraps around modulo 2ⁿ, which is well defined. Signed overflow (e.g. INT_MAX + 1) is undefined behaviour — the compiler may do anything.

Floating-point numbers store a binary fraction and an exponent, so most decimals (like 0.1) are approximations. Prefer double over float for accuracy, and never compare floating results with ==.

For exact widths use <stdint.h>: int32_t, uint8_t, int64_t. For true/false use <stdbool.h>, which provides bool, true and false.

Syntax

C
char grade = 'A';
int count = 42;
unsigned int flags = 7u;
long long big = 9000000000LL;
float ratio = 0.5f;
double pi = 3.141592653589793;
bool done = false; /* needs <stdbool.h> before C23 */
size_t bytes = sizeof(double);
  • Suffixes u, LL and f set the literal's type.
  • sizeof is an operator, not a function; parentheses are required only around type names.
  • size_t is the unsigned type used for sizes and counts.

Code examples

Sizes on this machine

C
#include <stdio.h>
int main(void) {
printf("char %zu\n", sizeof(char));
printf("int %zu\n", sizeof(int));
printf("double %zu\n", sizeof(double));
return 0;
}
Output
char   1
int    4
double 8

char is always 1; the others are typical for 32- and 64-bit desktops.

Limits from <limits.h>

C
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("INT_MAX = %d\n", INT_MAX);
printf("INT_MIN = %d\n", INT_MIN);
printf("UINT_MAX = %u\n", UINT_MAX);
return 0;
}
Output
INT_MAX  = 2147483647
INT_MIN  = -2147483648
UINT_MAX = 4294967295

More examples

Unsigned wrap-around and float precision

C
#include <stdio.h>
#include <limits.h>
int main(void) {
unsigned int u = UINT_MAX;
u = u + 1; /* well defined: wraps to 0 */
printf("UINT_MAX + 1 = %u\n", u);
unsigned int small = 0;
small = small - 1; /* wraps to UINT_MAX */
printf("0u - 1 = %u\n", small);
double x = 0.1 + 0.2;
printf("0.1 + 0.2 = %.17f\n", x);
printf("equal to 0.3? %d\n", x == 0.3);
return 0;
}
Output
UINT_MAX + 1 = 0
0u - 1 = 4294967295
0.1 + 0.2 = 0.30000000000000004
equal to 0.3? 0

Step-by-step explanation

  • u starts at the largest unsigned int; adding 1 wraps around to 0 by definition.
  • Subtracting 1 from an unsigned 0 wraps the other way to 4294967295.
  • 0.1 and 0.2 are stored as binary approximations, so their sum is slightly more than 0.3.
  • %.17f reveals the hidden digits; x == 0.3 is therefore false and prints 0.

Expected output

Running “Unsigned wrap-around and float precision” prints exactly:

TextOutput
UINT_MAX + 1 = 0
0u - 1 = 4294967295
0.1 + 0.2 = 0.30000000000000004
equal to 0.3? 0

Practical applications

  • Network protocols use exact-width types such as uint16_t for port numbers.
  • Image libraries store pixel channels as uint8_t (0–255).
  • Banking systems store money as integers of paise, never as float, to avoid rounding errors.
  • The Ariane 5 rocket failure (1996) was caused by converting a 64-bit float to a 16-bit integer that overflowed.

Best practices

  • Use int for ordinary counting, double for real numbers, size_t for sizes and indexes.
  • Use <stdint.h> types when the exact width matters (files, networks, hardware).
  • Never store currency in float; use integer units such as paise.

Common mistakes

Comparing floats with ==

Avoid

if (0.1 + 0.2 == 0.3) { /* never true */ }

Prefer

#include <math.h>
if (fabs((0.1 + 0.2) - 0.3) < 1e-9) { /* close enough */ }

Floating-point results carry rounding error. Compare with a small tolerance.

Wrong printf specifier

Avoid

long long n = 5000000000LL;
printf("%d\n", n);

Prefer

long long n = 5000000000LL;
printf("%lld\n", n);

A mismatched format specifier is undefined behaviour and usually prints garbage. -Wall warns about it.

Important notes

  • Whether plain char is signed or unsigned is implementation-defined; use signed char or unsigned char when it matters.
  • long is 8 bytes on Linux and macOS but only 4 bytes on 64-bit Windows; use long long or int64_t for portable 64-bit integers.

Key takeaways

  • Types fix size, range and operations; the standard gives minimums, not exact sizes.
  • Unsigned overflow wraps; signed overflow is undefined behaviour.
  • Floating point is approximate — don't compare with ==.

Try it yourself

  1. 1
    Print the size of short, long, long long, float and long double on your computer.
  2. 2
    Store 300 in an unsigned char and print it. Explain the result.
    Show hint

    An unsigned char holds 0–255; think modulo 256.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

8 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is `sizeof(char)` guaranteed to be?
  • BeginnerWhich format specifier prints a `long long` value?
  • BeginnerWhy should money not be stored in a `float`?
Practise this topic