This function has three main categories: round, roundf, and roundl. These functions return the appropriate type value for several types of floating-point numbers. Source files, such as the math header, must be compiled with the -lm flag to connect the library code.
The term “round” refers to a round value away from zero. The very same value is given if the integral value, -0, +0, NaN, is passed as an input. The lround function provides the integral value after rounding to the closest integer. With the help of illustrations, we will comprehend the round() function in C in this article.
Syntax
Syntax for round( ) function in C is specified underneath.
- double round (double y);
float roundf (float y);
long double roundl (long double y);
Pre-requisites
- Installation of GCC compiler on Windows 10 or any other operating system
Example 1
After learning about the basic concept and syntax of the round() function in the C programming language, let’s move towards our first example.
We have created a file “Untitled.c” in our installed GCC compiler. After that, we have added two main libraries that are required to execute this code example. Then the most important part of this program is a main() function where the whole process will execute. Initially, we have two float-type variables titled “a” and “b”. We have assigned them a value of 8.9 and 7.6. You can alter these values as well.
Then we have a printf() function, which will be used to round the provided values. For this purpose, we have called the round function to round off these values. Then we have mentioned “Return 0”. The term return is being used to return a value from a function. It signifies that our program has been accomplished appropriately, and we utilize this return statement to finish our main program.
Now save the file and run it on the GCC compiler. If any exception occurs, it will appear on the GCC compiler. Else, you will get a black console screen. You can verify that the values declared in the program have been rounded off to their nearest value.
Example 2
Now let’s move towards our second example. We have created a file “Untitled.c” in our installed GCC compiler. We have added the main libraries that are required to execute this code sample. After that, we have a function titled “roundNo” that will return an integer type value. In this function, we have defined the required condition. Then the most important part of this program is a main() function where the whole process will execute. Then we have a printf() function, which will be used to round the provided value inside the function. Then we have a getchar() function.
A getchar() function is indeed a non-standard function that accepts unique user input and has its meaning specified in the stdin.h header file. Put another way; the C library function reads a specific character (unsigned char) via stdin. Then we have mentioned “Return 0”. The term return is being used to return a value from a function. It signifies that our program has been finished appropriately, and we utilize this return statement to finish our main program.
Now save the file and run it on the GCC compiler. If any exception occurs, it will appear on the GCC compiler. If not, you will get a black console screen. You can confirm that the values declared in the program have been rounded off to their nearest value.
Conclusion
This article has covered the basic introduction of the round() function in the C programming language. We have introduced the syntax section in this guide to using the round() function accordingly. We have elaborated two examples to discuss the round() function. Now implement both examples by making little changes to them.