C & C++ Programming
Computer ScienceChapter 2 — Variables, Data Types and Operators
Lesson 2.1 : Variables in C and C++
Reading & Interactive Lesson
Variables in C and C++
Reading Material & Notes
Variables in C and C++
A variable is a named memory location used to store data during program execution. Variables allow programs to store values that can change while the program is running.
Variable Declaration
In C and C++, a variable is declared by specifying its data type followed by its name.
int age;
float salary;
char grade;
Variable Initialization
A variable can be assigned an initial value when it is declared.
int age = 20;
float price = 99.50;
Rules for Naming Variables
- A variable name can contain letters, digits and underscore.
- It cannot begin with a digit.
- Keywords cannot be used as variable names.
- Variable names are case-sensitive.
Example
#include <stdio.h>
int main()
{
int age = 20;
printf("Age = %d", age);
return 0;
}
Key Points
- Variables store program data.
- Every variable has a data type.
- Variables should have meaningful names.
Course Curriculum
10 Chapters • 50 Lessons
Chapter 1 — Introduction to C and C++
5 Lessons
Chapter 2 — Variables, Data Types and Operators
5 Lessons
Chapter 3 — Conditional Statements and Loops
5 Lessons
Chapter 4 — Functions and Modular Programming
5 Lessons
Chapter 5 — Arrays and Strings
5 Lessons
Chapter 6 — Pointers and References
5 Lessons
Chapter 7 — Structures, Unions and Enumerations
5 Lessons
Chapter 8 — Object-Oriented Programming in C++
5 Lessons
Chapter 9 — Inheritance, Polymorphism and Templates
5 Lessons
Chapter 10 — File Handling and Mini Projects
5 Lessons