C++ Variables
In C++, variables are used to store data that can be manipulated throughout the program. Each variable is associated with a data type that determines the kind of values it can store and how much memory is allocated for that variable.
Declaring Variables
In C++, you declare a variable by specifying its data type followed by the variable name.
Syntax:
cppdata_type variable_name;
Example:
cppint age; // Declares an integer variable called age
Variables can also be initialized (assigned a value) when they are declared:
cppint age = 25; // Declares and initializes age with a value of 25
Basic Data Types
-
Integer Types:
- Stores whole numbers (both positive and negative).
- Example:
int
,short
,long
,long long
cppint number = 10; // Integer variable long bigNumber = 1000; // Long integer variable
-
Floating-Point Types:
- Used to store decimal numbers.
- Example:
float
,double
cppfloat temperature = 36.5f; // Floating-point variable double pi = 3.14159; // Double-precision floating-point variable
-
Character Type:
- Used to store a single character.
- Example:
char
cppchar grade = 'A'; // Character variable
-
Boolean Type:
- Stores
true
orfalse
. - Example:
bool
cppbool isAdult = true; // Boolean variable
- Stores
-
String Type:
- Stores a sequence of characters. Although not a built-in type, strings are commonly used via the
std::string
class.
cppstd::string name = "John"; // String variable
- Stores a sequence of characters. Although not a built-in type, strings are commonly used via the
Variable Naming Rules
-
Names must begin with a letter or an underscore.
- Example:
int age;
,int _number;
- Example:
-
Subsequent characters can be letters, digits, or underscores.
- Example:
int count_123;
- Example:
-
Case-sensitive:
- Example:
int Age;
andint age;
are two different variables.
- Example:
-
Cannot use reserved keywords (such as
int
,class
,return
).
Variable Scope
-
Local Variables: Declared inside a function or block. Only accessible within that function or block.
cppvoid example() { int x = 10; // Local variable std::cout << x; }
-
Global Variables: Declared outside of all functions. Accessible by any function within the program.
cppint x = 10; // Global variable void example() { std::cout << x; // Accessing the global variable }
Variable Initialization
Variables can be initialized in multiple ways:
-
Direct Initialization:
cppint x = 5;
-
Brace Initialization (C++11):
cppint x{5};
-
Copy Initialization:
cppint x(5);
Constant Variables
Use the const
keyword to declare constant variables, whose values cannot be changed after
initialization.
Example:
cppconst float PI = 3.14159; // Constant variable
PI = 3.14; // Error: Cannot modify a constant variable
Example Code Using Variables
cpp#include
#include
int main() {
int age = 25; // Integer variable
float height = 5.9f; // Floating-point variable
char grade = 'A'; // Character variable
bool isStudent = true; // Boolean variable
std::string name = "John"; // String variable
// Output the values of the variables
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << std::endl;
std::cout << "Grade: " << grade << std::endl;
std::cout << "Is Student: " << isStudent << std::endl;
return 0;
}
Output:
vbnetName: John
Age: 25
Height: 5.9
Grade: A
Is Student: 1
Summary
- Variables store data that can be used and modified in the program.
- They have a data type (like
int
,float
,char
,bool
) which defines the kind of data they hold. - Variables can be local (within a function) or global (outside all functions).
- Use the
const
keyword to declare variables that should not change during program execution. - Proper variable naming and initialization are crucial for readability and avoiding bugs.