Variables and Data Types in C++
⏱ Estimated reading time: 2 min
A variable is a named memory location used to store data whose value can change during program execution.
Syntax
Example
Rules for Naming Variables
-
Must begin with a letter or underscore (
_) -
Cannot start with a number
-
No spaces or special symbols
-
Keywords cannot be used as variable names
-
C++ is case-sensitive (
Ageandageare different)
Data Types in C++
Data types specify the type and size of data a variable can store.
1. Basic (Primitive) Data Types
| Data Type | Description | Example |
|---|---|---|
| int | Stores integers | int a = 5; |
| float | Stores decimal values | float b = 3.14; |
| double | Stores large decimal values | double c = 3.14159; |
| char | Stores a single character | char d = 'A'; |
| bool | Stores true or false | bool e = true; |
| void | No value | Used in functions |
2. Derived Data Types
Derived from basic data types.
-
Array
-
Pointer
-
Function
-
Reference
Example:
3. User-Defined Data Types
Created by the programmer.
-
struct -
union -
enum -
typedef -
class
Example:
4. Modifiers
Used to modify basic data types.
-
short -
long -
signed -
unsigned
Example:
Variable Initialization
Constant Variables
Value cannot be changed.
Key Points
-
Variables store data values
-
Data types define memory size and operations
-
Correct data type selection improves efficiency
-
Constants protect fixed values
Conclusion
Variables and data types are fundamental concepts in C++ that help store, manage, and manipulate data efficiently in a program.
Register Now
Share this Post
← Back to Tutorials