If you're learning C f, this is one topic you simply can't skip. Every C program—whether it's a simple calculator or a complex operating system—starts with data and the operations performed on it.
When I started learning C, I was eager to jump straight into loops, functions, and data structures. But after solving a few programming problems, I realized something important: most beginner mistakes happen because we don't fully understand data types and operators.
These may seem like basic concepts, but they are the building blocks of everything you'll write in C.
Let's understand them in a simple way.
What Exactly Is Data?
Every program works with information.
For example:
- Your age
- Your exam marks
- A student's name
- The price of a product
- The temperature outside
All of these are pieces of data. A computer doesn't understand these values unless we tell it what kind of data they are.
That's where data types come in.
What Is a Data Type?
Think about moving to a new house.
You wouldn't pack books, clothes, and fragile glass items into the same box. Instead, you'd label different boxes according to what's inside.
A C compiler does something similar.
Before storing any value, it needs to know what kind of information it is dealing with.
That's the job of a data type.
A data type tells the compiler:
- what kind of value will be stored,
- how much memory should be reserved,
- what range of values is allowed, and
- what operations can safely be performed.
Without data types, the compiler would have no idea how to interpret the data stored in memory.
Why Do Data Types Matter?
Imagine storing the number 5.
Now imagine storing 98765432123456789.
If the computer reserved the same amount of memory for both values, it would waste a lot of space.
Different data types allow C to use memory efficiently while also improving performance.
This design is one of the reasons C is still used for operating systems, embedded systems, and performance-critical software.
The Four Basic Data Types in C
Let's look at the four primary data types every beginner should know.
1. int — For Whole Numbers
Whenever you need to store numbers without decimal places, use int.
int age = 20;
int marks = 95;
int temperature = -5;
Examples of integer values:
0
25
-100
9999
On most modern systems, an int occupies 4 bytes of memory.
2. char — For Single Characters
A char stores exactly one character.
char grade = 'A';
char gender = 'M';
char symbol = '#';
Notice the use of single quotes.
Internally, characters are stored as numbers using the ASCII character set.
For example,
'A' → 65
'B' → 66
'a' → 97
That's why this program prints 66.
char ch = 'B';
printf("%d", ch);
3. float — For Decimal Numbers
Need to store values like temperature or percentages?
Use float.
float temperature = 36.7;
float pi = 3.14;
A float usually occupies 4 bytes and provides around 6–7 digits of precision.
4. double — When You Need More Precision
Sometimes a float isn't accurate enough.
That's where double comes in.
double pi = 3.141592653589793;
A double generally occupies 8 bytes and can represent approximately 15 decimal digits accurately.
If precision matters, prefer double.
A Common Beginner Confusion: Why Doesn't 0.7 == 0.7?
Consider this code.
float x = 0.7;
if(x == 0.7)
printf("Equal");
else
printf("Not Equal");
Many beginners expect the output to be:
Equal
But on many systems, you'll actually see:
Not Equal
Why?
Because computers store floating-point numbers in binary.
Some decimal numbers—like 0.7—cannot be represented exactly in binary, so they are stored as the closest possible approximation.
That tiny approximation makes direct comparisons unreliable.
Interestingly, this program usually works:
float x = 0.5;
if(x == 0.5)
printf("Equal");
That's because 0.5 has an exact binary representation.
Takeaway: Avoid comparing floating-point numbers directly using ==. Comparing the difference against a very small value (epsilon) is usually a better approach.
Using sizeof to See Memory Usage
C provides a very useful operator called sizeof.
It tells us how many bytes a variable or data type occupies.
printf("%zu", sizeof(int));
Possible output:
4
You can also use it with variables.
int marks = 95;
printf("%zu", sizeof(marks));
One interesting example is:
int x = 5;
printf("%zu", sizeof(x++));
printf("%d", x);
Output:
4
5
Notice that x remains unchanged.
That's because sizeof determines the size from the variable's type—it doesn't execute the expression inside it.
Variables vs Constants
A variable can change during program execution.
int score = 80;
A constant cannot.
const float PI = 3.14159;
Constants make programs easier to read and prevent accidental modification.
What Are Operators?
If variables are the nouns of programming, operators are the verbs.
They tell the computer what action to perform.
Examples include:
- Adding numbers
- Comparing values
- Assigning data
- Checking conditions
- Performing logical operations
Without operators, a program would simply store data without doing anything useful.
Arithmetic Operators
These perform mathematical calculations.
| Operator | Purpose |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Remainder (Modulus) |
Example:
int a = 20;
int b = 3;
printf("%d\n", a + b);
printf("%d\n", a - b);
printf("%d\n", a * b);
printf("%d\n", a / b);
printf("%d\n", a % b);
Output:
23
17
60
6
2
Notice that 20 / 3 gives 6, not 6.67.
Since both operands are integers, C performs integer division.
Relational Operators
These compare two values.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Example:
printf("%d", 10 > 5);
Output:
1
In C,
-
1means true -
0means false
Logical Operators
Logical operators combine multiple conditions.
| Operator | Meaning |
|---|---|
| && | AND |
| ! | NOT |
Example:
int age = 22;
if(age >= 18 && age <= 60)
{
printf("Eligible");
}
Both conditions must be true.
Assignment Operators
The basic assignment operator is:
=
But C also provides shorthand versions.
+=
-=
*=
/=
%=
Example:
int x = 10;
x += 5;
This is simply another way of writing:
x = x + 5;
Pre-Increment vs Post-Increment
These two operators often confuse beginners.
Pre-increment
int x = 5;
printf("%d", ++x);
Output:
6
The value is increased first, then used.
Post-increment
int x = 5;
printf("%d", x++);
Output:
5
After the statement finishes, x becomes 6.
The increment happens after the current value is used.
The Conditional (Ternary) Operator
Instead of writing a complete if-else, you can write:
condition ? expression1 : expression2;
Example:
(age >= 18) ? printf("Adult") : printf("Minor");
It's compact and useful for simple decisions.
Beginner Mistakes Worth Avoiding
Here are a few mistakes almost everyone makes while learning C.
Mistake 1: Using = instead of ==
Incorrect:
if(x = 5)
Correct:
if(x == 5)
Mistake 2: Comparing floating-point numbers directly
Avoid:
if(value == 0.7)
Floating-point numbers often contain tiny precision errors.
Mistake 3: Expecting decimal results from integer division
5 / 2
Output:
2
Use at least one floating-point operand.
5.0 / 2
Output:
2.5
Key Takeaways
Let's quickly recap what we learned.
- Data types define what kind of information a variable stores.
-
int,char,float, anddoubleare the primary data types every beginner should know. - Operators allow us to perform calculations, comparisons, assignments, and logical decisions.
- Floating-point comparisons require extra care because not every decimal number can be represented exactly.
- Understanding these fundamentals will make topics like arrays, pointers, functions, and data structures much easier to learn.
Every experienced C programmer once struggled with these basics. Spending a little extra time mastering them now will save you hours of debugging later.
What's Next?
In the next article, I'll cover Control Statements in C, where we'll learn how programs make decisions using if, switch, for, while, and do-while loops.
If you're also learning C or preparing for GATE CSE, I'd love to know—what topic should we explore after control statements?
Top comments (1)
In the next article, I'll cover Control Statements in C, where we'll learn how programs make decisions using if, switch, for, while, and do-while loops. lets go ❤️🔥