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

C के Decimal (Floating-Point) Numbers

Decimal numbers को float या double जैसे floating-point type की ज़रूरत होती है, क्योंकि integer types सिर्फ whole numbers store कर सकते हैं, और double standard choice है जो float से लगभग दोगुनी precision देता है।
Syntax
c
float variable_name = decimal_valuef;
double variable_name = decimal_value;

Decimal (Floating-Point) Numbers क्या हैं?

Decimal numbers, जिन्हें floating-point numbers भी कहते हैं, fractional part वाली values represent करते हैं और float या double जैसे type की ज़रूरत होती है, क्योंकि int और दूसरे integer types सिर्फ whole numbers store कर सकते हैं।

उदाहरण: What are Decimal (Floating-Point) Numbers?

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

float की Precision

float एक single-precision floating-point type है जो आमतौर पर लगभग 6 से 7 significant decimal digits accurately store करता है, 4 bytes memory इस्तेमाल करते हुए, इसे double से ज़्यादा compact लेकिन कम precise बनाते हुए।

उदाहरण: float Precision

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare f and set it to 1.0f / 3.0f
	float f = 1.0f / 3.0f;
	// Print formatted text to the screen
	printf("%.7f", f);
	// Return 0 to signal that the program finished successfully
	return 0;
}

double की Precision

double एक double-precision floating-point type है जो आमतौर पर लगभग 15 से 16 significant decimal digits store करता है, 8 bytes memory इस्तेमाल करते हुए, और C में decimal literals और ज़्यादातर floating-point math के लिए default choice है।

उदाहरण: double Precision

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

Decimal Output को Format करना

printf का %f specifier एक precision के साथ customize किया जा सकता है, जैसे exactly दो decimal places के लिए %.2f, और formatted decimal number output में कैसे align और pad हो यह control करने के लिए एक field width।

उदाहरण: Formatting Decimal Output

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

Floating-Point Numbers Compare करना

क्योंकि floating-point numbers binary में store होते हैं और हर decimal value को exactly represent नहीं कर सकते, दो floating-point values को exact equality के लिए compare करना unreliable है, और यह check करना कि उनका difference एक छोटी tolerance से कम है यह standard approach है।

उदाहरण: Comparing Floating-Point Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 0.1 + 0.2
	double a = 0.1 + 0.2;
	// Declare b and set it to 0.3
	double b = 0.3;
	// Check whether fabs(a - b) < 1e-9
	if (fabs(a - b) < 1e-9) {
		// Print formatted text to the screen
		printf("Equal enough");
	}
	// 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. floats को == से compare करना, जैसे 0.1 + 0.2 == 0.3, जो rounding की वजह से false है।
  2. एक double के लिए scanf के साथ %f इस्तेमाल करना, जब scanf को doubles के लिए %lf चाहिए।
  3. यह उम्मीद करना कि एक float कई digits exactly रखेगा, जब यह सिर्फ लगभग 6 से 7 significant digits रखता है।

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.