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.
Frequently Asked Questions
What is a variable in C++?
int age = 20;, which reserves memory for an integer and stores 20 in it.What are the basic data types in C++?
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++?
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++?
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 करना कहते हैं।
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 निकालने देता है।