C++ Variables and Data Types: Expert Guide to int, double, char, string, bool
Variables in C++
In C++, a variable is a named storage location that lets a programmer hold and manipulate data. Every variable has an explicit type that determines its memory footprint, allowable value range, and the operations that can be performed on it.
This tutorial covers:
- Variables in C++
- Basic variable types
- Declaration rules
- Data type hierarchy
- Identifiers and naming conventions
- Const qualifier
- Scope rules
- Implicit and explicit type conversion
- Register variables
- Comments and escape sequences
Basic Types of Variables in C++
int – Whole numbers without fractional or exponential parts (e.g., 120, -90).
double – Double‑precision floating‑point numbers (e.g., 11.22, 2.345).
char – Single character literals enclosed in single quotes (e.g., 'a', 'm', 'F', 'P', '}').
float – Single‑precision floating‑point values (e.g., 1.3, 2.6).
string – Sequence of characters in double quotes (e.g., "How are you?").
bool – Holds either true or false.
Rules of Declaring Variables in C++
- Names may contain letters, digits, and underscores.
- They cannot start with a digit.
- Conventional practice favors lowercase; uppercase starts usually denote constants.
- Names must not collide with language keywords (e.g.,
int). - Starting with an underscore is allowed but discouraged due to reserved identifier rules.
C++ Variable Data Types
C++ offers a rich set of primitive types. The void type has no value and is typically used as a function’s return type when nothing is returned.
Arithmetic types split into two categories:
- Floating‑point types:
float(≈32 bits),double(≈64 bits), andlong double(≈96/128 bits depending on implementation). - Integral types: integers, characters, and
bool. Integral types can be signed or unsigned.
Signed types represent negative, zero, and positive values. For example, an 8‑bit signed char ranges from –127 to 127.
Unsigned types hold only non‑negative values. An 8‑bit unsigned char ranges from 0 to 255.

Variable Name or Identifiers
Identifiers can contain letters, digits, and underscores. Length is unlimited, and they are case‑sensitive:
int guru99, gurU99, GuRu99, GURU99;
Reserved keywords cannot be reused as identifiers. Adopting clear naming conventions improves readability: use lowercase for variables, camelCase or snake_case for multi‑word names, and capitalize class names.

Declaration and Definition Examples
int a = 5;
int b;
char c = 'A';
int a, b;
a = b = 1000;
int a(5);
int b{5};
Const Qualifier in C++
Use const when a value must remain unchanged after initialization:
const int bufSize = 512; // input buffer size
A const variable must be initialized; otherwise the compiler emits an error.
const int i = get_size(); // OK: runtime init const int j = 42; // OK: compile‑time init const int k; // error: uninitialized const int i = 42; const int ci = i; // OK: copy of i
Scope of Variables in C++
Scope defines where a name is visible. Variables are accessible from their point of declaration to the end of the enclosing block.
#include <iostream>
int main()
{
int sum = 0;
for (int val = 1; val <= 10; ++val)
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
return 0;
}
mainhas global scope.sumhas block scope withinmain.valhas local scope within theforloop.
Nested Scope
#include <iostream>
using namespace std;
int reused = 42; // global scope
int main()
{
int unique = 0; // block scope
cout << reused << " " << unique << endl; // 42 0
int reused = 0; // local reuse hides global
cout << reused << " " << unique << endl; // 0 0
cout << ::reused << " " << unique << endl; // 42 0
return 0;
}
Variable Type Conversion
C++ performs implicit conversions under certain conditions. Key rules include:
- Non‑bool to
bool: 0 becomesfalse; any other value becomestrue.bool b = 42; // b is true
boolto arithmetic:truebecomes 1,falsebecomes 0.bool b = true; int i = b; // i is 1
- Floating‑point to integer: truncates toward zero.
int i = 3.14; // i is 3
- Integer to floating‑point: fractional part is lost.
int i = 3; double pi = i; // pi is 3.0
- Unsigned overflow: value wraps modulo base.
unsigned char c = -1; // 255 on 8‑bit chars
- Signed overflow: undefined behavior.
When mixing signed and unsigned integers, negative signed values may unexpectedly convert to large unsigned values.
Register Variables
Use the register keyword to suggest that a frequently accessed variable be stored in a CPU register. Modern compilers typically ignore the hint, performing their own optimizations.
register int i;
Comments
Comments are ignored by the compiler and serve to document code:
- Single‑line – starts with
//. - Multi‑line – delimited by
/* ... */.
/* This is a comment */ /* C++ comments can also span multiple lines */
Escape Sequences
Escape sequences represent non‑printable or special characters:
| Escape | Description |
|---|---|
| \n | Newline |
| \t | Horizontal tab |
| \\ | Backslash |
| \b | Backspace |
| \r | Carriage return |
| \v | Vertical tab |
| \f | Formfeed |
| \a | Alert (bell) |
| \" | Double quote |
| \' | Single quote |
| \? | Question mark |
cout << '\n'; // newline cout << "\tguru99!\n"; // tab + text + newline
Generalized sequences use hexadecimal or octal values:
\7 // bell \12 // newline \40 // space \x4D // 'M'
Summary
- Variables provide named storage and are typed.
- Primary types: int, double, char, float, string, bool.
- Scope defines visibility; nested scopes can shadow names.
- Implicit and explicit conversions exist but must be used carefully.
- Register hints may improve performance but are advisory.
- Comments and escape sequences aid readability and correct output.
C Language
- Master C# Variables & Primitive Data Types: A Complete Guide
- Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
- C Variables, Constants, and Literals: A Complete Guide
- C# Data Types Explained: Int, Double, Bool, String & More
- Java Variables and Data Types – A Comprehensive Guide with Examples
- Python Variables: Declaring and Managing String Types
- Understanding Java Variable Types: A Comprehensive Guide
- C++ Variable Types Explained: Memory, Limits, and Operations
- Understanding Variable Scope in C++: Local vs Global Variables
- Python Variable Types: Understanding and Using Data Types