c
C Programming Tutorial : Introduction

C Functions: Library Functions and User-Defined Function

Library Functions

C has the facility to provide library functions for performing some operations. These functions are presents in the C library, and they are predefined. For example, sqrt() is a mathematical library function which is used for finding out the square root of any number. The functions scanf( ) and printf( ) are input-output library functions. Similarly we have functions like strlen(), strcmp() for string manipulation.

To use a library function, we must include the corresponding header file using the preprocessor directive #include. For example to use input-output functions like printf(), scanf() we have to include stdio.h, for mathematical library functions we have to include math.h, for string library string.h should be included. The following program illustrates the use of library function sqrt().

Example-1

/*Program to find' the square root of. any nunber.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    double n, s;
    printf ("Enter a number ");
    scanf (''%If'', &n) ;
    s=sqrt (n) ;
    printf ("The square root of %. 2lf is= %.2lf\n",n, s );
    getch();
}
OutPut
Enter a number: 12
Square root of 12.00 is= 3.46
 

In this program we have used three library functions – printf(), scanf() and sqrt( ).you will learn more about library functions later in this tutorial.

Example Program For Strlen() Function In C:

Example-2

/*WAP program to find , length of the string “edutechwebs.com” is 
determined by strlen() function as below. Length of this string 15 is 
displayed as output*/

#include <stdio.h>
#include <string.h>
 
int main( )
{
    int len;
    char array[20]=" edutechwebs.com " ;
 
    len = strlen(array) ;
 
    printf ( "\string length  = %d \n" , len ) ;
    return 0;
}

OUTPUT:
string length = 15
 

List of Most Used Header Files In C Programming Language:

Header file

Description

stdio.h

This is standard input/output header file in which Input/Output
functions are declared

conio.h

This is console input/output header file

string.h

All string related functions are defined in this header file

stdlib.h

This header file contains general functions used in C programs

math.h

All maths-related functions are defined in this header file

time.h

This header file contains time and clock-related functions

ctype.h

All character handling functions are defined in this header file

stdarg.h

Variable argument functions are declared in this header file

signal.h

Signal handling functions are declared in this file

setjmp.h

This file contains all jump functions

locale.h

This file contains locale functions

errno.h

Error handling functions are given in this file

assert.h

This contains diagnostics functions

User-defined functions

Users can create their own functions for performing any specific task of the program. These types of functions are called user-defined functions. To create and use these functions, we should know about these three things-

  1. Function definition.
  2. Function declaration.
  3. Function call.

How user-defined function works?

Syntax -1

#include <stdio.h>
void function-Name (); /*Function Declaration*/

int main()
{
    ... .. ...
    ... .. ...

    functionName(); /*Function Call*/
    
    ... .. ...
    ... .. ...
}
void function-Name () /*Function Definition*/

{
    ... .. ...
    ... .. ... 	
}
 

Example-1

/ * Program to draw a line*/
#include<stdio.h>
#include<conio.h>

void drawline (void); /*Function Declaration*/
main()
{
draw1ine();  /*Function Call*/
getch();
}
void drawline (void) /*Function Definition*/ \
{
int i;
for(i=1;i<=80;i++)
printf(" _ " ) ;
}
 

Syntax -2

/*Program to . find the sum of two numbers*/
#include<stdio.h>
int sum(int x, inty); /*Function declaration*/
main ( )
{
int a,b, s;
printf ("Enter values for a and b”);
scanf ("%d %d", &a, &b);
s=sum (a, b); 	/*Function call*/
printf ("Sum of %d and %d is % d \n" ,a,b,s) ;
}
int sum (int x, int y) /*Function definition*/
{
int s;
S=X+Y;
re'turn s;
}