×

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

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 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.