Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

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:

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++

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:

  1. Floating‑point types: float (≈32 bits), double (≈64 bits), and long double (≈96/128 bits depending on implementation).
  2. 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.

C++ Variables and Data Types: Expert Guide to int, double, char, string, bool

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.

C++ Variables and Data Types: Expert Guide to int, double, char, string, bool

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;
}

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:

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:

/* This is a comment */
/* C++ comments can also 
   span multiple lines */

Escape Sequences

Escape sequences represent non‑printable or special characters:

EscapeDescription
\nNewline
\tHorizontal tab
\\Backslash
\bBackspace
\rCarriage return
\vVertical tab
\fFormfeed
\aAlert (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

C Language

  1. Master C# Variables & Primitive Data Types: A Complete Guide
  2. Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
  3. C Variables, Constants, and Literals: A Complete Guide
  4. C# Data Types Explained: Int, Double, Bool, String & More
  5. Java Variables and Data Types – A Comprehensive Guide with Examples
  6. Python Variables: Declaring and Managing String Types
  7. Understanding Java Variable Types: A Comprehensive Guide
  8. C++ Variable Types Explained: Memory, Limits, and Operations
  9. Understanding Variable Scope in C++: Local vs Global Variables
  10. Python Variable Types: Understanding and Using Data Types