C++ Data Types and Variables | Built-in Types, Constants, sizeof and Type Casting


This complete tutorial on C++ Data Types and Variables explains how data is stored and managed in C++ programs. It covers built-in data types, type modifiers, the sizeof operator, constants using const and constexpr, and both implicit and explicit type casting. The tutorial follows best coding practices and helps beginners write safe, efficient, and readable C++ programs.

Data Types and Variables – Complete Tutorial

1. Variables in C++

A variable is a named memory location used to store data. The type of variable determines what kind of data it can hold.

Syntax:


data_type variable_name;

Example:


int age = 25;
double salary = 45000.50;
char grade = 'A';

Best Practices:

  1. Use meaningful variable names
  2. Initialize variables at the time of declaration
  3. Avoid unnecessary global variables

2. Built-in Data Types

C++ provides several built-in data types.

Fundamental Data Types:

Data TypeDescriptionExample
intInteger numbersint x = 10;
floatDecimal numbersfloat pi = 3.14;
doubleDouble precision decimaldouble d = 3.14159;
charSingle characterchar c = 'A';
boolTrue or falsebool flag = true;
voidNo valueUsed in functions

3. Type Modifiers

Type modifiers change the size or range of built-in data types.

Common Type Modifiers:

  1. short
  2. long
  3. signed
  4. unsigned

Example:


short int s = 10;
long int l = 100000;
unsigned int u = 50;

Note:

  1. unsigned types store only positive values
  2. Size of data types may vary by system and compiler

4. sizeof Operator

The sizeof operator is used to find the size (in bytes) of a data type or variable.

Example:


cout << sizeof(int);
cout << sizeof(double);

Using with variables:


int x;
cout << sizeof(x);

Best Practices:

  1. Use sizeof instead of assuming data type sizes
  2. Useful for memory optimization and portability

5. Constants in C++

Constants are variables whose values cannot be changed after initialization.

Using const


const int maxUsers = 100;

Using constexpr


constexpr int size = 10;

Difference Between const and constexpr:

Featureconstconstexpr
EvaluatedRuntimeCompile time
Use caseFixed valuesCompile-time constants
PerformanceNormalOptimized

Best Practices:

  1. Prefer constexpr for compile-time values
  2. Use const to protect variables from modification

6. Type Casting

Type casting is the process of converting one data type into another.

6.1 Implicit Type Casting

Done automatically by the compiler.

Example:


int x = 10;
double y = x; // int to double

6.2 Explicit Type Casting

Done manually by the programmer.

Example:


double x = 10.75;
int y = (int)x;

Modern C++ style:


int y = static_cast<int>(x);

Best Practices:

  1. Avoid unnecessary type casting
  2. Prefer static_cast over C-style casting
  3. Be careful of data loss during casting

Common Mistakes to Avoid

  1. Using uninitialized variables
  2. Ignoring data type limits
  3. Mixing signed and unsigned types
  4. Overusing type casting

Summary

In this chapter, you learned about C++ data types and variables, built-in data types, type modifiers, the sizeof operator, constants, and type casting. Understanding these concepts is essential for writing correct and efficient C++ programs.