C Standard Library Tutorial PDF
C Standard Library Tutorial PDF
C is the most widely used computer language that keeps fluctuating at number one scale
of popularity along with Java programming language which is also equally popular and
most widely used among modern software programmers.
The C Standard Library is a set of C built-in functions, constants and header files
like <assert.h>, <ctype.h>, etc. This library will work as a reference manual for C
programmers.
Audience
The C Standard Library is a reference for C programmers to help them in their projects
related to system programming. All the C functions have been explained in a user-friendly
way and they can be copied and pasted in your C projects.
Prerequisites
A basic understanding of the C Programming language will help you in understanding the
C built-in functions covered in this library.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
C Standard Library
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
Introduction ............................................................................................................................................ 1
Introduction ............................................................................................................................................ 3
Introduction .......................................................................................................................................... 26
Introduction .......................................................................................................................................... 31
Introduction .......................................................................................................................................... 34
Introduction .......................................................................................................................................... 37
ii
C Standard Library
Introduction .......................................................................................................................................... 45
Introduction .......................................................................................................................................... 68
Introduction .......................................................................................................................................... 72
Introduction .......................................................................................................................................... 78
Introduction .......................................................................................................................................... 83
iii
C Standard Library
Introduction .......................................................................................................................................... 87
iv
C Standard Library
1. C LIBRARY ─ <ASSERT.H>
Introduction
The assert.h header file of the C Standard Library provides a macro called assert which can
be used to verify assumptions made by the program and print a diagnostic message if this
assumption is false.
The defined macro assert refers to another macro NDEBUG which is not a part of
<assert.h>. If NDEBUG is defined as a macro name in the source file, at the point where
<assert.h> is included, the assert macro is defined as follows:
Library Macros
Following is the only function defined in the header assert.h:
This is actually a macro and not a function, which can be used to add diagnostics
in your C program.
Declaration
Following is the declaration for assert() Macro.
Parameters
expression -- This can be a variable or any C expression. If expression evaluates to
TRUE, assert() does nothing. If expression evaluates to FALSE, assert() displays an
5
C Standard Library
error message on stderr (standard error stream to display error messages and
diagnostics) and aborts program execution.
Return Value
This macro does not return any value.
Example
The following example shows the usage of assert() macro:
#include <assert.h>
#include <stdio.h>
int main()
{
int a;
char str[50];
return(0);
}
Let us compile and run the above program in the interactive mode as shown below:
6
C Standard Library
2. C LIBRARY ─ <CTYPE.H>
Introduction
The ctype.h header file of the C Standard Library declares several functions that are useful
for testing and mapping characters.
All the functions accepts int as a parameter, whose value must be EOF or representable as
an unsigned char.
All the functions return non-zero (true) if the argument c satisfies the condition described,
and zero (false) if not.
Library Functions
Following are the functions defined in the header ctype.h:
int isalnum(int c)
1
This function checks whether the passed character is alphanumeric.
int isalpha(int c)
2
This function checks whether the passed character is alphabetic.
int iscntrl(int c)
3
This function checks whether the passed character is control character.
int isdigit(int c)
4
This function checks whether the passed character is decimal digit.
int isgraph(int c)
5 This function checks whether the passed character has graphical representation
using locale.
6 int islower(int c)
7
C Standard Library
int isprint(int c)
7
This function checks whether the passed character is printable.
int ispunct(int c)
8
This function checks whether the passed character is a punctuation character.
int isspace(int c)
9
This function checks whether the passed character is white-space.
int isupper(int c)
10
This function checks whether the passed character is an uppercase letter.
int isxdigit(int c)
11
This function checks whether the passed character is a hexadecimal digit.
int isalnum(int c)
Description
The C library function void isalnum(int c) checks if the passed character is alphanumeric.
Declaration
Following is the declaration for isalnum() function.
Parameters
c -- This is the character to be checked.
Return Value
This function returns non-zero value if c is a digit or a letter, else it returns 0.
Example
The following example shows the usage of isalnum() function.
#include <stdio.h>
#include <ctype.h>
8
C Standard Library
int main()
{
int var1 = 'd';
int var2 = '2';
int var3 = '\t';
int var4 = ' ';
if( isalnum(var1) )
{
printf("var1 = |%c| is alphanumeric\n", var1 );
}
else
{
printf("var1 = |%c| is not alphanumeric\n", var1 );
}
if( isalnum(var2) )
{
printf("var2 = |%c| is alphanumeric\n", var2 );
}
else
{
printf("var2 = |%c| is not alphanumeric\n", var2 );
}
if( isalnum(var3) )
{
printf("var3 = |%c| is alphanumeric\n", var3 );
}
else
{
printf("var3 = |%c| is not alphanumeric\n", var3 );
}
if( isalnum(var4) )
{
9
C Standard Library
return(0);
}
Let us compile and run the above program to produce the following result:
int isalpha(int c)
Description
The C library function void isalpha(int c) checks if the passed character is alphabetic.
Declaration
Following is the declaration for isalpha() function.
Parameters
c -- This is the character to be checked.
Return Value
This function returns non-zero value if c is an alphabet, else it returns 0.
Example
The following example shows the usage of isalpha() function.
#include <stdio.h>
#include <ctype.h>
10
C Standard Library
int main()
{
int var1 = 'd';
int var2 = '2';
int var3 = '\t';
int var4 = ' ';
if( isalpha(var1) )
{
printf("var1 = |%c| is an alphabet\n", var1 );
}
else
{
printf("var1 = |%c| is not an alphabet\n", var1 );
}
if( isalpha(var2) )
{
printf("var2 = |%c| is an alphabet\n", var2 );
}
else
{
printf("var2 = |%c| is not an alphabet\n", var2 );
}
if( isalpha(var3) )
{
printf("var3 = |%c| is an alphabet\n", var3 );
}
else
{
printf("var3 = |%c| is not an alphabet\n", var3 );
}
if( isalpha(var4) )
{
11
C Standard Library
return(0);
}
Let us compile and run the above program to produce the following result:
int iscntrl(int c)
Description
The C library function void iscntrl(int c) checks if the passed character is a control character.
According to standard ASCII character set, control characters are between ASCII codes 0x00
(NUL), 0x1f (US), and 0x7f (DEL). Specific compiler implementations for certain platforms
may define additional control characters in the extended character set (above 0x7f).
Declaration
Following is the declaration for iscntrl() function.
Parameters
c -- This is the character to be checked.
Return Value
This function returns non-zero value if c is a control character, else it returns 0.
Example
The following example shows the usage of iscntrl() function.
#include <stdio.h>
12
C Standard Library
#include <ctype.h>
int main ()
{
int i = 0, j = 0;
char str1[] = "all \a about \t programming";
char str2[] = "tutorials \n point";
Let us compile and run the above program to produce the following result:
all tutorials
int isdigit(int c)
Description
The C library function void isdigit(int c) checks if the passed character is a decimal digit
character.
13
C Standard Library
Declaration
Following is the declaration for isdigit() function.
Parameters
c -- This is the character to be checked.
Return Value
This function returns non-zero value if c is a digit, else it returns 0.
Example
The following example shows the usage of isdigit() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 'h';
int var2 = '2';
if( isdigit(var1) )
{
printf("var1 = |%c| is a digit\n", var1 );
}
else
{
printf("var1 = |%c| is not a digit\n", var1 );
}
if( isdigit(var2) )
{
printf("var2 = |%c| is a digit\n", var2 );
}
else
{
printf("var2 = |%c| is not a digit\n", var2 );
14
C Standard Library
return(0);
}
Let us compile and run the above program to produce the following result:
int isgraph(int c)
Description
The C library function void isgraph(int c) checks if the character has graphical
representation.
The characters with graphical representations are all those characters that can be printed
except for whitespace characters (like ' '), which is not considered as isgraph characters.
Declaration
Following is the declaration for isgraph() function.
Parameters
c -- This is the character to be checked.
Return Value
This function returns non-zero value if c has a graphical representation as character, else it
returns 0.
Example
The following example shows the usage of isgraph() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = '3';
15
C Standard Library
if( isgraph(var1) )
{
printf("var1 = |%c| can be printed\n", var1 );
}
else
{
printf("var1 = |%c| can't be printed\n", var1 );
}
if( isgraph(var2) )
{
printf("var2 = |%c| can be printed\n", var2 );
}
else
{
printf("var2 = |%c| can't be printed\n", var2 );
}
if( isgraph(var3) )
{
printf("var3 = |%c| can be printed\n", var3 );
}
else
{
printf("var3 = |%c| can't be printed\n", var3 );
}
return(0);
}
Let us compile and run the above program to produce the following result:
16
C Standard Library
int islower(int c)
Description
The C library function int islower(int c) checks whether the passed character is a lowercase
letter.
Declaration
Following is the declaration for islower() function.
Parameters
c -- This is the character to be checked.
Return Value
This function returns a non-zero value(true) if c is a lowercase alphabetic letter else, zero
(false).
Example
The following example shows the usage of islower() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 'Q';
int var2 = 'q';
int var3 = '3';
if( islower(var1) )
{
printf("var1 = |%c| is lowercase character\n", var1 );
}
else
{
printf("var1 = |%c| is not lowercase character\n", var1 );
}
if( islower(var2) )
17
C Standard Library
{
printf("var2 = |%c| is lowercase character\n", var2 );
}
else
{
printf("var2 = |%c| is not lowercase character\n", var2 );
}
if( islower(var3) )
{
printf("var3 = |%c| is lowercase character\n", var3 );
}
else
{
printf("var3 = |%c| is not lowercase character\n", var3 );
}
return(0);
}
Let us compile and run the above program to produce the following result:
int isprint(int c)
Description
The C library function int isprint(int c) checks whether the passed character is printable. A
printable character is a character that is not a control character.
Declaration
Following is the declaration for isprint() function.
Parameters
c -- This is the character to be checked.
18
C Standard Library
Return Value
This function returns a non-zero value(true) if c is a printable character else, zero (false).
Example
The following example shows the usage of isprint() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 'k';
int var2 = '8';
int var3 = '\t';
int var4 = ' ';
if( isprint(var1) )
{
printf("var1 = |%c| can be printed\n", var1 );
}
else
{
printf("var1 = |%c| can't be printed\n", var1 );
}
if( isprint(var2) )
{
printf("var2 = |%c| can be printed\n", var2 );
}
else
{
printf("var2 = |%c| can't be printed\n", var2 );
}
if( isprint(var3) )
{
19
C Standard Library
return(0);
}
Let us compile and run the above program to produce the following result:
int ispunct(int c)
Description
The C library function int ispunct(int c) checks whether the passed character is a
punctuation character. A punctuation character is any graphic character (as in isgraph) that
is not alphanumeric (as in isalnum).
Declaration
Following is the declaration for ispunct() function.
Parameters
20
C Standard Library
Return Value
This function returns a non-zero value(true) if c is a punctuation character else, zero (false).
Example
The following example shows the usage of ispunct() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 't';
int var2 = '1';
int var3 = '/';
int var4 = ' ';
if( ispunct(var1) )
{
printf("var1 = |%c| is a punctuation character\n", var1 );
}
else
{
printf("var1 = |%c| is not a punctuation character\n", var1 );
}
if( ispunct(var2) )
{
printf("var2 = |%c| is a punctuation character\n", var2 );
}
else
{
printf("var2 = |%c| is not a punctuation character\n", var2 );
}
if( ispunct(var3) )
{
21
C Standard Library
return(0);
}
Let us compile and run the above program that will produce the following result:
int isspace(int c)
Description
The C library function int isspace(int c) checks whether the passed character is white-space.
22
C Standard Library
Declaration
Following is the declaration for isspace() function.
Parameters
c -- This is the character to be checked.
Return Value
This function returns a non-zero value(true) if c is a white-space character else, zero (false).
23
C Standard Library
24