← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 10 of 15

C++ के Numeric Data Types

C++ integer types (short, int, long, long long) और floating-point types (float, double) देता है, हर एक की अलग range या precision, इस आधार पर चुना जाता है कि किस तरह का number store करना है।
Syntax
cpp
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

cpp
// 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;
}

Floating-Point Types

Floating-point types, float और double, fractional part वाले numbers store करते हैं, double float से लगभग दोगुनी precision देता है ज़्यादा memory इस्तेमाल करने की कीमत पर।

उदाहरण: Floating-Point Types

cpp
// 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;
}

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

cpp
#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;
}

Number Ranges और Overflow

हर numeric type की representable values की एक fixed range होती है, और उस range को पार करना overflow cause करता है, जहाँ value चुपचाप wrap around होकर error उठाने की बजाय एक unexpected result देती है।

उदाहरण: Number Ranges and Overflow

cpp
#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;
}

Numeric Literals

Numeric literals decimal, hexadecimal (0x prefix के साथ), या octal (leading 0 के साथ) form में लिखे जा सकते हैं, और L, U, या F जैसे suffixes compiler को बताते हैं कि literal को किस specific type की तरह treat करना है।

उदाहरण: Numeric Literals

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक int में एक बड़ी value store करना, जैसे 3000000000, जो typical 32-bit ints पर overflow करती है और एक गलत result देती है।
  2. 5 / 2 जैसे दो ints divide करना और 2.5 की उम्मीद करना, जब integer division 2 देता है।
  3. floating-point numbers को == से compare करना, जैसे 0.1 + 0.2 == 0.3, जो rounding की वजह से false है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.