Chapter 3: Variables, Data Types, and Constants
1. Basic Data Types in C
C has a few basic (primitive) data types. Here are the most important ones beginners use:
| Data Type | What it stores | Typical Size (bytes) | Range (approximate) | Example Value |
|---|---|---|---|---|
| int | Whole numbers (no decimal) | 4 bytes | -2,147,483,648 to +2,147,483,647 | 25, -100, 0 |
| float | Decimal numbers (floating point) | 4 bytes | About 6 decimal places of precision | 3.14, 99.99, -0.5 |
| double | More accurate decimal numbers | 8 bytes | About 15 decimal places of precision | 3.141592653589793 |
| char | Single character | 1 byte | -128 to +127 (ASCII) | ‘A’, ‘5’, ‘$’ |
Quick Tip:
- Use int for ages, counts, scores
- Use float or double for money, temperature, distance
- Use double when you need more accuracy (most people use double instead of float)
2. Declaring and Initializing Variables
Declaration = Telling the computer: “I need a box of this type.” Initialization = Putting a value in the box right away.
Syntax:
|
0 1 2 3 4 5 6 7 |
data_type variable_name; // Declaration only data_type variable_name = value; // Declaration + Initialization |
Rules for Variable Names (Identifiers):
- Start with letter or underscore (_)
- Can contain letters, digits, underscore
- Case-sensitive (Age and age are different)
- Cannot be a keyword (like int, if, for, etc.)
Examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main() { // Declaration only int age; float price; double pi; char grade; // Declaration + Initialization int marks = 85; float salary = 45000.50; double temperature = 98.7654321; char initial = 'A'; // You can change value later age = 25; grade = 'B'; return 0; } |
3. Format Specifiers (How to Print Variables)
When you use printf(), you need to tell it what type of variable you are printing. These are called format specifiers.
| Data Type | Format Specifier | Example in printf |
|---|---|---|
| int | %d or %i | printf(“Age: %d\n”, age); |
| float | %f | printf(“Price: %f\n”, price); |
| double | %lf or %f | printf(“Pi: %.10lf\n”, pi); |
| char | %c | printf(“Grade: %c\n”, grade); |
Real Example – Printing Variables
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> int main() { int age = 25; float height = 5.9; double pi = 3.1415926535; char blood_group = 'O'; printf("My age is %d years.\n", age); printf("My height is %.1f feet.\n", height); // .1f = 1 decimal place printf("Value of pi is %.10lf\n", pi); // Show 10 decimal places printf("My blood group is %c\n", blood_group); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 9 |
My age is 25 years. My height is 5.9 feet. Value of pi is 3.1415926535 My blood group is O |
4. Constants and Literals
Constants = Values that cannot be changed after they are set. There are two main ways to make constants:
A. Using const keyword (modern way – recommended)
|
0 1 2 3 4 5 6 7 |
const float PI = 3.14159; const int MAX_STUDENTS = 100; |
If you try to change it → compiler error!
B. Using #define (old style – preprocessor)
|
0 1 2 3 4 5 6 7 |
#define PI 3.14159 #define MAX_MARKS 100 |
No semicolon at the end!
Literals = The actual values you write directly in code Examples of literals:
- Integer literal: 25, -100
- Floating-point literal: 3.14, 99.99f (f = float)
- Character literal: ‘A’, ‘5’, ‘$’
- String literal: “Hello World”
Example with Constants
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> #define MAX_SCORE 100 // Using #define const float GRAVITY = 9.81; // Using const int main() { int score = 85; printf("Maximum possible score: %d\n", MAX_SCORE); printf("Acceleration due to gravity: %.2f m/s²\n", GRAVITY); // GRAVITY = 10.0; // This line will give ERROR! return 0; } |
5. Keywords and Identifiers
Keywords = Special reserved words in C (you cannot use them as variable names)
There are 32 keywords in standard C (some common ones):
- int, float, double, char
- if, else, for, while, return
- const, void, switch, case, break
- static, extern, typedef, etc.
Identifiers = Names you give to variables, functions, etc.
- Examples: age, total_marks, myName, _count
- Cannot be keywords
Full Practical Example – Putting Everything Together
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* Program: Student Information Purpose: Learn variables, data types, constants, and printing Date: January 21, 2026 */ #include <stdio.h> #define MAX_MARKS 100 // Constant using #define const float PASS_PERCENTAGE = 40.0; // Constant using const int main() { // Variables declaration and initialization int roll_number = 101; char student_name[20] = "Alex"; // We'll learn strings later int maths_marks = 92; int science_marks = 85; float average; // Calculate average average = (maths_marks + science_marks) / 2.0; // Print everything with proper format specifiers printf("Student Details:\n"); printf("-----------------\n"); printf("Roll Number : %d\n", roll_number); printf("Name : %s\n", student_name); // %s for strings printf("Maths Marks : %d / %d\n", maths_marks, MAX_MARKS); printf("Science Marks : %d / %d\n", science_marks, MAX_MARKS); printf("Average Marks : %.2f %%\n", average); printf("Pass Percentage : %.1f %%\n", PASS_PERCENTAGE); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
Student Details: ----------------- Roll Number : 101 Name : Alex Maths Marks : 92 / 100 Science Marks : 85 / 100 Average Marks : 88.50 % Pass Percentage : 40.0 % |
Today’s Homework
- Copy the last example and run it.
- Change values (your own name, marks, roll number) and run again.
- Create a new program that stores:
- Your age (int)
- Your height in cm (float)
- Your favorite letter (char)
- A constant for your city name (using #define or const)
- Print everything nicely using printf.
- Try to use a keyword as variable name (like int int = 10;) – see the error!
