C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In C programming, decimals or floating-point numbers can be represented using the float
and double
data types. This tutorial will provide an overview of these data types, how to use them, and how to perform basic arithmetic operations with them.
float
and double
The float
and double
data types are used to represent single-precision and double-precision floating-point numbers, respectively. The float
data type typically occupies 4 bytes (32 bits) of memory, while the double
data type typically occupies 8 bytes (64 bits) of memory. To declare and initialize a floating-point variable, you can use the following syntax:
float f1 = 3.14f; // Declare and initialize a float variable double d1 = 3.14; // Declare and initialize a double variable
Notice the use of the f
suffix when initializing a float
variable. This tells the compiler that the value is a single-precision floating-point number, rather than the default double-precision.
float
and double
To read and display float
and double
variables, you can use the scanf
and printf
functions with the appropriate format specifiers: %f
for float
and %lf
for double
.
Example:
#include <stdio.h> int main() { float f2; double d2; printf("Enter a float value: "); scanf("%f", &f2); printf("Enter a double value: "); scanf("%lf", &d2); printf("Float: %f\n", f2); printf("Double: %lf\n", d2); return 0; }
float
and double
You can perform basic arithmetic operations (addition, subtraction, multiplication, and division) with float
and double
variables, just like you would with integers.
Example:
#include <stdio.h> int main() { float f1 = 3.14f; float f2 = 2.71f; double d1 = 3.14159265; double d2 = 2.71828183; float f_sum = f1 + f2; float f_diff = f1 - f2; float f_product = f1 * f2; float f_div = f1 / f2; double d_sum = d1 + d2; double d_diff = d1 - d2; double d_product = d1 * d2; double d_div = d1 / d2; printf("Float sum: %f, difference: %f, product: %f, division: %f\n", f_sum, f_diff, f_product, f_div); printf("Double sum: %lf, difference: %lf, product: %lf, division: %lf\n", d_sum, d_diff, d_product, d_div); return 0; }
In summary, the float
and double
data types in C programming are used to represent decimal or floating-point numbers. You can perform basic arithmetic operations with these data types and use the scanf
and printf
functions to read and display their values. The main difference between float
and double
is the precision and memory occupied: float
uses single-precision (typically 4 bytes) and double
uses double-precision (typically 8 bytes).
Decimal Representation in C Language:
#include <stdio.h> int main() { int decimalNumber = 42; printf("Decimal number: %d\n", decimalNumber); return 0; }
Precision and Accuracy with float
and double
in C:
#include <stdio.h> int main() { float floatValue = 1.23456789; double doubleValue = 1.234567890123456789; printf("Float Value: %.7f\n", floatValue); // Limited precision printf("Double Value: %.18f\n", doubleValue); return 0; }
Floating-Point Arithmetic in C Programming:
#include <stdio.h> int main() { float result = 1.0 / 3.0; printf("Result: %.6f\n", result); return 0; }
C Code Examples with float
and double
Variables:
#include <stdio.h> int main() { float floatValue = 3.14; double doubleValue = 3.141592653589793; printf("Float Value: %.2f\n", floatValue); printf("Double Value: %.6f\n", doubleValue); return 0; }
Comparing float
and double
in C:
#include <stdio.h> int main() { float floatNum = 1.0 / 3.0; double doubleNum = 1.0 / 3.0; if (floatNum == doubleNum) { printf("They are equal.\n"); } else { printf("They are not equal.\n"); } return 0; }
Handling Decimal Input in C:
#include <stdio.h> int main() { float userInput; printf("Enter a decimal number: "); scanf("%f", &userInput); printf("You entered: %.2f\n", userInput); return 0; }
Printf Format Specifiers for float
and double
in C:
#include <stdio.h> int main() { float floatValue = 3.14; double doubleValue = 3.141592653589793; printf("Float Value: %e\n", floatValue); // Scientific notation printf("Double Value: %g\n", doubleValue); // Compact notation return 0; }
Floating-Point Errors and Mitigation in C:
#include <stdio.h> int main() { float a = 1.0 / 3.0; float b = 0.1 + 0.1 + 0.1; if (a == b) { printf("They are equal.\n"); } else { printf("They are not equal.\n"); } return 0; }