C Library Functions
C Library Functions
STRING HANDLING
SIGNALS
TIME
CTYPE
C LIBRARY FUNCTIONS
MATH
LIMITS
STANDARD INPUT/OUTPUT
STANDARD LIBRARY
strlen( string.h )
Prototype int strlen ( const char *string ); int length; char buffer[20]; strcpy(buffer, Amrita University); length = strlen(buffer); printf(\n The number of characters in buffer = %d, length ); Note : The length does not include the null character (\0) that terminates the string
strcmp ( string.h )
Prototype int strcmp(const char * string1, const char *string2); char string1[25]; char string2[25]; int result; result = strcmp ( string1, string2 ); if ( result > 0 ) printf ( string1 is greater than string2 ); else if ( result < 0 ) printf (string1 is lesser than string2 ); else printf (The 2 strings are equal);
strcpy ( string.h )
Prototype char* strcpy ( char * dest, const char * src ); char src[25] = AMRITA UNIVERSITY; char dest[25]; strcpy(dest, src); Note : We need to ensure that the destination string has enough memory to accommodate the source string including its null character
strcat ( string.h )
Prototype char* strcat(char *dest, const char *src); char string1[30] = Amrita; char string2[30] = Vishwa Vidyapeetham; strcat(string1, ); strcat(string1, string2); printf(\n The concatenated string is %s, string1);
memset ( string.h )
Prototype void * memset ( void *ptr, int value, int num ); char str[] = All programmers know memset!; memset (str,'-',3); puts (str); int array[10]; memset(array, 0, 40);
memcpy ( string.h )
Prototype void * memcpy(void *dest, const void *src, int n); int u[5] = {1, 2, 3, 4, 5}; int v[5]; memcpy(v, u, 5*sizeof(int));
memmove ( string.h )
Prototype void * memmove(void *dest, const void *src, int n); int u[6] = {1, 2, 3, 4, 5}; memmove(u + 1, u, 5*sizeof(int));
memcpy Vs memmove
int u[6] = {1, 2, 3, 4, 5}; memcpy(u + 1, u, 5*sizeof(int)); output 111111 int u[6] = {1, 2, 3, 4, 5}; memmove(u + 1, u, 5*sizeof(int)); output 112345
fgets ( stdio.h )
Prototype char *fgets(char *str, int num, FILE *stream); #include <stdio.h> int main() { FILE * pFile; char mystring [100]; pFile = fopen ("myfile.txt" , "r");
fgets ( stdio.h )
if (pFile == NULL) perror ("Error opening file"); else { fgets (mystring , 100 , pFile); puts (mystring); fclose (pFile); } return 0; }
fputs ( stdio.h )
Prototype int fputs(const char *str, FILE *stream); #include <stdio.h> int main () { FILE * pFile; char sentence [256]; printf ("Enter sentence to append: ");
fputs ( contd. )
fgets (sentence,255,stdin); pFile = fopen ("mylog.txt","a"); fputs (sentence,pFile); fclose (pFile); return 0; }
feof ( stdio.h )
Prototype int feof(FILE *stream); char ch; ch = fgetc(fp); while ( ch != EOF ) { ch = fgetc(fp); } if ( feof(fp) ) printf(\n End of file reached);
feof ( contd. )
feof should be used after other file functions return. For e.g., after fgetc returns EOF we can confirm it by calling feof as indicated by the code snippet on the previous page. Similarly fgets returns null on EOF as well as some other error conditions such as unable to read input file. In such cases feof and ferror can be useful in identifying if it is an EOF condition (normal) or some other erroneous (error) condition.
ferror( stdio.h )
Prototype int ferror(FILE *stream); while (fgets(buffer, MAX_SIZE, fp)!=NULL){ } if ( ferror(fp) ) { printf(\n File cannot be read due to error); }
ferror ( contd. )
After the return of a file I/O function such as fgets we can use the ferror function to determine whether EOF condition was reached (or) if it was an error
sscanf ( stdio.h )
Prototype int sscanf(const char *s, const char *fmt, );
#include <stdio.h> int main() { char buffer[80], name[25]; int age; float salary; printf("\n Enter Employee Name Age and Salary :"); gets(buffer); sscanf(buffer, "%s %d %f", name, &age, &salary); printf("\nEmployee Name; %s\tAge: %d \tSalary: %f", name, age, salary); return 0; }
fseek ( stdio.h )
Prototype int fseek ( FILE *stream, long int offset, int origin ); offset is the offset from origin ( in bytes ) origin can be SEEK_SET ( beginning ) SEEK_CUR ( current position ) SEEK_END ( end )
fseek ( contd. )
#include <stdio.h> int main() { FILE *fp; fp = fopen("input", "r"); fseek(fp, 10, SEEK_SET); printf("\n Character read is %c %ld", fgetc(fp), ftell(fp)); fseek(fp, -5, SEEK_END); printf("\n Character read is %c %ld", fgetc(fp), ftell(fp)); return 0; }
ftell ( stdio.h )
Prototype long int ftell ( FILE * stream ); #include <stdio.h> int main () { FILE * pFile; long size; pFile = fopen ("myfile.txt","rb");
ftell ( contd. )
if (pFile==NULL) perror ("Error opening file"); else { fseek (pFile, 0, SEEK_END); size=ftell (pFile); fclose (pFile); printf ("Size of myfile.txt: %ld bytes.\n",size); } return 0; }
rewind ( stdio.h )
Prototype void rewind ( FILE * stream ); #include <stdio.h> int main () { int n; FILE * pFile; char buffer [27]; pFile = fopen ("myfile.txt","w+");
rewind ( contd. )
for ( n='A' ; n<='Z' ; n++) fputc ( n, pFile); rewind (pFile); fread (buffer,1,26,pFile); fclose (pFile); buffer[26]='\0'; puts (buffer); return 0; }
calloc ( stdlib.h )
Prototype void * calloc (int num, int size); #include <stdio.h> #include <stdlib.h> int main () { int i,n; int * pData; printf ("Amount of numbers to be entered: "); scanf ("%d",&i); pData = (int*) calloc (i,sizeof(int)); if (pData==NULL) exit (1);
calloc ( contd. )
for (n=0;n<i;n++) { printf ("Enter number #%d: ",n); scanf ("%d",&pData[n]); } printf ("You have entered: "); for (n=0;n<i;n++) printf ("%d ",pData[n]); free (pData); return 0;
realloc ( stdlib.h )
Prototype void * realloc (void * ptr, int size ); #include <stdio.h> #include <stdlib.h> int main () { int input,n; int count=0; int * numbers = NULL; do { printf ("Enter an integer value (0 to end): "); scanf ("%d", &input); count++; numbers = (int*) realloc (numbers, count * sizeof(int));
realloc ( contd. )
if (numbers==NULL) { puts ("Error (re)allocating memory"); exit (1); } numbers[count-1]=input; } while (input!=0); printf ("Numbers entered: "); for (n=0;n<count;n++) printf ("%d ",numbers[n]); free (numbers); return 0;
atoi ( stdlib.h )
Prototype int atoi(const char *s); Converts a string into the equivalent integer. For e.g., String 100 becomes the integer 100 #include <stdio.h> #include <stdlib.h> int main() { char buffer[100]; int number; scanf("%s", buffer); number = atoi(buffer); printf("\n %d", number);
atof ( stdlib.h )
Prototype float atof(const char *s); Converts a string into the equivalent floating point number. For e.g., String 100.23 becomes the floating point number 100.23
atof ( contd. )
#include <stdio.h> #include <stdlib.h> int main() { char buffer[100]; double number; scanf("%s", buffer); number = atof(buffer); printf("\n %lf", number); return 0; }
pow ( math.h )
Prototype double pow(double base, int exponent); #include <stdio.h> #include <math.h> int main () { printf ("7 ^ 3 = %lf\n", pow (7,3)); printf ("4.73 ^ 12 = %lf\n", pow (4.73,12)); printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54)); return 0; }
sqrt ( math.h )
Prototype double sqrt(double number); #include <stdio.h> #include <math.h> int main () { double param, result; param = 1024.0; result = sqrt (param); printf ("sqrt(%lf) = %lf\n", param, result ); return 0; }
floor ( math.h )
Prototype double floor (double number); #include <stdio.h> #include <math.h> int main () { printf ("floor of 2.3 is %.1lf\n", floor (2.3) ); printf ("floor of 3.8 is %.1lf\n", floor (3.8) ); printf ("floor of -2.3 is %.1lf\n", floor (-2.3) ); printf ("floor of -3.8 is %.1lf\n", floor (-3.8) ); return 0; }
ceil ( math.h )
Prototype double ceil (double number); #include <stdio.h> #include <math.h> int main () { printf ("ceil of 2.3 is %.1lf\n", ceil (2.3) ); printf ("ceil of 3.8 is %.1lf\n", ceil (3.8) ); printf ("ceil of -2.3 is %.1lf\n", ceil (-2.3) ); printf ("ceil of -3.8 is %.1lf\n", ceil (-3.8) ); return 0; }
tolower ( ctype.h )
Prototype int tolower ( int ch ); char ch; ch = A; ch = tolower(ch); Printf(\n The character converted to lowercase = %c, ch);
toupper ( ctype.h )
Prototype int toupper ( int ch ); char ch; ch = u; ch = toupper(ch); printf(\n The character converted to uppercase = %c, ch);
assert ( assert.h )
Prototype void assert ( int expression ); FILE *fp; fp = fopen (input.txt, r); assert( fp != NULL ); Note : If the expression evaluates to FALSE then it will throw an error message Assertion Failed
offsetof ( stddef.h )
Macro offsetof(type, member) struct node { char a; short int b; int c; }; printf("\n Offset of a = %d", offsetof(struct node, a)); printf("\n Offset of b = %d", offsetof(struct node, b)); printf("\n Offset of d = %d", offsetof(struct node, c));
References
http://www.cplusplus.com/reference/clibrary/ http://www.eskimo.com/~scs/cclass/notes/top.html http://www.eskimo.com/~scs/cclass/int/top.html