Basics Lesson 4 of 16
C Variables, Data Types and Constants
Declare variables in C with int, float, double and char, understand sizes with sizeof, convert types with casting, and create constants with const and #define.
A variable is a named piece of memory that stores a value. In C, you must declare each variable's type before using it, because the type tells C how many bytes to reserve and how to read them.
Basic types
| Type | Stores | Typical size | printf |
|---|---|---|---|
int | Whole numbers (about ±2.1 billion) | 4 bytes | %d |
long long | Very large whole numbers | 8 bytes | %lld |
float | Decimals, ~7 digits accuracy | 4 bytes | %f |
double | Decimals, ~15 digits accuracy | 8 bytes | %f |
char | One character (or small number) | 1 byte | %c |
C89 had no bool type; since C99, include <stdbool.h> to use bool, true and false. (In C23, they're built in.)
Declaring and using variables
#include <stdio.h>
int main(void) {
int students = 42;
double average = 88.75;
char section = 'B';
int boys, girls; /* declared, assigned later */
boys = 20;
girls = students - boys;
printf("Section %c has %d students (%d boys, %d girls).\n", section, students, boys, girls);
printf("Class average: %.2f\n", average);
return 0;
}
Output
Section B has 42 students (20 boys, 22 girls). Class average: 88.75
sizeof
#include <stdio.h>
int main(void) {
printf("char: %zu byte\n", sizeof(char));
printf("int: %zu bytes\n", sizeof(int));
printf("double: %zu bytes\n", sizeof(double));
return 0;
}
Output
char: 1 byte int: 4 bytes double: 8 bytes
sizeof gives the size in bytes. %zu is the specifier for its result type. Sizes can differ on unusual systems, which is why C programmers check with sizeof instead of assuming.
Characters are numbers
#include <stdio.h>
int main(void) {
char letter = 'A';
printf("%c is stored as %d\n", letter, letter);
printf("Next letter: %c\n", letter + 1);
return 0;
}
Output
A is stored as 65 Next letter: B
Each character has a number code (ASCII). That's why you can do math with characters.
Integer division and casting
#include <stdio.h>
int main(void) {
int total = 264, subjects = 3;
printf("%d\n", total / subjects);
printf("%d\n", 7 / 2);
printf("%.2f\n", (double) 7 / 2);
printf("%d\n", (int) 9.99);
return 0;
}
Output
88 3 3.50 9
int / int drops the decimal part. A cast like (double) converts a value to another type before the operation. Casting a decimal to int cuts off the decimals; it doesn't round.
Constants
Two ways to make a value that never changes:
#include <stdio.h>
#define PASSING 75 /* preprocessor constant: text replaced before compiling */
int main(void) {
const double VAT = 0.12; /* typed constant */
double price = 250;
printf("With VAT: %.2f\n", price * (1 + VAT));
printf("Passing grade: %d\n", PASSING);
return 0;
}
Output
With VAT: 280.00 Passing grade: 75
#define lines have no = and no semicolon. const variables have a type, so the compiler can check how you use them. Both are written in UPPERCASE by convention.
Naming rules
- Letters, digits and
_only; can't start with a digit. - Case-sensitive:
totalandTotalare different. - Can't be a keyword like
int,returnorwhile. - C programmers commonly use snake_case:
final_grade,student_count.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04