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.
int age = 20; // declare and initialise double price = 9.99; char grade = 'A'; bool isPassed = true; age = 21; // change the value later
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
| Type | Holds | Example |
|---|---|---|
int | Whole numbers | 42, -7 |
float | Decimal numbers (less precise) | 3.14f |
double | Decimal numbers (more precise) | 3.14159 |
char | A single character | 'A' |
bool | true or false | true |
Sizes and ranges
Each type uses a fixed amount of memory. You can check it with sizeof:
#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;
}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
#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;
}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:
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.
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;stringfor text. sizeofshows how many bytes a type uses.autolets 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 करना कहते हैं।
int age = 20; // declare aur initialise double price = 9.99; char grade = 'A'; bool isPassed = true; age = 21; // baad mein value badlein
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' |
bool | true या false | true |
Sizes और ranges
हर type एक निश्चित मात्रा में memory इस्तेमाल करता है। आप इसे sizeof से जाँच सकते हैं:
#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;
}int: 4 bytes
double: 8 bytes
char: 1 bytes
Sizes system के अनुसार बदल सकते हैं, पर int आमतौर पर 4 bytes, double 8, और char हमेशा 1।
एक चलता उदाहरण
#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;
}Amit is 20 with 88.5%
Text के लिए string type पर ध्यान दें — यह standard library से आता है और C-शैली के character arrays से कहीं आसान है।
auto keyword
C++ auto से आपके लिए type पता कर सकता है:
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 इस्तेमाल करता है।autocompiler को value से type निकालने देता है।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में variable क्या है?
int age = 20;, जो एक integer के लिए memory आरक्षित करता है और उसमें 20 रखता है।C++ में बुनियादी data types क्या हैं?
int, दशमलव के लिए float और double, एकल characters के लिए char, और true/false values के लिए bool। short, 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 पर देनी होगी।