Вы находитесь на странице: 1из 19

Standard Libraries

C Programming

Content
Appreciate C standard libraries Appreciate and use standard I/O library Appreciate and use character type library Appreciate and use mathematics library Appreciate and use standard utility library

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

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 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 functions are supported on one platform, and not on other

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

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 used for integer input/output. Following table gives the summary of conversion specifiers for different data types to print using printf and to get as input using scanf.

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

Conversion specifiers table in C


Conversion specifier Data Type Integer decimal number Unsigned octal number Hexadecimal number , using a,b,c,d,e,f or A,B,C,D,E,F Unsigned decimal number Int;unsigned single character char *; print characters from the string until '\0' is found double Pointer variable

%d, %i %o %x, or %X %u %c %s

%f %p

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

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 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 it seeing the comments.

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

Sprintf and sscanf-example


#include<stdio.h> int main() { float f1=766.98; char c1='z'; int i1=212; int i4; char c2; 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 they are 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; }
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value Restricted Aricent Group 2011 7

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 ) isalpha(int c) iscntrl(int c ) isdigit(int c) isgraph(int c) islower(int c) isupper(int c) isspace(int c) isxdigit(int c) // It checks whether a character is alphanumeric or not? // checks whether a character is alphabetic or not // checks whether a character is control character or not // checks whether a character is digit or not // checks whether the character is printable character or not (except space) // checks whether the character is lowercase English character or not // checks whether the character is uppercase English character or not // checks whether the character is space or not // 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 character itself toupper(int c) // It converts the character to uppercase if possible otherwise result is input character itself
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value Restricted Aricent Group 2011 8

tolower(int c) // it converts the character to lowercase if possible otherwise result is input

ctype.h - Example
Following 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; printf("Original string: \"%s\"\n", str); 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; }

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

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

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

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

10

math.h
Following program just uses above mentioned functions on specific number for illustration. #include<stdio.h> #include<math.h> int main() { double d=9.7; printf("the sin value of the numer is =%f\n",sin(d)); 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; }

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

11

stdlib.h
stdlib.h is the header of the general purpose standard library of C programming language 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 null pointer constant, and represents a pointer value that is guaranteed not to point to a valid address in memory. NULL may be defined as a constant expression equal to int zero, long int zero, or zero cast to a void * pointer: #define NULL 0 #define NULL 0L #define NULL ((void *) 0)

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

12

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 operator evaluates to size_t.

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.

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

13

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 least 32767 // 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.

srand()

See the examples in coming slide for illustration of these functions.

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

14

stdlib.h Example ( atoi() function)


int main() { int result; char *test1 = "310"; char *test2 = "No Number"; result = atoi (test1); printf("Test1 is %d\n", result); // It will print 310 as an integer. result = atoi(test2); // Here result is unpredictable as atoi doesnt check for errors. printf("Test2 is %d\n", result); }

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

15

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 () { int total = 0, die, nd; for (nd = 0; nd < 3; nd++) { die = dieroll (); // using the loop, three random values are generated every time the program is run. But these are repeated between the calls of the program . E.g. if 4,1,3 is the pattern in first run of the program, that would be repeated in second run, 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); }
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value Restricted Aricent Group 2011 16

Rand and srand


We 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; 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; }
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value Restricted Aricent Group 2011 17

Here time(0) function is returning new value each time the program is called and 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 to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. But since our seed value is different each time we shall get different values every time.

Disclaimer
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 for which Aricent Group assumes no responsibility for the availability or content 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

The contents here are for Aricent Group internal training purposes only and do not carry any commercial value

Restricted Aricent Group 2011

18

Thank You

Вам также может понравиться