c standard lib ref
TRANSCRIPT
-
8/13/2019 C Standard Lib Ref
1/19
C Programming
-
8/13/2019 C Standard Lib Ref
2/19
Content
Appreciate C standard libraries
Appreciate and use standard I/O library
Appreciate and use character type library
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 2Restricted Aricent Group 2011
pprec a e an use ma ema cs rary
Appreciate and use standard utility library
-
8/13/2019 C Standard Lib Ref
3/19
Standard Library What is meant by standard library?
It has header files and library files of standard functions e.g printf, scanf.
What is there in a header file?
It usually contains the prototypes of different functions, definition of macros and prototype
declaration of user defined data types.
What is there in Library files?
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 3Restricted Aricent Group 2011
The definitions of the functions and variables
Is standard library compiler dependant?
Yes , though most of the functions are provided by every compiler , yet there are some functionsare supported on one platform, and not on other
-
8/13/2019 C Standard Lib Ref
4/19
stdio.h stdio.h provides many input/output functions in C .
printf and scanf are two functions which are used heavily in c for
input/output.
printf and scanf use different conversion specifiers for different
data types .e.g. %c is used for character input/output while %d is
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 4Restricted Aricent Group 2011
use or n eger npu ou pu .
Following table gives the summary of conversion specifiers for
different data types to print using printf and to get as input using
scanf.
-
8/13/2019 C Standard Lib Ref
5/19
Conversion specifiers table in C
%d, %i Integer decimal number
%o Unsigned octal number%x, or %X Hexadecimal number , using
a,b,c,d,e,f or A,B,C,D,E,F
%u Unsigned decimal number
Conversion specifier Data Type
Restricted Aricent Group 2011The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 5Restricted Aricent Group 2011
%cInt;unsigned single character
%s char *; print characters from the stringuntil '\0'
is found
%f double
%p Pointer variable
-
8/13/2019 C Standard Lib Ref
6/19
Stdio.h
Whats the difference between sprintf and printf ?
printf writes the output on the console while sprintf writes the output
in some string.
What is the difference between sscanf and scanf?
scanf takes the input from the keybord or standard input device and
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 6Restricted Aricent Group 2011
stores those values in to some variables, while sscanf takes values
from some string and stores them in the variables
Program on the next slide illustrate the working of sscanf and sprintf. Go through itseeing the comments.
-
8/13/2019 C Standard Lib Ref
7/19
Sprintf and sscanf-example
#include
int main()
{
float f1=766.98;
char c1='z';
int i1=212;
int i4;
char c2;
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 7Restricted Aricent Group 2011
float f2;char string1[30];
char string[30];
sprintf(string,"%d %f %s",i1,f1,"amit"); /* here values of i1,f1 and amit
will be copied in the string*/
sscanf(string,"%d%f%s",&i4, &f2, string1); /* here values are read back from string and theyare copied in i4, f2 and string1 */
printf("string is: %s\n",string);
printf("\nafter scanf we got %d %f %s\n ",i4,f2,string1);
return 0;
}
-
8/13/2019 C Standard Lib Ref
8/19
ctype.h Functions in ctype.h checks the type of character or converts its type from one type to
another. Let us consider each of them
isalnum(int c ) // It checks whether a character is alphanumeric or not?
isalpha(int c) // checks whether a character is alphabetic or not iscntrl(int c ) // checks whether a character is control character or not
isdigit(int c) // checks whether a character is digit or not
isgraph(int c) // checks whether the character is printable character or
not (except space)
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 8Restricted Aricent Group 2011
islower(int c) // checks whether the character is lowercase Englishcharacter or not
isupper(int c) // checks whether the character is uppercase English
character or not
isspace(int c) // checks whether the character is space or not
isxdigit(int c) // checks whether the character is hexadecimal digit or not i.e if character is any of {0,1,.9, A, B, C, D, E, F, a,b,c,d,e,f } or not
tolower(int c) // it converts the character to lowercase if possible otherwise result is input
character itself
toupper(int c) // It converts the character to uppercase if possible otherwise result is input
character itself
-
8/13/2019 C Standard Lib Ref
9/19
ctype.h - ExampleFollowing program illustrate the working of two functions of ctype .h , toupper and
tolower. All other functions can be used in same way. Program takes a string and
first converts all of it characters in lowercase letters and then in uppercase letters.
int main (){
char *str = "Moral indignation is jealousy with a halo. - H. G. Wells";
char *p;
rintf "Ori inal strin : \"%s\"\n", str ;
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 9Restricted Aricent Group 2011
printf("After tolower: \"");for (p = str; *p; p++)
printf("%c", tolower (* p));
printf("\"\n");
printf("After toupper: \"");
for (p = str; *p; p++)printf("% c", toupper (* p));
printf ("\"\n");
return 0;
}
-
8/13/2019 C Standard Lib Ref
10/19
math.h As the name suggests math.h have the functions related to mathematical Functions.
Some of them have been listed here with their prototypes. The names of most of the
functions describe the functionality of them. However some description is given for
clarification.
double sin(double x) // x is in radian here. It returns the sin value of x
double cos(double x) // cos value is returned
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 10Restricted Aricent Group 2011
double sqrt(double x) // square root value of x is obtained
double exp(double x) // It returns value of e ( base of natural logarithm) raised to
the power x
double pow(double x,double y) // It returns value of x raised to the power y
-
8/13/2019 C Standard Lib Ref
11/19
math.h Following program just uses above mentioned functions on specific number for
illustration.
#include
#include
int main()
{
double d=9.7;
rintf "the sin value of the numer is =%f\n",sin d ;
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 11Restricted Aricent Group 2011
printf("the cos value of the numer is =%f\n",cos(d));printf("the square root value of the numer is =%f\n",sqrt(d));
printf("the value of the 'e' raised to the power %f is =%f\n",d,exp(d));
printf("the value of the 2.5 raised to the power is =%f\n",pow(2.5,d));
return 0;
}
-
8/13/2019 C Standard Lib Ref
12/19
stdlib.h
stdlib.h is the header of the general purpose standard library of C programminglanguage which includes functions involving memory allocation, process control,conversions and others.
Member Constants
The stdlib.h and stddef.h header files define the macro NULL, which yields a nullpointer constant, and represents a pointer value that is guaranteed not to point to avalid address in memory.
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 12Restricted Aricent Group 2011
, ,
cast to a void * pointer:
#define NULL 0
#define NULL 0L
#define NULL ((void *) 0)
-
8/13/2019 C Standard Lib Ref
13/19
stdlib.h
Member Data Types
A data type called size_t is defined in the stdlib.h library, which is used to
represent the size of an object.
Library functions that take sizes expect them to be of type size_t, and the sizeof
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 13Restricted Aricent Group 2011
opera or eva ua es o s ze_ .
The actual type of size_t is platform-dependent; a common mistake is to
assume size_t is the same as unsigned int, which can lead to programming
errors.
-
8/13/2019 C Standard Lib Ref
14/19
stdlib.h Some of the very important functions provided by stdlib.h are as follows atoi() // It converts a string to a integer like 123 will be converted 123 (read as
an integer).
itoa() // It does the reverse i.e. it converts the integer to the string
rand() // rand function return the random number between 0 to RAND_MAX.
Value of RAND_MAX depends on the library. However it is assumed at
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 14Restricted Aricent Group 2011
srand() // If we use rand() function in isolation it will generate same sequence of
random numbers. So to get different sequence every time we use srand()
along with rand() . srand takes seed value as an argument. If seed value
is different then sequence of numbers will be different. This thing is
explained with the help of the program in coming slide.
See the examples in coming slide for illustration of these functions.
-
8/13/2019 C Standard Lib Ref
15/19
stdlib.h Example ( atoi() function)int main()
{
int result;
char *test1 = "310";
char *test2 = "No Number";
result = atoi (test1);
" "
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 15Restricted Aricent Group 2011
, .
result = atoi(test2); // Here result is unpredictable as atoi doesnt
check for errors.
printf("Test2 is %d\n", result);
}
-
8/13/2019 C Standard Lib Ref
16/19
Stdlib.h Example ( rand and srand
function)int dieroll ()
{
return ((rand () % 6) + 1); // dieroll function returns the random value between 1- 6
whenever it is called}
int main ()
{
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 16Restricted Aricent Group 2011
, ,
for (nd = 0; nd < 3; nd++)
{
die = dieroll (); // using the loop, three random values are generated every time theprogram is run. But these are repeated between the calls of the program . E.g. if4,1,3 is the pattern in first run of the program, that would be repeated in secondrun, third run so on.
total += die;
printf (" Die roll %d: %d\n", nd + 1, die);
}
printf ("\nThe total of the three dice is %d.\n", total);
}
-
8/13/2019 C Standard Lib Ref
17/19
Rand and srandWe can remove the anomaly in previous program using srand function shown below.
int dieroll ()
{
return ((rand () % 6) + 1);
}int main ()
{
int total = 0, die, nd;
Here time(0) function is returning new
value each time the program is calledand that new value is passed as an
argument to srand.The srand() function
sets its argument as the seed for a new
Sequence of pseudo-random integers
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 17Restricted Aricent Group 2011
srand(time(0));
for (nd = 0; nd < 3; nd++)
{
die = dieroll ();
total += die;
printf (" Die roll %d: %d\n", nd + 1, die);
}
printf ("\nThe total of the three dice is %d.\n", total);
return 0;
}
.
These sequences are repeatable bycalling srand() with the same seed value.
But since our seed value is different
each time we shall get different values
every time.
-
8/13/2019 C Standard Lib Ref
18/19
Aricent Group makes no representations or warranties with respect to
contents of these slides and the same are being provided as is. The
content/materials in the slides are of a general nature and are not intended to address the specific circumstances of any particular
individual or entity. The material may provide links to internet sites (for
the convenience of users) over which Aricent Group has no control and
Disclaimer
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value 18Restricted Aricent Group 2011
for which Aricent Group assumes no responsibility for the availability orcontent of these external sites. While the attempt has been to
acknowledge sources of materials wherever traceable to an individual or
an institution; any materials not specifically acknowledged is purely
unintentional
-
8/13/2019 C Standard Lib Ref
19/19
an ou