Float:
In C, the float data type is used to represent floating-point numbers, which are real numbers with a decimal point. It is one of the primary data types and is commonly used for variables that store values with fractional parts. The float type typically occupies 4 bytes in memory.
Example:
#include <stdio.h>
int main() {
// Declaration and initialization of float variables
float num1 = 3.14;
float num2 = -2.5;
float sum, product;// Performing arithmetic operations
sum = num1 + num2;
product = num1 * num2;// Displaying the values and results
printf(“Number 1: %fn”, num1);
printf(“Number 2: %fn”, num2);
printf(“Sum: %fn”, sum);
printf(“Product: %fn”, product);return 0;
}
Output:
Number 1: 3.140000
Number 2: -2.500000
Sum: 0.640000
Product: -7.850000
Explanation of code:
- #include <stdio.h>: This line includes the standard input-output library for functions like printf.
- int main(): The main function, where the program execution begins.
- float num1 = 3.14; and float num2 = -2.5;: Declaration and initialization of float variables.
- float sum, product;: Declaration of two more float variables without initialization.
- sum = num1 + num2; and product = num1 * num2;: Performing arithmetic operations.
- printf(“Number 1: %fn”, num1);: Printing the value of num1 using the %f format specifier.
- Similarly, the values of num2, sum, and product are printed using printf.
- return 0;: Indicates successful program execution.