int vs long int — Whole Numbers
int stores whole numbers in 4 bytes (32 bits). It can hold values from -2,147,483,648 to +2,147,483,647 — roughly ±2 billion.
long int stores whole numbers in 8 bytes (64 bits). It can hold values from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 — roughly ±9.2 quintillion.
- int format specifier →
%d - long int format specifier →
%ld
Memory layout — how bits are used
#include <stdio.h> int main() { // INT — 4 bytes — holds up to ~2 billion int a = 100; int b = -500; int c = 2147483647; // MAX value of int printf("int a = %d\n", a); printf("int b = %d\n", b); printf("int max = %d\n", c); // LONG INT — 8 bytes — holds much bigger numbers long int x = 100000; long int y = -9999999; long int z = 9223372036854775807; // MAX of long int printf("long int x = %ld\n", x); printf("long int y = %ld\n", y); printf("long int max = %ld\n", z); return 0; }
int a = 100 int b = -500 int max = 2147483647 long int x = 100000 long int y = -9999999 long int max = 9223372036854775807
int = 4 bytes = 32 bits → 2³¹ - 1 = 2,147,483,647 (1 bit used for ± sign)
long int = 8 bytes = 64 bits → 2⁶³ - 1 = 9,223,372,036,854,775,807
float vs double vs long double
float uses 4 bytes and gives about 7 digits of accuracy. Good for simple prices or small decimals.
double uses 8 bytes and gives about 15 digits of accuracy. This is the default decimal type in C — use this most of the time.
long double uses 16 bytes and gives about 18 digits of accuracy. Used in science, engineering, and astronomy.
#include <stdio.h> int main() { // FLOAT — 4 bytes — ~7 digits precision float f = 3.14159265358979; printf("float = %.7f\n", f); // only 7 digits reliable // DOUBLE — 8 bytes — ~15 digits precision double d = 3.14159265358979; printf("double = %.15f\n", d); // 15 digits reliable // LONG DOUBLE — 16 bytes — ~18 digits precision long double ld = 3.14159265358979323846L; printf("long double = %.18Lf\n", ld); // 18 digits reliable return 0; }
float = 3.1415927 ← rounds at 7th digit double = 3.141592653589790 long double = 3.141592653589793238
| Type | Bytes | Format (printf) | Format (scanf) | Accurate Digits |
|---|---|---|---|---|
| float | 4 | %f | %f | ~7 |
| double | 8 | %f or %lf | %lf | ~15 |
| long double | 16 | %Lf (capital L) | %Lf | ~18 |
Always use
%f for float and %lf for double inside scanf.
Mixing them up causes silent wrong values — no error message, just wrong data.
%.nf Precision — How Many Decimal Places?
The format %.nf controls exactly how many decimal places are printed. The n tells printf how many digits to show after the decimal point. C rounds automatically at the last digit shown.
- Minimum n:
0— shows no decimal places at all - Useful maximum:
15for double,18for long double - Beyond that: garbage digits — CPU has no real data left to show
#include <stdio.h> int main() { double num = 3.123456789; printf("%.0f\n", num); // 0 decimal places printf("%.1f\n", num); // 1 decimal place printf("%.2f\n", num); // 2 decimal places printf("%.5f\n", num); // 5 decimal places — note rounding printf("%.10f\n", num); // 10 decimal places printf("%.15f\n", num); // last accurate digits printf("%.20f\n", num); // garbage starts here! return 0; }
3 ← %.0f — whole number only 3.1 ← %.1f 3.12 ← %.2f 3.12346 ← %.5f — 5 rounds up! 3.1234567890 ← %.10f 3.123456789000000 ← %.15f — still accurate 3.12345678900000017763568394 ← %.20f — garbage after 17!
| Format | Output | Use For |
|---|---|---|
| %.0f | 3 | Whole number display |
| %.1f | 3.1 | Simple temperature |
| %.2f | 3.12 | Money / prices ₹99.50 |
| %.4f | 3.1235 | Engineering values |
| %.6f | 3.123457 | Default %f (6 places) |
| %.15f | 3.123456789000000 | Max safe for double |
| %.20f | 3.12345…garbage | ❌ Do not use |
3.123456789 with %.5f gives 3.12346 — the 6th digit was 6, so the 5th digit 5 rounds up to 6. C always rounds the last shown digit automatically.
Integer Overflow — What Happens at the Limit
When you go one step beyond the maximum value of an integer type, it does not crash — it wraps around to the most negative number. This is called integer overflow and is one of the most dangerous silent bugs in C.
int overflow — what happens step by step
#include <stdio.h> int main() { // INT overflow int a = 2147483647; // MAX int int b = 2147483647 + 1; // OVERFLOW! printf("int max = %d\n", a); printf("int max + 1 = %d\n", b); // shows -2147483648 ! // Real world: 3 billion too big for int! int wrong = 3000000000; // WRONG — overflows long int correct = 3000000000; // CORRECT — fits fine printf("int wrong = %d\n", wrong); // garbage! printf("long correct= %ld\n", correct); // 3000000000 // LONG INT overflow long int c = 9223372036854775807; // MAX long long int d = 9223372036854775807 + 1; // OVERFLOW! printf("long max = %ld\n", c); printf("long max + 1= %ld\n", d); // shows most negative! return 0; }
int max = 2147483647 int max + 1 = -2147483648 ← overflow! int wrong = -1294967296 ← garbage! long correct = 3000000000 ← correct long max = 9223372036854775807 long max + 1 = -9223372036854775808 ← overflow!
long int when your value might exceed 2 billion. For population, file sizes, or large counts — always use long int.
sizeof() and limits.h — Check Size & Limits in Code
sizeof() is a built-in operator that returns the number of bytes any type or variable uses in memory. It is extremely useful for understanding and debugging memory usage.
<limits.h> is a library that provides ready-made constants like INT_MAX, INT_MIN, LONG_MAX — so you never have to remember the exact numbers.
#include <stdio.h> #include <limits.h> // library for INT_MAX, LONG_MAX etc. int main() { // sizeof — how many bytes each type uses printf("--- SIZES ---\n"); printf("int = %lu bytes\n", sizeof(int)); printf("long int = %lu bytes\n", sizeof(long int)); printf("float = %lu bytes\n", sizeof(float)); printf("double = %lu bytes\n", sizeof(double)); printf("long double = %lu bytes\n", sizeof(long double)); printf("char = %lu bytes\n", sizeof(char)); // limits.h constants — INT_MAX, LONG_MAX etc. printf("\n--- INT LIMITS ---\n"); printf("INT_MIN = %d\n", INT_MIN); printf("INT_MAX = %d\n", INT_MAX); printf("\n--- LONG LIMITS ---\n"); printf("LONG_MIN = %ld\n", LONG_MIN); printf("LONG_MAX = %ld\n", LONG_MAX); return 0; }
--- SIZES --- int = 4 bytes long int = 8 bytes float = 4 bytes double = 8 bytes long double = 16 bytes char = 1 bytes --- INT LIMITS --- INT_MIN = -2147483648 INT_MAX = 2147483647 --- LONG LIMITS --- LONG_MIN = -9223372036854775808 LONG_MAX = 9223372036854775807
sizeof() returns type size_t which is an unsigned long. Always print it with %lu, not %d, to avoid compiler warnings.
All Types Together — Complete Reference Program
Here is one single program that demonstrates every data type together — int, long int, float, double, long double — with their values, format specifiers, and sizes all in one place. Save this as your reference.
#include <stdio.h> int main() { // ── INTEGER TYPES ── int i = 2147483647; long int li = 9223372036854775807; // ── DECIMAL TYPES ── float f = 3.14159265358979; double d = 3.14159265358979; long double ld = 3.14159265358979323846L; printf("--- INTEGER TYPES ---\n"); printf("int = %d\n", i); printf("long int = %ld\n", li); printf("\n--- DECIMAL TYPES ---\n"); printf("float = %.7f\n", f); printf("double = %.15f\n", d); printf("long double = %.18Lf\n", ld); printf("\n--- SIZES IN MEMORY ---\n"); printf("int = %lu bytes\n", sizeof(int)); printf("long int = %lu bytes\n", sizeof(long int)); printf("float = %lu bytes\n", sizeof(float)); printf("double = %lu bytes\n", sizeof(double)); printf("long double = %lu bytes\n", sizeof(long double)); return 0; }
--- INTEGER TYPES --- int = 2147483647 long int = 9223372036854775807 --- DECIMAL TYPES --- float = 3.1415927 double = 3.141592653589790 long double = 3.141592653589793238 --- SIZES IN MEMORY --- int = 4 bytes long int = 8 bytes float = 4 bytes double = 8 bytes long double = 16 bytes
Complete Quick Reference Table
| Type | Bytes | Bits | printf | scanf | Max Value | Use For |
|---|---|---|---|---|---|---|
| int | 4 | 32 | %d | %d | 2,147,483,647 | age, score, year |
| long int | 8 | 64 | %ld | %ld | 9,223,372,036,854,775,807 | population, big counts |
| float | 4 | 32 | %f | %f | 3.4 × 10³⁸ | simple decimals |
| double | 8 | 64 | %f | %lf | 1.7 × 10³⁰⁸ | precise decimals ✅ |
| long double | 16 | 128 | %Lf | %Lf | 1.1 × 10⁴⁹³² | science / max precision |
| char | 1 | 8 | %c | %c | 127 | single character |
Whole number →
int → too big? → long intDecimal number →
double by default → need more? → long doubleSimple / small decimal →
float (saves memory)
Quick Quiz — Test Yourself
What is the maximum value of int in C?
Which format specifier do you use to print a long int?
What happens when you store 3000000000 in an int variable?
How many reliable decimal digits does double give you?
Which format specifier is used to print a long double?
Lesson Checklist
- I know int uses 4 bytes and holds up to 2,147,483,647
- I know long int uses 8 bytes and holds up to 9.2 quintillion
- I use %d for int and %ld for long int
- I understand float gives ~7 digits of precision
- I understand double gives ~15 digits and is the default decimal type
- I know long double uses %Lf with capital L
- I understand %.nf controls decimal places in output
- I know beyond %.15f for double gives garbage digits
- I understand integer overflow — no error, just wrong value
- I can use sizeof() and know to print it with %lu
- I know limits.h provides INT_MAX, LONG_MAX constants
- I completed the quiz