Learn about variables, data types, naming rules, type modifiers, and type conversion in C++ programming.
int age = 25;
declares a variable named age
of type int
(integer) and assigns it the value 25
.cout
prints the stored value to the screen._
).int
, float
, return
, etc.).Age
and age
are different variables).Valid | Invalid |
---|---|
userName | 1stName (starts with a digit) |
_totalSum | float (keyword) |
score_100 | user-name (contains - ) |
int
)-2,147,483,648
to 2,147,483,647
(on 32-bit systems).float
, double
)float
(4 bytes, up to 7 decimal places).double
(8 bytes, up to 15 decimal places).char
)'A'
= 65 in ASCII).bool
)0
(false) or 1
(true).string
)<string>
header).
char
for handling text.wchar_t
)#include <cwchar>
and wcout
.Modifier | Example | Meaning |
---|---|---|
signed | signed int x; | Default, stores both positive & negative numbers |
unsigned | unsigned int x; | Stores only positive numbers, doubles max range |
short | short int x; | Uses less memory (2 bytes) |
long | long int x; | Stores larger numbers (8 bytes) |
unsigned int
ensures only non-negative numbers.totalMarks
instead of x
).
✔️ Choose the correct data type to save memory.
✔️ Initialize variables before use.
✔️ Prefer const
for constant values (const double PI = 3.1416;
).
✔️ Avoid unnecessary type conversions.
int
, float
, char
, bool
, string
).unsigned
, long
, etc.).