×

Ceil function in C

Introduction

In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than or equal to x (i.e., rounds up the nearest integer).

The C library function double ceil(double x) returns the smallest integer value greater than or equal to x.

Declaration

The ceil function is declared using the keyword ‘ceil’. The following statement is the declaration for ceil() function.

double ceil(double x)

Parameters

  • x - This is the floating point value.

Return Value

This function returns the smallest integral value not less than x.

Program 1:

#include <stdio.h>  // header files
#include <math.h>




int main () {
  
 float val1, val2, val3, val4;  // declaring the local variables




   val1 = 1.6;
   val2 = 1.2;
   val3 = 2.8;
   val4 = 2.3;
   
   printf ("value1 = %.1lf\n", ceil(val1));
   printf ("value2 = %.1lf\n", ceil(val2));
   printf ("value3 = %.1lf\n", ceil(val3));
   printf ("value4 = %.1lf\n", ceil(val4));
   
   return(0);




}

Program 2 :

#include <stdio.h>  // header files
#include <math.h>
int main(int argc, const char * argv[])
{   
    double value;  // declaring the temporary variables 
    double result;
    value = 1.6;   // Assigning the value we will find the ceil of 
  result = ceil(value);   // Calculating the ceil of value 
  printf("The ceil of %f is %f\n", value, result);   // printing the result of the calculation 




    return 0;
    
}

The ceil function is a library function in C and is used to solve mathematical problems in C. The ceil function is used to solve approximation problems which comes handy when the programmer tries to enter that part of mathematical aspects. To get the broad idea of how these functions work, let’s try to understand more about the library functions in C.

What are library functions?

Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are grouped together and placed.

Each function in the library performs a specific task(operation) and is there to make things easy for the programmer.

The library functions are prototyped and defined at the time of designing the compiler and are present in their respective header files.

Eg: The main() function, printf() function etc. are some of the commonly used functions that are basically the library functions in C.

* The header files are implemented in the C program using the following command,

#include<filename.h>

For eg: #include<stdio.h>, #include<conio.h> …

* If we do not use the header files, the program will throw an error.

For eg: In order to use the printf() function, <stdio.h> should be included in order to print the desired output.

Header File Functions

Most of us are aware of various header files or library functions as they are often used while programming in C. Following is a list of commonly used library functions and the library files in which they are included

Header FileMeaningLibrary Functions
<stdio.h>Standard input-output headerprintf(),scanf(),getchar(c), putchar(s) etc.
<conio.h>Console input-output headerclrscr() , getch() etc.
<stdlib.h>Standard library headercalloc() and malloc()
<math.h>Math header (carry out mathematical calculations)sqrt(d),pow(d1,d2) etc.
<string.h>String header (performs operations on strings)strcpy,strlen,strrev etc.
<ctype.h>Character type header (carry out operations on character)isdigit(), tolower() , toupper() etc.

Program 3: use of <math.h> header file and use of sqrt() library functions

#include <stdio.h>  // header files
#include <math.h>




int main()
{
   float num, sq_root;  // declaring the local variables
   printf("Enter a number: ");
   scanf("%f", &num);




   sq_root = sqrt(num);  // computes the square root of num and stores in root.








   printf("Square root of %.2f = %.2f", num, sq_root);
   return 0;




}

Accessing Library Function

A library function can be accessed by following the simple steps;

  1. Writing the function name
  2. Writing the list of arguments

 (a) The arguments must be enclosed in brackets. They must be separated by commas.

 (b) There may be no arguments (in some cases)

Eg: pow (p1,p2) ? arguments enclosed in brackets and separated by commas

?

Function name

Significance/ Advantages of library function

The major reason to use library functions is that they make the work of a programmer easy, the programmer can simply use the pre-existing codes available in the compiler and need not worry about declaring and defining his own code in order to perform certain basic functions.

The other advantages of using library functions are as follows:

  • Flexibility
  • Performance optimized
  • Time-saving
  • Portability and reliability

Related Topics

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.