C Standard Library Functions
C Standard library functions or simply C Library functions are inbuilt functions in C
programming.
The prototype and data definitions of these functions are present in their respective
header files. To use these functions we need to include the header file in our
program. For example,
If you want to use the printf() function, the header file <stdio.h> should be
included.
#include <stdio.h>
int main()
{
printf("Catch me if you can.");
}
If you try to use printf() without including the stdio.h header file, you will get an
error.
Advantages of Using C library functions
1. They work
One of the most important reasons you should use library functions is simply
because they work. These functions have gone through multiple rigorous testing
and are easy to use.
2. The functions are optimized for performance
Since, the functions are "standard library" functions, a dedicated group of
developers constantly make them better. In the process, they are able to create the
most efficient code optimized for maximum performance.
3. It saves considerable development time
Since the general functions like printing to a screen, calculating the square root,
and many more are already written. You shouldn't worry about creating them once
again.
4. The functions are portable
With ever-changing real-world needs, your application is expected to work every
time, everywhere. And, these library functions help you in that they do the same
thing on every computer.
Example: Square root using sqrt() function
Suppose, you want to find the square root of a number.
To compute the square root of a number, you can use the sqrt() library function.
The function is defined in the math.h header file.
#include <stdio.h>
#include <math.h>
int main()
{
float num, root;
printf("Enter a number: ");
scanf("%f", &num);
// Computes the square root of num and stores in root.
root = sqrt(num);
printf("Square root of %.2f = %.2f", num, root);
return 0;
}
Run Code
When you run the program, the output will be:
Enter a number: 12
Square root of 12.00 = 3.46
User-defined function
You can also create functions as per your need. Such functions created by the user
are known as user-defined functions.
How user-defined function works?
#include <stdio.h>
void functionName()
... .. ...
... .. ...
int main()
... .. ...
... .. ...
functionName();
... .. ...
... .. ...
The execution of a C program begins from the main() function.
When the compiler encounters functionName();, control of the program jumps to
void functionName()
And, the compiler starts executing the codes inside functionName().
The control of the program jumps back to the main() function once code inside the
function definition is executed.
Note, function names are identifiers and should be unique.
This is just an overview of user-defined functions. Visit these pages to learn more
on:
User-defined Function in C programming
Types of user-defined Functions
Advantages of user-defined function
1.The program will be easier to understand, maintain and debug.
2.Reusable codes that can be used in other programs
3.A large program can be divided into smaller modules. Hence, a large project can
be divided among many programmers.
Note, function names are identifiers and should be unique.
Advantages of user-defined function
1.The program will be easier to understand, maintain and debug.
2.Reusable codes that can be used in other programs
3.A large program can be divided into smaller modules. Hence, a large project can
be divided among many programmers.
Note, function names are identifiers and should be unique.
This is just an overview of user-defined functions. Visit these pages to learn more
on:
User-defined Function in C programming
Types of user-defined Functions
Advantages of user-defined function
1.The program will be easier to understand, maintain and debug.
2.Reusable codes that can be used in other programs
3.A large program can be divided into smaller modules. Hence, a large project can
be divided among many programmers.
Types of User-defined Functions in C
Programming
In this tutorial, you will learn about different approaches you can take to solve the
same problem using functions.
These 4 programs below check whether the integer entered by the user is a prime
number or not.
The output of all these programs below is the same, and we have created a user-
defined function in each example. However, the approach we have taken in each
example is different.
Example 1: No Argument Passed and No Return Value
#include <stdio.h>
void checkPrimeNumber();
int main() {
checkPrimeNumber(); // argument is not passed
return 0;
}
// return type is void meaning doesn't return any value
void checkPrimeNumber() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
// 0 and 1 are not prime numbers
if (n == 0 || n == 1)
flag = 1;
for(i = 2; i <= n/2; ++i) {
if(n%i == 0) {
flag = 1;
break;
}
The checkPrimeNumber() function takes input from the user, checks whether it is a
prime number or not, and displays it on the screen.
The empty parentheses in checkPrimeNumber(); inside the main() function indicates
that no argument is passed to the function.
The return type of the function is void. Hence, no value is returned from the
function.
Example 2: No Arguments Passed But Returns a Value
#include <stdio.h>
int getInteger();
int main() {
int n, i, flag = 0;
// no argument is passed
n = getInteger();
// 0 and 1 are not prime numbers
if (n == 0 || n == 1)
flag = 1;
for(i = 2; i <= n/2; ++i) {
if(n%i == 0){
flag = 1;
break;
}
}
if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
return 0;
}
// returns integer entered by the user
int getInteger() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
return n;
}
The empty parentheses in the n = getInteger(); statement indicates that no
argument is passed to the function. And, the value returned from the function is
assigned to n.
Here, the getInteger() function takes input from the user and returns it. The code to check
whether a number is prime or not is inside the main() function.
Example 3: Argument Passed But No Return Value#include
<stdio.h>
void checkPrimeAndDisplay(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
// n is passed to the function
checkPrimeAndDisplay(n);
return 0;
}
// return type is void meaning doesn't return any value
void checkPrimeAndDisplay(int n) {
int i, flag = 0;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1)
flag = 1;
for(i = 2; i <= n/2; ++i) {
if(n%i == 0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not a prime number.",n);
else
printf("%d is a prime number.", n);
}
The integer value entered by the user is passed to
the checkPrimeAndDisplay() function.
Here, the checkPrimeAndDisplay() function checks whether the argument passed is a
prime number or not and displays the appropriate message.
Example 4: Argument Passed and Returns a Value
#include <stdio.h>
int checkPrimeNumber(int n);
int main() {
int n, flag;
printf("Enter a positive integer: ");
scanf("%d",&n);
// n is passed to the checkPrimeNumber() function
// the returned value is assigned to the flag variable
flag = checkPrimeNumber(n);
if(flag == 1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
// int is returned from the function
int checkPrimeNumber(int n) {
// 0 and 1 are not prime numbers
if (n == 0 || n == 1)
return 1;
int i;
for(i=2; i <= n/2; ++i) {
if(n%i == 0)
return 1;
}
return 0;
}
The input from the user is passed to the checkPrimeNumber() function.
The checkPrimeNumber() function checks whether the passed argument is prime or
not.
If the passed argument is a prime number, the function returns 0. If the passed
argument is a non-prime number, the function returns 1. The return value is
assigned to the flag variable.
Depending on whether flag is 0 or 1, an appropriate message is printed from
the main() function.