DEV Community

Cover image for Standard library functions in C programming.
Sujith V S
Sujith V S

Posted on

Standard library functions in C programming.

Standard library functions are predefined function which is already defined inside a file and we can directly use them in our program.

Input / Output functions

printf()
printf() is a standard library function which is defined in the stdio.h header file. So that is why <stdio.h> is included in our program file.

#include <stdio.h>

int main() {

    printf("Hello world");

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Other I/O functions in C are:
scanf(): Reads formatted input from the console.
getchar(): Reads a single character from the console.
putchar(): Writes a single character to the console.
fopen(): Opens a file.
fclose(): Closes a file.
fread(): Reads data from a file.
fwrite(): Writes data to a file.

Mathematical functions

sqrt()
sqrt() is a standard library function which is defined in the <math.h> header file. It is used to calculate the square root of a number.

#include <stdio.h>
#include <math.h>

int main() {

    int num = 25;
    printf("Square root %lf", sqrt(num));

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

cbrt()
cbrt() is a standard library function which is defined in the <math.h> header file. It is used to calculate the cube root of a number.

#include <stdio.h>
#include <math.h>
int main() {

    int num = 27;
    printf("Cube root %lf", cbrt(num));

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

pow(a, b)
In C, pow(a, b) is used to calculate the a raised to the power of b. It's available in the <math.h> header file and takes two arguments.
a: The base value
b: The exponent

#include <stdio.h>
#include <math.h>
int main() {

    int a = 5;
    int b = 2;

    double result = pow(a, b);
    printf("Power: %lf", result);

    return 0;
} 
Enter fullscreen mode Exit fullscreen mode

Other mathematical functions in C are:
abs(): Returns the absolute value of a number.
sin(): Calculates the sine of an angle.
cos(): Calculates the cosine of an angle.

Character handling functions

toupper()
toupper() is a standard library function which is defined in the <ctype.h> header file. It is used to convert a lowercase letter to its uppercase equivalent.

#include <stdio.h>
#include <ctype.h>

int main() {
    char alpha = 'e';
    char upper = toupper(alpha);
    printf("%c", upper);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Other character handling functions in C are:
isalpha(): Checks if a character is an alphabet.
isdigit(): Checks if a character is a digit.
isupper(): Checks if a character is uppercase.
islower(): Checks if a character is lowercase.
tolower(): Converts a character to lowercase.


Apart from the standard library functions that are mentioned above, there are many other standard library functions in C programming which are used for important operations. The remaining functions will be discussed in the upcoming posts.

Top comments (0)