- The difference between declaring a variable and initializing it
- The fundamental C data types —
int,float,char,double, plusshort,long, andunsigned - Which format specifier matches which type in
printf - Why
floatliterals need thefsuffix andlongliterals need theLsuffix - How to use
sizeofto see how much memory each type occupies - Why choosing the right type matters for correctness
What variables and data types are
A variable is a named storage location in memory. When you declare int age;, the compiler reserves space for an integer and associates the name age with that space. Every time you write age in the code, the compiler knows to look at that reserved location. Every time you write age = 25;, it stores 25 there. Every time you write printf("%d", age);, it reads back whatever is stored.
A data type tells the compiler how much space to reserve, how to interpret the bits stored there, and what operations are valid. An int on a typical 32-bit system takes 4 bytes and holds a whole number from about −2.1 billion to +2.1 billion. A double takes 8 bytes and holds a floating-point value with about 15 significant decimal digits. A char takes 1 byte and holds a single character (more precisely, a small integer representing a character's code point).
Declaration is the act of introducing a name and giving it a type — int age;. Initialization is the act of giving it a first value — int age = 25; (declaration and initialization combined), or age = 25; after a separate declaration. You can declare without initializing (the variable's value is then indeterminate), but you should not read an uninitialized variable — its bits are whatever happened to be in that memory location, which could be anything.
C has a small set of fundamental types that everything else is built from. The integer types — char, short, int, long, and their unsigned variants — store whole numbers. The floating-point types — float and double — store fractional numbers. And void is the absence of a type, used for functions that return nothing. Everything else — pointers, arrays, structs, unions — is built from these primitives.
💡 In one line: a variable is a named memory location; a data type determines how much memory it uses and what operations are valid. Declare a variable to introduce it, initialize it to give it a first value.
The fundamental C types at a glance
Here's a reference table for the types we'll use in the program:
| Type | Typical Size | Range / Precision | Format Specifier |
|---|---|---|---|
char | 1 byte | −128 to 127 (or a single char) | %c (char) or %d (as int) |
short | 2 bytes | −32,768 to 32,767 | %hd |
int | 4 bytes | −2,147,483,648 to 2,147,483,647 | %d |
long | 4 or 8 bytes | Platform-dependent | %ld |
unsigned int | 4 bytes | 0 to 4,294,967,295 | %u |
float | 4 bytes | ~7 decimal digits | %f |
double | 8 bytes | ~15 decimal digits | %lf |
The sizes are "typical" because the C standard leaves them partly implementation-defined. An int is at least 16 bits, but most modern systems use 32. A long is at least 32 bits, but on 64-bit Linux it's 64 bits, while on Windows it's still 32. If you need a guaranteed size, use the fixed-width types from <stdint.h>: int32_t, uint64_t, and so on.
The format specifier must match the type you're printing. Printing an int with %f, or a double with %d, is undefined behavior — the bytes are reinterpreted incorrectly, giving nonsense. Always match specifier to type: %d for int, %f for float, %lf for double, %c for char, %zu for size_t (the return type of sizeof).
🎯 Key idea: pick the type based on the range and precision you need. For a person's age, int is overkill but standard. For a year, short would work if you're memory-constrained. For a price, double is safer than int (which would lose cents). For a flag or a character, char is right. Choosing wrong costs either memory (if you pick too big) or correctness (if you pick too small).
Complete C Program
Program: declare, initialize, and print one variable of each fundamental type
#include <stdio.h>
int main(void) {
/* ---------- Integer types ---------- */
int age = 25; /* typical whole number */
short small = 100; /* shorter int, fits in 2 bytes */
long large = 1000000L; /* longer int, at least 4 bytes */
unsigned int positive = 4000000000u; /* 0 to ~4.29 billion */
/* ---------- Floating-point types ---------- */
float temperature = 98.6f; /* single-precision, ~7 digits */
double pi = 3.14159265358979; /* double-precision, ~15 digits */
/* ---------- Character type ---------- */
char grade = 'A'; /* a single character */
/* ---------- Print all values with matching specifiers ---------- */
printf("int age = %d\n", age);
printf("short small = %hd\n", small);
printf("long large = %ld\n", large);
printf("unsigned pos = %u\n", positive);
printf("float temp = %.1f\n", temperature);
printf("double pi = %.14lf\n", pi);
printf("char grade = %c\n", grade);
/* ---------- Print the size of each variable ---------- */
printf("\nSizes (in bytes):\n");
printf("sizeof(age) = %zu\n", sizeof(age));
printf("sizeof(small) = %zu\n", sizeof(small));
printf("sizeof(large) = %zu\n", sizeof(large));
printf("sizeof(positive) = %zu\n", sizeof(positive));
printf("sizeof(temperature)= %zu\n", sizeof(temperature));
printf("sizeof(pi) = %zu\n", sizeof(pi));
printf("sizeof(grade) = %zu\n", sizeof(grade));
return 0;
}
The program declares one variable of each fundamental type, initializes each to a representative value, and then prints both the value and the size in bytes. The value print shows the type's purpose (whole numbers, decimals, characters); the size print shows that each type occupies a different amount of memory.
Note the literal suffixes: 1000000L (the L makes it long), 4000000000u (the u makes it unsigned), and 98.6f (the f makes it float, not double). Without the suffix, 98.6 would be a double — the assignment float temperature = 98.6; would still work, but the value would be silently downcast, and -Wconversion might warn. The suffix makes the intent explicit.
And here's a compact version that uses a lookup table to print all the types in a consistent format:
Variant: use a small lookup table
#include <stdio.h>
int main(void) {
int i = 42;
float f = 3.14f;
double d = 2.718281828;
char c = 'Z';
short s = -5;
long l = 1000000L;
unsigned u = 3000000000u;
printf("Type | Value | Size\n");
printf("-----------|------------------|-----\n");
printf("int | %d | %zu bytes\n", i, sizeof(i));
printf("float | %.2f | %zu bytes\n", f, sizeof(f));
printf("double | %.6lf | %zu bytes\n", d, sizeof(d));
printf("char | '%c' | %zu byte\n", c, sizeof(c));
printf("short | %hd | %zu bytes\n", s, sizeof(s));
printf("long | %ld | %zu bytes\n", l, sizeof(l));
printf("unsigned | %u | %zu bytes\n", u, sizeof(u));
return 0;
}
Both programs do essentially the same thing — the first focuses on values, the second focuses on a tabular view. For a viva answer, the first is usually what's expected. For self-study, the second makes the comparisons easier to scan. The alignment in the table is imperfect (the %d and %zu have different widths), but with careful spacing it's readable.
Program Output
Here is typical output from the first program on a 64-bit Linux system:
int age = 25
short small = 100
long large = 1000000
unsigned pos = 4000000000
float temp = 98.6
double pi = 3.14159265358979
char grade = A
Sizes (in bytes):
sizeof(age) = 4
sizeof(small) = 2
sizeof(large) = 8
sizeof(positive) = 4
sizeof(temperature)= 4
sizeof(pi) = 8
sizeof(grade) = 1
Each line matches the type's purpose: int holds 25 (a typical age), short holds 100 (small number), long holds 1,000,000 (larger), unsigned int holds 4,000,000,000 (out of range for signed int, but fine for unsigned), float holds 98.6 (temperature), double holds π (many decimal digits), char holds 'A'. The size section shows how much memory each type needs on this platform.
The output varies by platform. On a 32-bit system, long would be 4 bytes, not 8. On Windows with MSVC, long is also 4 bytes (it's long long that's 8). The C standard does not fix these sizes — it only guarantees minimums. This is why the table said "typical" rather than "always."
And the second program's tabular output:
Type | Value | Size
-----------|------------------|-----
int | 42 | 4 bytes
float | 3.14 | 4 bytes
double | 2.718282 | 8 bytes
char | 'Z' | 1 byte
short | -5 | 2 bytes
long | 1000000 | 8 bytes
unsigned | 3000000000 | 4 bytes
Same information, different presentation. The table format is nice when you want a quick reference; the first format is better for one-off values.
🔍 Try this: run the program on different platforms (Linux, macOS, Windows). You'll notice that sizeof(long) varies (8 on 64-bit Linux, 4 on Windows even in 64-bit mode), while sizeof(int) and sizeof(double) are consistent (4 and 8). This is a real portability issue — programs that assume long is 8 bytes can silently break on Windows. Use long long or int64_t when you need a guaranteed 8-byte integer.
Step-by-Step Explanation
Let's walk through the program line by line, then explain the underlying concepts.
1. The include
#include <stdio.h> — necessary for printf and sizeof's print via %zu. (Strictly, sizeof is a C operator, not a function — but its result is printed via printf, so we need the header.)
2. Integer declarations
int age = 25; — declares an integer named age and initializes it to 25. On a typical system, int is 4 bytes, holding values from about −2.1 to +2.1 billion. 25 is well within range.
short small = 100; — short is a smaller integer type, typically 2 bytes. Range is about ±32,000. 100 fits comfortably.
long large = 1000000L; — long is at least 4 bytes, sometimes 8. The L suffix on the literal 1000000L marks it as long explicitly, avoiding any implicit conversion. Without the suffix, the literal is int (which can hold 1,000,000 fine), but the assignment to a long would implicitly widen it. The suffix is good style.
unsigned int positive = 4000000000u; — unsigned int can hold values from 0 to about 4.29 billion. 4,000,000,000 exceeds the signed int max of about 2.1 billion, but fits in unsigned int. The u suffix marks the literal as unsigned. Without it, the compiler would treat 4000000000 as long or long long (since it doesn't fit in int), and the assignment would still work — but the suffix makes the intent explicit.
3. Floating-point declarations
float temperature = 98.6f; — float is a single-precision floating-point type, 4 bytes, about 7 significant decimal digits. The f suffix on 98.6f marks the literal as float; without it, 98.6 would be a double, and the assignment would silently downcast (with a possible warning under -Wconversion).
double pi = 3.14159265358979; — double is double-precision, 8 bytes, about 15 significant decimal digits. No suffix is needed — a floating-point literal without f is double by default. This is why double is the "default" floating-point type in C.
4. The char declaration
char grade = 'A'; — char is a 1-byte type that holds a single character (technically a small integer, whose value corresponds to a character code — 'A' is 65 in ASCII). The value is written in single quotes, which distinguishes it from a string literal "A" (which is a 2-byte array: 'A' plus a null terminator).
A common mistake: writing char grade = "A"; (double quotes). That would attempt to assign a const char * (a pointer to the string) to a char, which is a type mismatch and would produce a warning or error. Always use single quotes for single characters.
5. The print statements
Each printf uses a format specifier that matches its variable's type:
%dforint— prints a signed decimal integer.%hdforshort— thehis a length modifier, tellingprintfto interpret the argument asshort(which is promoted tointfor varargs, but thehtells the formatter to narrow it back).%ldforlong— thelis a length modifier forlong.%uforunsigned int— prints as an unsigned decimal integer.%.1fforfloat—ffor floating-point,.1for one decimal place. Note: forprintf,%faccepts bothfloatanddoubledue to default argument promotion in varargs — afloatpassed toprintfis promoted todouble. So%fis correct for both, though%.1fcontrols the precision.%.14lffordouble— thelin%lfis technically a no-op forprintf(it's required forscanfbut ignored byprintf), but it's a common convention for documentation. The.14prints 14 decimal places, showing the precision ofdouble.%cforchar— prints a single character.
Using the wrong specifier is undefined behavior — for example, printing an int with %f would reinterpret the 4-byte integer as a float, producing a completely unrelated value. Always match specifier to type.
6. The sizeof operator
sizeof(age) — returns the size in bytes of age's type, i.e., sizeof(int). sizeof is an operator, not a function; it's evaluated at compile time (in most cases) and its result is a size_t (an unsigned integer type, typically 8 bytes on 64-bit systems).
The %zu format specifier matches size_t. The z is a length modifier meaning "size_t-sized" and u means unsigned. This is the portable way to print sizeof results. Some older code uses %lu (casting to unsigned long), but %zu is correct for C99 and later.
7. Why the sizes vary by platform
The C standard guarantees minimums: char ≥ 8 bits, short ≥ 16 bits, int ≥ 16 bits, long ≥ 32 bits, float ≥ 32 bits, double ≥ 64 bits. It doesn't mandate exact sizes. In practice, common sizes are:
- char: always 8 bits on modern systems.
- short: always 16 bits.
- int: 32 bits on nearly all modern systems (16 bits on some embedded systems).
- long: 32 bits on Windows and 32-bit Linux, 64 bits on 64-bit Linux and macOS.
- long long: always at least 64 bits.
- float: always 32 bits.
- double: always 64 bits.
Because of this variation, portable code uses the fixed-width types from <stdint.h>: int8_t, int16_t, int32_t, int64_t (and their unsigned counterparts). These guarantee exact widths on every platform that supports them.
8. The f, L, and u suffixes
Literal suffixes tell the compiler the type of a numeric literal:
98.6f— afloatliteral. Without thef,98.6would be adouble.1000000L— alongliteral. Without theL, it would beint(which fits 1,000,000 fine, but the intent is unclear).4000000000u— anunsigned intliteral. Without theu, it would belong(since it exceedsintmax), but the intent is unclear.
These suffixes aren't strictly required in most cases — the compiler will often infer the right type from context. But they document intent, prevent unintended conversions, and avoid warnings under strict flags. Use them consistently.
9. Common pitfalls
A few traps to watch for:
- Using
double quotesfor a char literal:char c = "A";is wrong — it should bechar c = 'A';. The double quotes make a string, not a character. - Reading an uninitialized variable: if you declare
int x;and then printxwithout initializing, you'll get garbage (whatever happened to be in that memory). Always initialize before use. - Mismatched format specifiers: printing
floatwith%dorintwith%fis undefined behavior. Match carefully. - Assuming sizes: writing code that assumes
longis 8 bytes breaks on Windows. Usesizeofor fixed-width types when size matters. - Signed vs. unsigned overflow:
unsigned intwraps around at its maximum, whileintoverflow is undefined behavior. Neither is safe for sensitive calculations — use a wider type or check bounds.
🎯 Key idea: the C standard doesn't guarantee exact sizes for most types — only minimums. On a given platform, sizeof tells you the actual size. Write portable code by using fixed-width types (int32_t, etc.) when exact sizes matter, and by always using sizeof rather than hardcoded numbers.
Frequently Asked Questions
1. What's the difference between declaring and initializing a variable?
Declaring a variable introduces its name and type, reserving memory — int x;. Initializing gives it a first value — int x = 5; (declaration + initialization), or x = 5; after a separate declaration. You can declare without initializing, but reading the variable before assigning to it gives an indeterminate value (undefined behavior). Always initialize variables before use.
2. Why does sizeof(int) sometimes print 4 and sometimes 8?
Because the C standard doesn't fix the size of int — it only requires at least 16 bits. On virtually all modern systems, int is 32 bits (4 bytes). But on some embedded systems, it's 16 bits. Some experimental compilers might even use 64-bit int. If you need a guaranteed size, use the types from <stdint.h>: int32_t, int64_t, etc.
3. What's the difference between float and double?
float is single-precision (32 bits, ~7 decimal digits), double is double-precision (64 bits, ~15 decimal digits). double is the default for floating-point literals in C. For simple calculations, float is fine; for anything that requires precision (financial calculations, scientific computing), double is safer. On modern hardware, double is often as fast as float, so there's rarely a reason to use float unless you're memory-constrained.
4. Why does char hold characters instead of numbers?
It doesn't really — char holds a small integer (a byte, so values 0–255 or −128–127 depending on signedness). The "character" interpretation comes from the ASCII encoding: 'A' is 65, 'a' is 97, etc. When you write char c = 'A';, you're storing 65. When printf sees %c, it interprets the stored value as an ASCII character and displays "A". If you print the same variable with %d, you'll see 65. So char is really "the integer type used for character storage."
5. Why does sizeof(long) differ between Windows and Linux?
Because the two platforms chose different data models. On 64-bit Linux and macOS, the model is "LP64": long is 64 bits, pointer is 64 bits, int is 32 bits. On 64-bit Windows, the model is "LLP64": long is 32 bits (same as int), long long is 64 bits. This is a historical compatibility choice — Windows kept long at 32 bits to maintain compatibility with its 32-bit codebase. For portable code, use int64_t instead of long.
6. Can I store a character in an int variable?
Yes, technically, because a char is just a small integer. int x = 'A'; stores 65 in x. You can print x with %c and see "A", or with %d and see 65. However, this is usually a sign of confusion about types — a variable intended to hold a character should be declared as char. The exception is when you're deliberately working with character codes (e.g., encoding/decoding), where int might be more convenient because it can hold all code points including extended Unicode (beyond the 8-bit range of char).
💡 Tip: once this works, try the assignment with long long and unsigned long long. Add them to the program, and see their sizes (typically 8 bytes each). Then try printing the maximum values: INT_MAX, INT_MIN, LLONG_MAX, ULLONG_MAX — defined in <limits.h>. That exercise shows you exactly what range each type can hold, which is invaluable when deciding which type to use.