Appendix C

Header Files and Standard Library Functions

Every C compiler provides the standard library functions. The following table describes few C library functions that can be used by programmers’ in their programs. Since standard library functions are huge in numbers, it is not possible to include all of them in this appendix. Programmer can refer to the help of the C compiler. Functions supported by compilers may vary from compiler to compiler.

Header files and functions supported by a compiler are as under.

  1. stdio.h
    Function Name & Syntax Description Examples
    printf:
    int printf(const char *format, argument, …);
    It applies each argument to corresponding format specifier in *format and sends formatted output to stdin. If successful, returns non-zero; otherwise EOF#.
    • printf("%d",d): prints value of d. (65)
    • printf("%c",65): prints ASCII character of 65 i.e. ‘A’
    • printf("s=%f, t=%d", s, t): prints float value of s and int value of t. (s=4.5, t=2)
    scanf:
    int scanf (const char *format, address, …);
    It scans a series of input fields one character at a time, format each field according to a corresponding format specifier in *format and stores the formatted input at an address passed as an argument. If successful, returns number of fields scanned; returns zero, if no fields are stored. Returns EOF# when it attempts to read end of file.
    • scanf("%d",&s): reads the value for s and stores in s.
    • scanf("%d%c",&i,&c): reads value for i as int and value for c as char and stores in i and c.
    • scanf("%s",p): reads string for p and stores in char array p.
    gets: char *gets(char *s); It collects a string of characters terminated by a new line from the standard input stream stdin and puts it into s by replacing newline by null character (). It allows whitespaces in string. If successful, returns string s otherwise null. If we enter "C language" in screen for input;
    • scanf only takes "C" as string
    • gets takes "C Language" as string
    • gets(a): entered string will be stored in char array a.
    puts:
    int puts(const char *s);
    It copies the null-terminated string s to the standard output stream stdout and appends a newline character. If successful, returns non-zero; otherwise EOF#. puts(a): The entire string with whitespaces stored in char array will get printed on screen.
    fopen:
    FILE *fopen(const char *filename, const char *mode);
    fopen opens a file and associate a stream with it and returns a pointer that identifies the stream in subsequent operations. Mode indicates in which mode a file will be accessed (r,w,a).
    • FILE *fp=fopen("a.txt", "r"): opens file a.txt in read mode.
    • FILE *fc=fopen("s.C","w"): opens file s.C in write mode.
    • FILE *fd=fopen("p.doc","a"): opens file p.doc in append mode.
    fclose:
    int fclose(FILE *stream);
    fclose closes the named stream and returns 0 on success otherwise EOF#. All buffers associated with the stream are flushed before closing. System-allocated buffers are freed upon closing. fclose(fp);
    fclose(fc);
    fclose(fd);
    fprintf:
    int fprintf(FILE *stream, const char *format, argument, …);
    It applies each argument to corresponding format specifier in *format and sends formatted output to stream. If successful, returns non-zero; otherwise EOF#. fprintf(fp,"s=%f, t=%d", s, t);
    Writes "s=4.5, t=2" to a file indicated by fp.
    fscanf:
    int fscanf(FILE *stream, const char *format, address, …);
    It scans a series of input fields one character at a time from a stream, format each field according to a corresponding format specifier in *format and stores the formatted input at an address passed as an argument. If successful, returns number of fields scanned; returns zero, if no fields are stored. Returns EOF# when it attempts to read end-of-file. fscanf(fp,"%s",&str);
    Reads a string from a file indicated by fp and stores it in char array str.
    fgets:
    char *fgets(char *s, int n, FILE *stream);
    It reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first. fgets retains the newline character at the end of s and appends a null byte to s to mark the end of the string. fgets returns the string pointed to by s. fgets(str,10,fp);
    Reads 9 characters or newline character, whichever comes first from a file pointed by fp and stores them into char pointer str.
    fputs:
    int fputs(const char *s, FILE *stream);
    fputs copies the null-terminated string s to the given output stream. It does not append a newline character, and the terminating null character is not copied. fputs returns the last character written. fputs(str,fp);
    Writes the characters in string str to a fle pointed by fp.
    fgetc:
    int fgetc(FILE *stream);
    It returns the next character on the named input stream. It returns the character read, after converting it to an int on success; EOF# otherwise. fgetc(fp);
    Reads next character from file indicated by fp.
    fputc:
    int fputc(int c, FILE *stream);
    It outputs character c to the named stream. It returns the character written on success; EOF# otherwise. fputc(65,fp); Writes ‘A’ to a file indicated by fp.
    fflush:
    int fflush(FILE *stream);
    If the given stream has buffered output, fflush writes the output for stream to the associated file.  
    #EOF: end-of-file

     

  2. conio.h
    Function Name & Syntax Description Example
    clrscr:
    void clrscr(void);
    It clears the current text window and places the cursor in the upper left-hand corner (at position 1,1). clrscr();
    getch:
    int getch(void);
    It reads a single character directly from the keyboard, without echoing to the screen and returns the character read from keyboard. getch();

     

  3. alloc.h
    Function Name & Syntax Description Example
    malloc:
    void *malloc(size_t size);
    It allocates a block of size bytes from the memory heap. It allows a program to allocate memory explicitly as it’s needed, and in the exact amounts needed. On success, returns pointer to newly allocated block of memory; otherwise null. char *str=(char *) malloc(10);
    Allocates space for 10 chracters pointed by str. Typecasting is needed here.
    calloc:
    void *calloc(size_t nitems, size_t size);
    calloc provides access to the C memory heap, which is available for dynamic allocation of variable-sized blocks of memory. calloc allocates a block (nitems * size) bytes and clears it to 0. On success, returns pointer to newly allocated block of memory; otherwise null. int *str=(int *) malloc(10, sizeof(int)); Allocates space for 10 chracters as 10*sizeof(int)=10*2=20 bytes pointed by str. Typecasting is also needed here.
    free:
    void free(void *block);
    free deallocates a memory block allocated by a previous call to calloc, malloc, or realloc. free(str);
    Frees the allocated block pointed by str.
    Note: Above three functions are also available in stdlib.h file.

     

  4. math.h
    Function Name & Syntax Description Example
    abs:
    int abs(int x);
    It returns absolute value of its argument. abs(-1);
    returns 1
    ceil:
    double ceil(double x);
    It finds the smallest integer not less than x. ceil(2.6);
    returns 3.0
    floor:
    double floor(double x);
    It finds the largest integer not greater than x. floor(2.6);
    returns 2.0
    exp:
    double exp(double x);
    It calculates the exponential function; e to the xth power. exp(2);
    returns e^2=7.389056
    log:
    double log(double x);
    double log10(double x);
    log calculates the natural logarithm of x while log10 calculates the base 10 logarithm of x. log(5);
    returns 1.609438
    log10(5);
    returns 0.698970
    pow:
    double pow(double x, double y);
    It calculates x**y i.e. x to the power y. pow(2,5);
    returns 32
    Trigonometric:
    double cos(double x);
    double sin(double x);
    double tan(double x);
    cos computes the cosine of the input value. sin compute the sine of the input value. tan calculates the tangent of the input value. The value to be passed as argument must be in radians. cos(30*M_PI/180);
    #returns 0.866025
    sin(30*M_PI/180); returns 0.500000
    cos(30*M_PI/180); returns 0.577350
    sqrt:
    double sqrt(double x);
    It calculates the positive square root of the input value. sqrt(6.25);
    returns 2.500000
    #M_PI=π=3.141593

     

  5. stdlib.h
    Function Name & Syntax Description Example
    atoi:
    int atoi(const char *s);
    It converts a string pointed to by s to int. If s is inconvertible, it returns zero. char *s="123"atoi(s);
    returns an integer 123.
    exit:
    void exit(int status);
    It terminates the calling process. Before termination, it closes all files and writes buffered output. status=0 indicates normal exit and non-zero indicates error.This function is also available in process.h file. exit(0);
    stops the execution of main function hence terminates the program.
    itoa:
    char *itoa(int value, char *string, int radix);
    It converts value to a null-terminated string and stores the result in string. radix specifies the base to be used in converting value (2to36). itoa(123,a,16);
    The hexadecimal string format of 123 i.e. 7b will be stored in string a.
    random:
    int random(int num);
    It returns a random number between 0 and (num-1). random(999);
    may return 10
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.108.176