Functions in ‘C’
language
Functions
in ‘C’ language are divided into two types, standard library functions and user
defined functions or a programmer defined functions.
Standard Library functions
Standard
library functions are pre-packaged functions or in-built functions of ‘C’
The
‘C’ standard library provide a rich collection of functions for performing
common mathematical calculations, string manipulations character manipulations,
input/output and many other useful operations.
List
of the most common libraries and a brief description of the most useful
functions is given below:
stdio.h:
I/O functions:
getchar()
returns the next character typed on the keyboard
putchar()
outputs a single character to the screen
printf()
used to write functions to write I/O devices
scanf()
used to read from I/O devices
string.h: String functions
strcat()
concatenates a copy of str2 to str1
strcmp()
compares two strings
strcpy()
compares contents of str2 to str1
ctype.h:
Character
functions
isdigit()
returns non-0 if arg is digit 0 to 9
islapha()
returns non-0 if arg is a letter of the alphabet
isalnum()
retuns non-0 if arg is a letter or digit
isslower()
returns non-0 if arg is a lowercase letter
isupper()
returns non-0 if arg is uppercase letter
math.h: Mathematics functions
acos()
returns arc cosine of arg
asin()
returns arc sine of arg
atan()
returns arc tangent of arg
cos()
returns cosine of arg
exp()
returns natural logarithm e
fabs()
returns absolute value of num
sqrt()
returns square root of num
time.h: Time and Date
functions
time()
returns current calendar time of system
difftime()
returns difference in sec between two times
clock()
returns number of system clock cycles since program execution
stdlib.h: Miscellaneous
functions
malloc()
provides dynamic memory allocation, covered in future sections
reand()
as already described previously
srand()
used to set the starting point for rand()
User defined functions
User
defined functions are self-contained blocks of statements which are written by
the user to compute or perform a task.
They
can be called by the main program repeatedly as per the requirement.
Uses of functions
They
are very much useful when a block of statements has to be written/executed
again and again.
They
are useful when program sizes are too large and complex.
It
works like a top-down modular programming technique, to solve a problem.
They
are also used to reduce the difficulties during debugging a program.
The
length of a source program can be reduced by using functions at appropriate
places.
It
is easy to locate and isolate a faulty function for further investigations.
A
function can be used by many other programs.
Thus,
the C programmer can build their own library.