🟢 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.

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 निकालने देता है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में variable क्या है?
Variable एक नामित storage स्थान है जो एक value रखता है जिसे आपका program इस्तेमाल और बदल सकता है। आप इसे एक type और नाम से declare करते हैं, जैसे int age = 20;, जो एक integer के लिए memory आरक्षित करता है और उसमें 20 रखता है।
C++ में बुनियादी data types क्या हैं?
मूल built-in types हैं: पूर्ण संख्याओं के लिए int, दशमलव के लिए float और double, एकल characters के लिए char, और true/false values के लिए boolshort, long, signed और unsigned जैसे modifiers इनका size और range समायोजित करते हैं।
C++ में float और double में क्या अंतर है?
दोनों दशमलव संख्याएँ रखते हैं, पर double ज़्यादा memory (आमतौर पर 8 bytes) इस्तेमाल करता है और float (आमतौर पर 4 bytes) से लगभग दोगुनी सटीकता रखता है। ज़्यादा सटीक दशमलव चाहिए तो double इस्तेमाल करें, इसीलिए यह C++ में आम default है।
C++ में bool data type क्या है?
bool type दो values में से एक रखता है, true या false। यह conditions और logic के लिए इस्तेमाल होता है, और भीतर से true 1 और false 0 के रूप में रखा जाता है। पुरानी C के विपरीत, C++ में bool एक असली built-in type है।
C++ में auto keyword क्या करता है?
auto compiler को आपके दिए value से variable का type निकालने देता है, तो auto x = 5; x को int बनाता है और auto pi = 3.14; उसे double। यह दोहराव कम करता है और लंबे type नामों के साथ काम आता है, हालाँकि value declaration पर देनी होगी।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.