C++ के Data Types
In this page:
data_type variable_name = value;
// e.g. int, char, float, double, bool
Integer Type
int type बिना किसी fractional component के whole numbers store करता है, और ज़्यादातर modern 32-bit और 64-bit systems पर यह 4 bytes occupy करता है, इसे लगभग -2 billion से +2 billion तक की range देते हुए।
counters, indexes, और किसी भी ऐसी value के लिए int इस्तेमाल करें जिसे कभी decimal point की ज़रूरत नहीं पड़ेगी।
उदाहरण: Integer Type
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare counter and set it to 2000000
int counter = 2000000;
// Print to the console with cout
std::cout << "Counter: " << counter << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Character Type
char type exactly 1 byte में एक single character store करता है, पूरी strings के लिए इस्तेमाल होने वाले double quotes की बजाय A जैसे single quotes इस्तेमाल करते हुए।
Internally, एक char असल में सिर्फ एक छोटा integer है जो ASCII table में उस character की position represent करता है, यही वजह है कि आप characters पर सीधे arithmetic perform कर सकते हैं।
उदाहरण: Character Type
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare grade and set it to 'A'
char grade = 'A';
// Print to the console with cout
std::cout << "Grade: " << grade << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Floating-Point Types
float और double दोनों decimal point वाले numbers store करते हैं, लेकिन वे precision के लिए memory trade off करते हैं: float 4 bytes और लगभग 7 significant digits की accuracy इस्तेमाल करता है, जबकि double लगभग 15-16 digits के लिए 8 bytes इस्तेमाल करता है।
ज़्यादातर C++ code default से double इस्तेमाल करता है जब तक memory tightly constrained न हो, क्योंकि extra precision subtle rounding errors से बचाती है।
उदाहरण: Floating-Point Types
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare price and set it to 9.99f
float price = 9.99f;
// Declare pi and set it to 3.14159265358979
double pi = 3.14159265358979;
// Print to the console with cout
std::cout << price << " " << pi << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boolean Type
bool type सिर्फ दो possible values रखता है, true या false, और यह किसी भी yes/no condition represent करने वाली चीज़ के लिए natural type है, जैसे क्या एक user logged in है।
एक int flag (0 या 1) की बजाय bool इस्तेमाल करना conditional code को self-documenting बनाता है, क्योंकि isLoggedIn loggedInFlag == 1 से कहीं ज़्यादा clearly पढ़ता है।
उदाहरण: Boolean Type
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isLoggedIn and set it to true
bool isLoggedIn = true;
// Print to the console with cout
std::cout << "Logged in: " << isLoggedIn << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Void Type
void एक value की deliberate absence represent करता है, सबसे ज़्यादा एक function के return type की तरह दिखता है जो एक action perform करता है लेकिन caller को कुछ वापस नहीं देता, जैसे void printMessage()।
यह कुछ lower-level contexts में भी इस्तेमाल होता है, जैसे एक generic pointer type void* जो किसी भी type के data की ओर point कर सकता है।
उदाहरण: Void Type
// Include std::cout and std::cin
#include <iostream>
void printMessage() {
// Print to the console with cout
std::cout << "No value returned" << std::endl;
}
// Program execution starts in main()
int main() {
printMessage();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- decimals वाली एक value के लिए
intइस्तेमाल करना जैसेint price = 9.99;, जो चुपचाप 9 तक truncate हो जाता है। - यह भूल जाना कि
charएक single character रखता है औरchar name = "Bob";लिखना, जो compile नहीं होता। - यह मान लेना कि
intयाlongका size हर platform पर same है, जब standard सिर्फ minimum sizes guarantee करता है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: