C++ के Numeric Data Types
In this page:
integer_type variable_name = whole_number;
floating_type variable_name = decimal_number;
// integer_type: short, int, long, long long
// floating_type: float, double
Integer Types
C++ कई integer types देता है -- short, int, long, और long long -- हर एक whole numbers की अलग range देता है, int ज़्यादातर everyday counting और arithmetic के लिए standard default choice है।
उदाहरण: Integer Types
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare small and set it to 100
short small = 100;
// Declare regular and set it to 100000
int regular = 100000;
long big = 100000000L;
long long huge = 10000000000LL;
// Print to the console with cout
std::cout << small << " " << regular << " " << big << " " << huge << 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
Floating-point types, float और double, fractional part वाले numbers store करते हैं, double float से लगभग दोगुनी precision देता है ज़्यादा memory इस्तेमाल करने की कीमत पर।
उदाहरण: Floating-Point Types
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare f and set it to 3.14f
float f = 3.14f;
// Declare d and set it to 3.14159265358979
double d = 3.14159265358979;
// Print to the console with cout
std::cout << f << " " << d << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
int बनाम float बनाम double चुनना
int, float, और double के बीच चुनना इस पर आता है कि क्या एक value हमेशा whole number है, limited memory use के साथ decimal precision चाहिए, या ज़्यादातर general decimal math के लिए double जितनी higher precision चाहिए।
उदाहरण: Choosing int vs float vs double
#include <iostream>
int main() {
int quantity = 5; // always whole
float weightKg = 2.5f; // decimal, limited memory
double preciseValue = 3.14159265358979; // higher precision
std::cout << quantity << " " << weightKg << " " << preciseValue << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Number Ranges और Overflow
हर numeric type की representable values की एक fixed range होती है, और उस range को पार करना overflow cause करता है, जहाँ value चुपचाप wrap around होकर error उठाने की बजाय एक unexpected result देती है।
उदाहरण: Number Ranges and Overflow
#include <iostream>
#include <climits>
int main() {
int maxVal = INT_MAX;
int overflowed = maxVal + 1; // wraps around unexpectedly
std::cout << maxVal << " " << overflowed << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Numeric Literals
Numeric literals decimal, hexadecimal (0x prefix के साथ), या octal (leading 0 के साथ) form में लिखे जा सकते हैं, और L, U, या F जैसे suffixes compiler को बताते हैं कि literal को किस specific type की तरह treat करना है।
उदाहरण: Numeric Literals
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare decimal and set it to 255
int decimal = 255;
// Declare hex and set it to 0xFF
int hex = 0xFF;
int octal = 0377;
long big = 100000L;
// Print to the console with cout
std::cout << decimal << " " << hex << " " << octal << " " << big << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक
intमें एक बड़ी value store करना, जैसे 3000000000, जो typical 32-bit ints पर overflow करती है और एक गलत result देती है। 5 / 2जैसे दो ints divide करना और 2.5 की उम्मीद करना, जब integer division 2 देता है।- floating-point numbers को
==से compare करना, जैसे0.1 + 0.2 == 0.3, जो rounding की वजह से false है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: