Printing a float value in C is important when it comes to debugging your program, as you can easily check the correctness of your calculations. It helps you identify the exact values of the functions or variables used in your code. There are several different approaches you can take when it comes to printing the float value depending on the complexity of your code and the language version you are using. This article will explain two different methods for printing a float value in C.
Print a float Value in C Language
There are two ways to print a float value in C Language.
Method 1: Using printf() Function
The first way of printing a float value in C is to use the printf() function. This is a built-in function that is available in most versions of the C language and allows you to easily print out the value of a float. The printf() function needs to have a format specifier that indicates the type of data being passed in (in this case, a float). This is usually %f and must be placed between quotation marks. After the format specifier there should be the name of the float value, indicated by its memory address. The code to print a float using printf() looks like this:
int main()
{
float x= 3.42;
printf("float %f", x);
return 0;
}
In the case above, x is the name of the float being printed. By default, it prints float values up to 6 decimal places.
Output
Additionally, you may select how many decimal places should be shown in the output. This can be done by adding a period followed by the number of decimal places before the f in %f. For example, “%.3f” would print the float to three decimal places.
Let’s see this in the code below.
int main()
{
float x= 3.42;
printf("float %.3f", x);
return 0;
}
In this code, ‘%.3f’ specifies to print the value of float x up to three decimal places.
Output
Method 2: Using fprintf() Function
The second way of printing a float value in C is to use the fprintf() function. This function can be useful for printing more complex structures, like an array of floats. The fprintf() method operates similarly to printf() but requires an additional parameter, a file reference. This indicates which file the output should be printed to. The code to print an array of floats would look like this:
int main ()
{
FILE * file;
file = fopen ("floatfile.txt", "w");
if (file == NULL)
{
printf ("This file doesn't exist!");
return 0;
}
float num;
printf ("Enter value of num:");
scanf ("%f", &num);
fprintf (file, "num : %.2f\n", num);
fclose (file);
return 0;
}
In the above case, ‘file’ is the file pointer and ‘num’ is the name of the variable that contains the floats. As before, you can add the number of decimal places the float should be printed to after the %f and the value of floats can be seen written in the file.
Output
File
The value of float is printed in the file for two decimal places as specified in the code.
Conclusion
It is possible to print a float value in C using either the printf() or fprintf() functions. Both functions require a format specifier (%f) and the name of the float (or array of floats) that you wish to print. It’s also possible to specify the number of decimal places the float should be printed to by adding a period followed by the number of decimal places after the %f. Understanding these methods will help you print complex structures with ease and confidence in C.