Topic 2 of 15 · Beginner
Data Types, Variables & Operators
int, char, float, double, format specifiers and operators.
Concept
Basic types: char (1 byte), int (usually 4), float (4), double (8). Modifiers short, long, unsigned change the size or range.
printf/scanf use format specifiers: %d int, %ld long, %f float/double, %c char, %s string, %p pointer, %zu size_t.
Watch integer division (7/2 = 3), operator precedence and the difference between i++ (use then increment) and ++i.
Cheat sheet
| Type | Typical size | Specifier |
|---|---|---|
| char | 1 byte | %c |
| int | 4 bytes | %d |
| long long | 8 bytes | %lld |
| float | 4 bytes | %f |
| double | 8 bytes | %lf (scanf) |
Code (C17)
#include <stdio.h>
int main(void) {
int age;
double cgpa;
printf("Enter age and CGPA: ");
scanf("%d %lf", &age, &cgpa);
printf("Age %d, CGPA %.2f\n", age, cgpa);
printf("sizeof(int) = %zu\n", sizeof(int));
printf("7/2 = %d, 7/2.0 = %.1f\n", 7 / 2, 7 / 2.0);
return 0;
}Remember
- scanf needs addresses (&var).
- sizeof returns size_t.
- Uninitialised local variables hold garbage.
Interview questions
What is the output of int i = 5; printf("%d", i++ + ++i);?
Undefined behaviour — i is modified twice without a sequence point, so any answer is possible.
Difference between = and ==?
= assigns a value; == compares two values. if (x = 5) is a common bug.
Lesson test
Take an 8-question test on “Data Types, Variables & Operators” — concepts, output prediction, bug spotting and interview questions.