Variables & Data Types in C
A variable in C is a named box in memory that holds a value, and its data type decides what kind of value fits in that box and how much memory it uses. Before you can use a variable in C, you must declare it with a type — for example int age; reserves space for a whole number called age.
What is a variable?
Declaring a variable tells the compiler its name and type. You can declare and assign in one line:
int score = 90; /* declare and initialize together */
float price; /* declare now... */
price = 49.5f; /* ...assign later */
A variable name must start with a letter or underscore, and can contain letters, digits, and underscores. C is case-sensitive: Score and score are different variables.
The core data types
C's built-in types fall into a few families. The exact sizes depend on the platform, but typical values on a 64-bit system are shown below.
| Type | Holds | Typical size | Format specifier |
|---|---|---|---|
int | whole numbers | 4 bytes | %d |
float | single-precision decimals | 4 bytes | %f |
double | double-precision decimals | 8 bytes | %lf |
char | a single character | 1 byte | %c |
You can also add modifiers like short, long, and unsigned to change range, e.g. unsigned int (no negatives) or long long (a wider integer).
A complete example
#include <stdio.h>
int main(void) {
int age = 19; /* whole number */
float height = 5.8f; /* the f suffix marks a float literal */
double pi = 3.14159; /* more precise decimal */
char grade = 'A'; /* single character in single quotes */
/* Each format specifier must MATCH its variable's type */
printf("Age: %d\n", age);
printf("Height: %.1f feet\n", height); /* .1 = one decimal place */
printf("Pi: %lf\n", pi);
printf("Grade: %c\n", grade);
return 0;
}
Notice 'A' uses single quotes (a char), while text like "Pi:" uses double quotes (a string). Mixing them up is a classic beginner error.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingConstants and the const keyword
If a value should never change, mark it const:
const double GRAVITY = 9.8; /* the compiler stops you reassigning it */
This documents intent and lets the compiler catch accidental edits.
Choosing the right type
- Counting things? Use
int. - Money or measurements with decimals? Prefer
doubleoverfloatfor accuracy. - A single letter or symbol? Use
char. - Storing text? You need an array of
char— see Strings in C.
Common mistakes
- Wrong format specifier: printing a
doublewith%d(or anintwith%f) gives garbage. Match the specifier to the type. - Forgetting to initialize: an uninitialized local variable holds a random value, not zero. Always assign before reading.
- Single vs double quotes:
'A'is a char;"A"is a string (two bytes including the null terminator). - Integer division surprise:
5 / 2is2, not2.5, because both operands are integers. Use5.0 / 2to get2.5. - Float precision:
0.1 + 0.2is not exactly0.3. Never compare floats with==.
FAQ
float or double? Use double by default; it has more precision. Use float only when memory is tight.
Can a variable change type? No. In C a variable's type is fixed at declaration.
Keep exploring at the hub C Programming, then Operators in C and If-Else in C.
Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in C
A beginner guide to arrays in C — declaring, initializing and indexing them, looping over elements, multidimensional arrays, and avoiding out-of-bounds errors.
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
