🟢 Foundation  ·  Lesson 07

Variables and Data Types in C++

What is a variable?

A variable is a named box in memory that holds a value. The name lets you read or change that value later. In C++ every variable has a type, which decides what kind of value it can store and how much memory it uses.

Declaring and initialising

To declare a variable you write its type, then its name. You can give it a starting value at the same time — this is called initialising.

C++
int age = 20;          // declare and initialise
double price = 9.99;
char grade = 'A';
bool isPassed = true;

age = 21;              // change the value later
💡 Always initialise

A variable declared but not given a value holds an unpredictable "garbage" value until you assign one. Initialise as you declare when you can.

Built-in data types

TypeHoldsExample
intWhole numbers42, -7
floatDecimal numbers (less precise)3.14f
doubleDecimal numbers (more precise)3.14159
charA single character'A'
booltrue or falsetrue

Sizes and ranges

Each type uses a fixed amount of memory. You can check it with sizeof:

C++
#include <iostream>
using namespace std;
int main() {
    cout << "int: "    << sizeof(int)    << " bytes\n";
    cout << "double: " << sizeof(double) << " bytes\n";
    cout << "char: "   << sizeof(char)   << " bytes\n";
    return 0;
}
Output (typical):
int: 4 bytes
double: 8 bytes
char: 1 bytes

Sizes can vary by system, but int is usually 4 bytes, double 8, and char always 1.

A working example

C++
#include <iostream>
using namespace std;
int main() {
    string name = "Amit";
    int age = 20;
    double marks = 88.5;
    cout << name << " is " << age << " with " << marks << "%\n";
    return 0;
}
Output:
Amit is 20 with 88.5%

Note the string type for text — it comes from the standard library and is far easier than C-style character arrays.

The auto keyword

C++ can figure out a type for you with auto:

C++
auto count = 10;       // int
auto pi = 3.14159;     // double
auto letter = 'Z';     // char

Use auto to avoid repeating long type names; the value on the right decides the type.

Common mistakes

  • Using a variable before giving it a value (garbage result).
  • Storing a decimal in an int — the fractional part is dropped.
  • Putting text in single quotes; 'A' is a char, "A" is a string.
  • Starting a name with a digit or using a keyword as a name.
🏋️ Practice

Declare an int, a double, a char and a bool, give each a value, and print them all on one line with labels. Then print the sizeof each type.

Summary

  • A variable is a named, typed box in memory holding a value.
  • Declare with type name = value; and initialise as you declare.
  • Core types: int, float, double, char, bool; string for text.
  • sizeof shows how many bytes a type uses.
  • auto lets the compiler deduce the type from the value.

Frequently Asked Questions

What is a variable in C++?
A variable is a named storage location that holds a value your program can use and change. You declare it with a type and a name, such as int age = 20;, which reserves memory for an integer and stores 20 in it.
What are the basic data types in C++?
The fundamental built-in types are int for whole numbers, float and double for decimals, char for single characters, and bool for true/false values. Modifiers like short, long, signed and unsigned adjust their size and range.
What is the difference between float and double in C++?
Both store decimal numbers, but double uses more memory (usually 8 bytes) and holds about twice the precision of float (usually 4 bytes). Use double when you need more accurate decimals, which is why it is the common default in C++.
What is the bool data type in C++?
The bool type holds one of two values, true or false. It is used for conditions and logic, and internally true is stored as 1 and false as 0. C++ has bool as a real built-in type, unlike older C.
What does the auto keyword do in C++?
auto lets the compiler deduce a variable's type from the value you assign, so auto x = 5; makes x an int and auto pi = 3.14; makes it a double. It reduces repetition and is handy with long type names, though the value must be given at declaration.

Variable क्या है?

Variable memory में एक नामित डिब्बा है जो एक value रखता है। नाम आपको बाद में वह value पढ़ने या बदलने देता है। C++ में हर variable का एक type होता है, जो तय करता है कि वह किस तरह की value रख सकता है और कितनी memory इस्तेमाल करता है।

Declare और initialise करना

Variable declare करने को आप उसका type, फिर नाम लिखते हैं। आप साथ ही उसे शुरुआती value दे सकते हैं — इसे initialise करना कहते हैं।

C++
int age = 20;          // declare aur initialise
double price = 9.99;
char grade = 'A';
bool isPassed = true;

age = 21;              // baad mein value badlein
💡 हमेशा initialise करें

Declare किया पर value न दिया variable तब तक अप्रत्याशित "garbage" value रखता है जब तक आप assign न करें। हो सके तो declare करते ही initialise करें।

Built-in data types

Typeक्या रखता हैउदाहरण
intपूर्ण संख्याएँ42, -7
floatदशमलव संख्याएँ (कम सटीक)3.14f
doubleदशमलव संख्याएँ (ज़्यादा सटीक)3.14159
charएक एकल character'A'
booltrue या falsetrue

Sizes और ranges

हर type एक निश्चित मात्रा में memory इस्तेमाल करता है। आप इसे sizeof से जाँच सकते हैं:

C++
#include <iostream>
using namespace std;
int main() {
    cout << "int: "    << sizeof(int)    << " bytes\n";
    cout << "double: " << sizeof(double) << " bytes\n";
    cout << "char: "   << sizeof(char)   << " bytes\n";
    return 0;
}
Output (सामान्य):
int: 4 bytes
double: 8 bytes
char: 1 bytes

Sizes system के अनुसार बदल सकते हैं, पर int आमतौर पर 4 bytes, double 8, और char हमेशा 1।

एक चलता उदाहरण

C++
#include <iostream>
using namespace std;
int main() {
    string name = "Amit";
    int age = 20;
    double marks = 88.5;
    cout << name << " is " << age << " with " << marks << "%\n";
    return 0;
}
Output:
Amit is 20 with 88.5%

Text के लिए string type पर ध्यान दें — यह standard library से आता है और C-शैली के character arrays से कहीं आसान है।

auto keyword

C++ auto से आपके लिए type पता कर सकता है:

C++
auto count = 10;       // int
auto pi = 3.14159;     // double
auto letter = 'Z';     // char

लंबे type नाम दोहराने से बचने को auto इस्तेमाल करें; दाईं ओर की value type तय करती है।

आम गलतियाँ

  • Variable को value देने से पहले इस्तेमाल करना (garbage परिणाम)।
  • दशमलव को int में रखना — भिन्नात्मक हिस्सा गिर जाता है।
  • Text को single quotes में रखना; 'A' char है, "A" string है।
  • नाम digit से शुरू करना या keyword को नाम के रूप में इस्तेमाल करना।
🏋️ अभ्यास

एक int, एक double, एक char और एक bool declare करें, हर एक को value दें, और उन्हें labels के साथ एक line में print करें। फिर हर type का sizeof print करें।

सारांश

  • Variable memory में एक नामित, typed डिब्बा है जो value रखता है।
  • type name = value; से declare करें और declare करते ही initialise करें।
  • मुख्य types: int, float, double, char, bool; text के लिए string
  • sizeof दिखाता है कि type कितने bytes इस्तेमाल करता है।
  • auto compiler को value से type निकालने देता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.