Академический Документы
Профессиональный Документы
Культура Документы
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
Standard Library
What is meant by standard library?
It has header files and library files of standard functions e.g printf, scanf.
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value
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
%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
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
/* 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
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
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)
double cos(double x)
double sqrt(double x)
double exp(double x)
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value
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
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
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
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()
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()
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value
14
The contents here are for Aricent Group internal training purposes only and do not carry any commercial value
15
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
18
Thank You