Fundamentals 7 min read

Master C++ Variables: Types, Declarations, Naming Rules & Conversions

This guide explains what variables are in C++, covers declaration and initialization, details fundamental data types such as int, float, double, char, and bool, outlines naming conventions, and demonstrates both implicit and explicit type conversions with clear code examples.

php Courses
php Courses
php Courses
Master C++ Variables: Types, Declarations, Naming Rules & Conversions

1. What is a variable?

In C++, a variable is a container for storing data. Each variable has a name (identifier) and a data type that determines what values it can hold and how much memory it occupies.

Variable declaration and initialization

int age = 25; // Declare and initialize an integer variable
float price = 19.99f; // Declare and initialize a single‑precision floating‑point number
char grade = 'A'; // Declare and initialize a character variable
bool isActive = true; // Declare and initialize a boolean variable

Declaration : tells the compiler the variable’s name and type.

Initialization : assigns an initial value to the variable.

2. C++ basic data types

C++ provides several data types, mainly divided into the following categories:

(1) Integer (int)

Used to store whole numbers (no fractional part).

int num = 42; // standard int (usually 4 bytes)
short smallNum = 10; // short int (usually 2 bytes)
long bigNum = 100000L; // long int (4 or 8 bytes)
long long hugeNum = 1'000'000'000L; // long long (usually 8 bytes)
int

range is typically –2,147,483,648 to 2,147,483,647 on a 32‑bit system.

(2) Floating‑point (float, double)

Used to store numbers with a fractional part.

float pi = 3.14f; // single‑precision (≈6‑7 significant digits)
double precisePi = 3.1415926535; // double‑precision (≈15‑16 significant digits)
float

occupies 4 bytes, double occupies 8 bytes.

By default, a literal like 3.14 is a double; adding the suffix f makes it a float.

(3) Character (char)

Used to store a single character (letter, digit, symbol).

char letter = 'A'; // store a character
char symbol = '$'; // store a symbol
char newline = '
'; // escape character for newline
char

occupies 1 byte (8 bits) and can represent ASCII characters (0‑127).

Use single quotes for characters and double quotes for strings, e.g., "Hello".

(4) Boolean (bool)

Used to store logical values true or false.

bool isCppFun = true; // true (1)
bool isHard = false; // false (0)
bool

occupies 1 byte but logically needs only 1 bit.

Commonly used in conditional statements, e.g., if (isCppFun) { … }.

3. Variable naming rules

Names may contain letters, digits, and underscores, but cannot start with a digit.

Names are case‑sensitive (e.g., age vs Age).

Keywords such as int, return, class cannot be used as identifiers.

Recommended styles: camelCase (e.g., userAge) or snake_case (e.g., user_age).

Examples

Valid identifiers:

int userAge;
float accountBalance;
bool isLoggedIn;

Invalid identifiers:

int 123age; // cannot start with a digit
float return; // cannot use a keyword
char first name; // cannot contain spaces

4. Type conversion (Type Conversion)

C++ allows conversion between different data types.

(1) Implicit conversion (automatic)

int num = 10;
double decimal = num; // int → double (automatic)

Converting a smaller type to a larger type (e.g., intdouble) is usually safe.

(2) Explicit conversion (cast)

double pi = 3.14159;
int approxPi = (int)pi; // double → int (explicit cast, fractional part lost)

Use (type)variable or C++‑style static_cast<type>(variable) for explicit casts.

5. Summary

int

: stores integers, typically 4 bytes. float: stores single‑precision floating‑point numbers, 4 bytes. double: stores double‑precision floating‑point numbers, 8 bytes. char: stores a single character, 1 byte. bool: stores logical values, 1 byte.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data Typesnaming conventionsprogramming fundamentalsC++Variablestype conversion
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.