In certain cases, it is more convenient to work with integers in the input arguments of these functions or with their results, either because our program should work only with these quantities or because their fractional values would not represent a significant break in the final result of the operation.
For these rounding operations, the C language provides a set of functions included in the “math” library. To round a variable to the nearest integer, there is the function ceil(). To round a variable to the nearest integer, there is the function floor().
In this Linux Hint article, you will learn all about using the function to round the fractional values to the nearest integer.
To help you better understand and master this function, we created practical examples with code fragments and images that show its use with different types of input variables.
Syntax of the Ceil() Function in C language:
Ceil() Function Description in the C Language
This function rounds to the next larger integer value of the numeric variable that is specified in its input arguments.
The ceil() function is one of a set of functions provided by the “math” library. To use this function, we must include it in our “.c” file as follows:
Once the “math” libraries are included, we can use ceil() to round up or floor() to round down, as well as all the functions in the math libraries.
To get the rounding of “x”, you must call this function, specifying the variable name in its input argument. Ceil() returns the result in “a”.
Example 1: How to Round a Fractional Integer with the Ceil() Function in Linux GCC
In this example, we will see how to round a fractional value to the next larger integer using the ceil function in gcc.
The following code snippet shows how to use the ceil() function to get the rounding of double “x” that has an assigned value of 3.1416. Then, the printf() function is used to output the result as “a” on the command console:
In the following image, we see the result in the command console. In this case, the rounding of 3.1416 is 4.00000.
Common Problems with the Rounding Functions Ceil() and Floor() and How to Solve Them
When we use the functions and compile our code, it often happens that the compilation gives the following error:
This causes us to look for the syntax errors or undefined variables in our “.c” or “.h” code, since everything indicates that our problem lies there. But this leads to a loss of valuable time since our problem lies in the linking of the libraries and the data compatibility of these functions.
For programmers who like to know the reason for a problem and not just solve it, we will explain this error step by step to make programming work more fluent and avoid loading unnecessary libraries for our code.
To explain this, we compile the code from the previous example as follows. In the following figure, we see the compilation of our code and the error that is referred to in this section:
This is because since C99, gcc splits its libraries into two parts – libc and libm. Although the library that we refer to in our code is found in both, the data types that these functions accept in each of their versions are different, and that is the source of the problem.
If the function ceil() is called with an int as input, the error disappears, although the rounding down occurs.
These problems are fixed at compile time by invoking the libm library on the command line that we use to compile. The following is the path:
Conclusion
In this Linux Hint article, we showed you how to use the Yes function to round the fractional values to the nearest integer. We explained step by step on how to load the “math” library to use this function. We also showed you how to use the rounding functions of this library with a practical example. We also showed you one of the most common errors when using this function and how to fix it to get a smooth programming task. We hope you found this C language article helpful. Read the other Linux Hint articles for more tips and information.