Nearby lessons

19 of 124

C - Data Types

Data Types is one of the foundational topics in C programming. This lesson explains The Basic Data Types — Each One in Its Own Program, Program 1: int (whole numbers) and Program 2: float (decimal numbers) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The Basic Data Types — Each One in Its Own Program

A data type tells C what kind of value a variable will store. Let us see each of the four basic types running on its own.

Data typeStoresSize (approx)Example
intWhole numbers (integers)2 or 4 bytesint age = 20;
floatNumbers with decimal points4 bytesfloat pi = 3.14;
doubleDecimal numbers with more precision8 bytesdouble price = 99.99;
charOne single character1 bytechar grade = 'A';
In simple words: the data type tells the machine how to treat your value — int for whole numbers, float/double for decimals, char for one single letter.

Program 1: int (whole numbers)

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int age = 20;
6 int year = 2026;
7 
8 printf("Age : %d\n", age);
9 printf("Year : %d\n", year);
10 printf("Sum : %d\n", age + year); // 2046
11}
Output

Age  : 20
Year : 2026
Sum  : 2046
      

Program 2: float (decimal numbers)

Example03
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 float marks = 87.5;
6 float percent = marks / 100;
7 
8 printf("Marks : %.2f\n", marks);
9 printf("Percent : %.2f\n", percent);
10}
Output

Marks     : 87.50
Percent   : 0.88
      

Program 3: double (more precision)

Example04
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 double price = 99.991234567;
6 double total = price * 2;
7 
8 printf("Price : %.6lf\n", price);
9 printf("Total : %.6lf\n", total);
10}
Output

Price : 99.991235
Total : 199.982469
      

Program 4: char (one character)

Trainer's Note: Size trick: char is always 1 byte (holds one character). int is the standard whole number. Use float for simple decimals and double when you need very accurate decimals (like scientific calculations).
Example05
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 char grade = 'A';
6 char symbol = '5';
7 
8 printf("Grade : %c\n", grade);
9 printf("Symbol : %c\n", symbol);
10}
Output

Grade  : A
Symbol : 5
      

Declaration and Initialization

Declaration tells C the type and name. Initialization gives a value at the time of declaration.

Example06
CCode Cell
1int a, b, c; // declare three integers
2int total = 0; // declare and initialize
3float salary = 5000.50;
4char ch = 'A';

Program: declaration styles in action

Example07
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a, b, c; // declaration only
6 int total = 0; // declaration + initialization
7 float salary = 5000.50;
8 char ch = 'A';
9 
10 a = 10; b = 20; c = 30; // assign later
11 total = a + b + c;
12 
13 printf("Total : %d\n", total);
14 printf("Salary : %.2f\n", salary);
15 printf("Char : %c\n", ch);
16}
Output

Total  : 60
Salary : 5000.50
Char   : A
      
📝 Key Takeaways
  • Tokens = keywords, identifiers, constants, operators, special symbols.
  • Constants never change; variables can hold different values.
  • Identifier rules: start with letter/underscore, no spaces/symbols, no keywords.
  • Basic data types: int, float, double, char — each with its own example program.
  • Declaration = type + name; initialization = giving a value.
  • sizeof tells the memory size of a type.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2