← Back to C Course | Chapter 1: Introduction & Basics | Lesson 11 of 21

C के Numeric Data Types

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare s and set it to 10
	short s = 10;
	// Declare i and set it to 1000
	int i = 1000;
	long l = 100000L;
	long long ll = 10000000000LL;
	// Print formatted text to the screen
	printf("%hd %d %ld %lld", s, i, l, ll);
	// 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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// 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 formatted text to the screen
	printf("%.2f %.11f", f, d);
	// 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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare quantity and set it to 3
	int quantity = 3;
	// Declare price and set it to 19.99
	double price = 19.99;
	// Print formatted text to the screen
	printf("Total: %.2f", quantity * price);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Number Ranges और Overflow

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

उदाहरण: Number Ranges and Overflow

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare maxVal and set it to 2147483647
	int maxVal = 2147483647;
	maxVal = maxVal + 1;
	// Print formatted text to the screen
	printf("%d", maxVal);
	// Return 0 to signal that the program finished successfully
	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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare decimal and set it to 100
	int decimal = 100;
	// Declare hex and set it to 0x64
	int hex = 0x64;
	int octal = 0144;
	// Print formatted text to the screen
	printf("%d %d %d", decimal, hex, octal);
	// 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. 5 / 2 जैसे दो ints divide करना और 2.5 की उम्मीद करना, जब integer division 2 देता है।
  2. एक int को overflow करना, जैसे INT_MAX में 1 add करना, जो signed types के लिए undefined behavior है।
  3. गलत format specifier इस्तेमाल करना, जैसे एक double के लिए %d, जो garbage print करता है।

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.