C के Decimal (Floating-Point) Numbers
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
float की Precision
float एक single-precision floating-point type है जो आमतौर पर लगभग 6 से 7 significant decimal digits accurately store करता है, 4 bytes memory इस्तेमाल करते हुए, इसे double से ज़्यादा compact लेकिन कम precise बनाते हुए।
उदाहरण: float Precision
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Decimal Output को Format करना
printf का %f specifier एक precision के साथ customize किया जा सकता है, जैसे exactly दो decimal places के लिए %.2f, और formatted decimal number output में कैसे align और pad हो यह control करने के लिए एक field width।
उदाहरण: Formatting Decimal Output
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- floats को
==से compare करना, जैसे0.1 + 0.2 == 0.3, जो rounding की वजह से false है। - एक
doubleके लिएscanfके साथ%fइस्तेमाल करना, जबscanfको doubles के लिए%lfचाहिए। - यह उम्मीद करना कि एक
floatकई digits exactly रखेगा, जब यह सिर्फ लगभग 6 से 7 significant digits रखता है।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers