A C++ program often needs to store information while it is running. For example, we may need to store a student’s name, age, marks, or a product price.
In C++, we use variables to store this information, while data types tell C++ what kind of information a variable can store. Let’s begin with C++ variables and data types.
What is a Variable?
A variable is a named place used to store a value.
Think of a variable like a box with a name on it. You can put a value inside the box and use it later.
For example:
int age = 14;
Here:
intis the data type.ageis the variable name.14is the value stored in the variable.
Creating a Variable
The basic syntax is:
dataType variableName = value;
For example:
int marks = 85;
You can display the value using cout:
#include <iostream>
using namespace std;
int main() {
int marks = 85;
cout << "Marks: " << marks;
return 0;
}
Output:
Marks: 85
Changing a Variable’s Value
The value of a variable can be changed after it is created.
#include <iostream>
using namespace std;
int main() {
int age = 14;
cout << "Age: " << age << "\n";
age = 15;
cout << "New Age: " << age;
return 0;
}
Output:
Age: 14
New Age: 15
What is a Data Type?
A data type tells C++ what kind of value a variable will store.
Some commonly used C++ data types are:
| Data Type | Stores | Example |
|---|---|---|
int | Whole numbers | 25 |
float | Decimal numbers | 5.5 |
double | Decimal numbers | 99.99 |
char | One character | 'A' |
bool | True or false | true |
string | Text | "Hello" |
Let’s see some examples.
int
int is used for whole numbers.
int age = 14;
int marks = 90;
float
float is used for decimal numbers.
float height = 5.7;
double
double is also used for decimal numbers and generally provides more precision than float.
double price = 199.99;
char
char stores a single character. The character is written inside single quotes.
char grade = 'A';
bool
bool stores either true or false.
bool passed = true;
string
string is used to store text.
string name = "Rahul";
When using string, include the <string> library:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Rahul";
cout << "Name: " << name;
return 0;
}
Output:
Name: Rahul
Using Different Data Types Together
A program can use different data types at the same time.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Rahul";
int age = 14;
double marks = 88.5;
char grade = 'A';
bool passed = true;
cout << "Name: " << name << "\n";
cout << "Age: " << age << "\n";
cout << "Marks: " << marks << "\n";
cout << "Grade: " << grade << "\n";
cout << "Passed: " << boolalpha << passed;
return 0;
}
Output:
Name: Rahul
Age: 14
Marks: 88.5
Grade: A
Passed: true
This is how variables and data types work together to store different kinds of information.
Rules for Naming Variables
C++ has a few simple rules for variable names:
- A variable name can contain letters, numbers, and
_. - It cannot start with a number.
- Spaces are not allowed.
- C++ keywords such as
intandreturncannot be used as variable names. - C++ is case-sensitive.
For example:
int studentAge = 14;
int student_age = 14;
Both are valid variable names.
But this is not valid:
int 2age = 14;
A Simple Example
Let’s create a small program that stores a student’s information.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Anjali";
int age = 14;
double marks = 91.5;
cout << "Name: " << name << "\n";
cout << "Age: " << age << "\n";
cout << "Marks: " << marks;
return 0;
}
Output:
Name: Anjali
Age: 14
Marks: 91.5
Key Points to Remember
- A variable stores information in a program.
- A data type tells C++ what kind of value a variable stores.
intis used for whole numbers.floatanddoubleare used for decimal numbers.charstores one character.boolstorestrueorfalse.stringstores text.- Variable names should be clear and meaningful.
What’s Next?
Now that you know how to store information, the next chapter will show you how to take input from the user and display output in C++ using cin and cout.
Frequently Asked Questions (FAQs)
Q1. What are C++ Variables and Data Types?
C++ Variables and Data Types work together to store information in a program. A variable stores the value, while the data type defines what kind of value it can hold.
Q2. What are the main C++ data types?
Common C++ data types include int for whole numbers, float and double for decimals, char for characters, bool for true or false values, and string for text.
Q3. How do you declare C++ variables?
C++ variable declaration uses a data type followed by a variable name. For example, int age = 14; declares and initializes an integer variable.
Q4. What is the difference between int, float, and double in C++?
int stores whole numbers, while float and double store decimal numbers. double generally provides greater precision than float.
Q5. Why should you use meaningful C++ variable names?
Meaningful C++ variable names make your code easier to read and understand. Names such as studentAge and totalMarks clearly describe the stored values.
Q6. Can one C++ program use different data types?
Yes. A program can use multiple C++ data types at the same time. For example, a student’s name can use string, age can use int, and marks can use double.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
