Variables and Data Types in C++

📘 C++ 👁 34 views 📅 Dec 22, 2025
⏱ Estimated reading time: 2 min

A variable is a named memory location used to store data whose value can change during program execution.

Syntax

data_type variable_name;

Example

int number; number = 10;

Rules for Naming Variables

  • Must begin with a letter or underscore (_)

  • Cannot start with a number

  • No spaces or special symbols

  • Keywords cannot be used as variable names

  • C++ is case-sensitive (Age and age are different)


Data Types in C++

Data types specify the type and size of data a variable can store.


1. Basic (Primitive) Data Types

Data TypeDescriptionExample
intStores integersint a = 5;
floatStores decimal valuesfloat b = 3.14;
doubleStores large decimal valuesdouble c = 3.14159;
charStores a single characterchar d = 'A';
boolStores true or falsebool e = true;
voidNo valueUsed in functions

2. Derived Data Types

Derived from basic data types.

  • Array

  • Pointer

  • Function

  • Reference

Example:

int arr[5]; int *ptr;

3. User-Defined Data Types

Created by the programmer.

  • struct

  • union

  • enum

  • typedef

  • class

Example:

struct Student { int roll; float marks; };

4. Modifiers

Used to modify basic data types.

  • short

  • long

  • signed

  • unsigned

Example:

unsigned int age = 20; long int distance;

Variable Initialization

int x = 10; // Initialization float y = 5.5; char grade = 'A';

Constant Variables

Value cannot be changed.

const float PI = 3.14;

Key Points

  • Variables store data values

  • Data types define memory size and operations

  • Correct data type selection improves efficiency

  • Constants protect fixed values


Conclusion

Variables and data types are fundamental concepts in C++ that help store, manage, and manipulate data efficiently in a program.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes