Module 2: Variables and Data Types in C
Learning Objectives
By the end of this module, you will be able to:
- Understand variables and constants.
- Learn C keywords and identifiers.
- Use different data types in C.
- Work with integers, floating-point numbers, characters, and Boolean values.
- Perform type conversion.
- Use the
sizeofoperator. - Take user input using
scanf(). - Display output using
printf(). - Build beginner C programs using variables and calculations.
Introduction
Every program works with data. Whether you are building a calculator, game, banking application, or operating system, your program needs to store and process information.
In C, data is stored using variables, and the type of data stored is defined by data types.
For example:
- Student age → Integer
- Product price → Float
- Student grade → Character
- Login status → Boolean
Understanding variables and data types is one of the most important foundations of C programming.
Variables
A variable is a named memory location used to store data.
Think of a variable as a container that holds a value.
Syntax
1data_type variable_name;
Example
1int age;
Here:
intis the data type.ageis the variable name.
Declaring Variables
1int marks; 2float salary; 3char grade;
Variables are declared before they are used.
Initializing Variables
Assigning a value to a variable is called initialization.
1int age = 20; 2float price = 99.99; 3char grade = 'A';
Multiple Variable Declaration
1int x, y, z;
Initialization together:
1int a = 10, b = 20, c = 30;
Variable Naming Rules
A variable name:
- Can contain letters, digits, and underscores.
- Must begin with a letter or underscore.
- Cannot contain spaces.
- Cannot be a keyword.
- Is case-sensitive.
Valid Names
1age 2studentName 3_marks 4salary2025
Invalid Names
12age 2student name 3float 4my-name
Constants
A constant is a value that cannot be changed during program execution.
Using const
1const float PI = 3.14159;
Attempting to change PI results in an error.
Using #define
1#define MAX_STUDENTS 100
The preprocessor replaces every occurrence of MAX_STUDENTS with 100 before compilation.
Difference Between Variables and Constants
| Variables | Constants |
|---|---|
| Can change | Cannot change |
| Stored in memory | Often replaced during preprocessing (#define) or treated as read-only (const) |
| Used for changing data | Used for fixed values |
Keywords
Keywords are reserved words in C with predefined meanings.
You cannot use keywords as variable names.
Common C Keywords
1int 2float 3char 4if 5else 6while 7for 8switch 9case 10return 11void 12break 13continue 14const 15sizeof
Modern C standards define more keywords, but these are the most commonly used by beginners.
Identifiers
Identifiers are names given to:
- Variables
- Functions
- Arrays
- Structures
- Files
Example:
1int studentAge;
Here, studentAge is an identifier.
Identifier Rules
✔ Begin with a letter or underscore
✔ Use letters, numbers, and underscores
✔ Cannot be keywords
✔ Case-sensitive
Example:
1Marks 2marks 3MARKS
All three are different identifiers.
Data Types
A data type tells the compiler:
- How much memory to allocate.
- What kind of data will be stored.
- What operations are allowed.
Categories of Data Types
Basic Data Types
- int
- float
- double
- char
Derived Data Types
- Arrays
- Pointers
- Functions
User-Defined Data Types
- Structure
- Union
- Enum
- typedef
int Data Type
The int data type stores whole numbers.
Examples:
110 2250 3-45 41000
Example:
1int age = 22; 2 3printf("%d", age);
Output
122
Format specifier:
1%d
float Data Type
Stores decimal numbers with single precision.
Example:
1float price = 199.99; 2 3printf("%f", price);
Output
1199.990005
Display only two decimal places:
1printf("%.2f", price);
Output
1199.99
double Data Type
Stores decimal numbers with higher precision than float.
Example:
1double pi = 3.1415926535; 2 3printf("%.10lf", pi);
Output
13.1415926535
Format specifier:
1%lf
char Data Type
Stores a single character.
Example:
1char grade = 'A'; 2 3printf("%c", grade);
Output
1A
Characters are enclosed in single quotes.
Correct:
1'A'
Incorrect:
1"A"
Boolean Data Type
C does not have a built-in bool type in older standards. Since C99, include <stdbool.h>.
Example:
1#include <stdio.h> 2#include <stdbool.h> 3 4int main() 5{ 6 bool isPassed = true; 7 8 printf("%d", isPassed); 9 10 return 0; 11}
Output
11
Values:
1true 2false
Type Conversion
Type conversion means converting one data type into another.
There are two types:
- Implicit Conversion
- Explicit Conversion
Implicit Conversion
The compiler converts automatically.
Example:
1int a = 10; 2float b = a; 3 4printf("%.2f", b);
Output
110.00
Explicit Conversion (Type Casting)
Programmer manually converts data.
Example:
1int a = 5; 2int b = 2; 3 4float result = (float)a / b; 5 6printf("%.2f", result);
Output
12.50
Without casting:
12
sizeof Operator
The sizeof operator returns the memory occupied by a data type or variable in bytes.
Example
1printf("%zu\n", sizeof(int)); 2printf("%zu\n", sizeof(float)); 3printf("%zu\n", sizeof(char));
Typical output on many systems (implementation-dependent):
14 24 31
Input Using scanf()
scanf() reads input from the keyboard.
Syntax
1scanf("format", &variable);
Example
1int age; 2 3scanf("%d", &age);
Notice the & symbol.
It gives the memory address of the variable.
Reading Multiple Values
1int a, b; 2 3scanf("%d %d", &a, &b);
Input
110 20
Reading Float
1float price; 2 3scanf("%f", &price);
Reading Character
1char grade; 2 3scanf(" %c", &grade);
The leading space helps skip leftover whitespace.
Output Using printf()
printf() displays output on the screen.
Example
1printf("Welcome to C Programming");
Printing Variables
1int age = 22; 2 3printf("Age = %d", age);
Multiple Variables
1char name[] = "John"; 2int age = 20; 3 4printf("Name: %s\nAge: %d", name, age);
Common Format Specifiers
| Data Type | Format Specifier |
|---|---|
| int | %d |
| float | %f |
| double | %lf |
| char | %c |
| string | %s |
Practice Project 1: Temperature Converter
Convert Celsius to Fahrenheit.
1#include <stdio.h> 2 3int main() 4{ 5 float celsius, fahrenheit; 6 7 printf("Enter temperature in Celsius: "); 8 scanf("%f", &celsius); 9 10 fahrenheit = (celsius * 9 / 5) + 32; 11 12 printf("Temperature in Fahrenheit = %.2f\n", fahrenheit); 13 14 return 0; 15}
Practice Project 2: Area Calculator
Calculate the area of a rectangle.
1#include <stdio.h> 2 3int main() 4{ 5 float length, width, area; 6 7 printf("Enter length: "); 8 scanf("%f", &length); 9 10 printf("Enter width: "); 11 scanf("%f", &width); 12 13 area = length * width; 14 15 printf("Area = %.2f\n", area); 16 17 return 0; 18}
Practice Project 3: Simple Interest Calculator
Calculate simple interest.
Formula:
1Simple Interest = (Principal × Rate × Time) / 100
1#include <stdio.h> 2 3int main() 4{ 5 float principal, rate, time, interest; 6 7 printf("Enter Principal: "); 8 scanf("%f", &principal); 9 10 printf("Enter Rate: "); 11 scanf("%f", &rate); 12 13 printf("Enter Time (Years): "); 14 scanf("%f", &time); 15 16 interest = (principal * rate * time) / 100; 17 18 printf("Simple Interest = %.2f\n", interest); 19 20 return 0; 21}
Best Practices
- Use meaningful variable names such as
studentAgeinstead ofa. - Initialize variables before using them.
- Use
constfor values that should not change. - Choose the appropriate data type to save memory and improve performance.
- Use the correct format specifier with
scanf()andprintf(). - Prefer
doubleoverfloatwhen higher precision is required.
Module Summary
In this module, you learned:
- What variables are and how to declare and initialize them.
- The difference between variables and constants.
- Rules for keywords and identifiers.
- The basic C data types:
int,float,double,char, andbool. - How implicit and explicit type conversion works.
- How to use the
sizeofoperator to determine memory usage. - How to read user input with
scanf()and display output withprintf(). - How to apply these concepts through practical programs such as a temperature converter, area calculator, and simple interest calculator.
With these fundamentals, you're ready to move on to the next module, where you'll learn operators, expressions, and decision-making in C.