Showing posts with label C questions & Answers. Show all posts
Showing posts with label C questions & Answers. Show all posts

Tuesday, 19 December 2017

perror & strerror, convert the perror number into an string value


What is perror ?

The C library function void perror(const char *str) prints a descriptive error message to stderr.
First the string str is printed, followed by a colon then a space.
Str = User string.

what is strerror ?

The strerror() function returns a pointer to a string that describes the error code passed in the argument errnum.
Possibly using the LC_MESSAGES part of the current locale to select the appropriate language. (For example, if errnum is EINVAL, the returned description will "Invalid argument".)
This string must not be modified by the application, but may be modified by a subsequent call to strerror(). No library function, including perror(3), will modify this string.

Return value:

The strerror() and the GNU-specific strerror_r() functions return the appropriate error description string, or an "Unknown error nnn" message if the error number is unknown.

Example:


#include <stdio.h>
#include <errno.h>
#include <string.h>
#define BUF_64              64
int main()
{
    FILE   *fp = NULL;
    char   strerr_buf[BUF_64] = {'\0'};
    int    ret = 0;
    /* first rename if there is any file */
    rename("file.txt", "newfile.txt");
    /* now let's try to open same file */
    fp = fopen("file.txt", "r");
    if (fp == NULL) {
        perror("Error: ");
        ret = strerror_r(errno, strerr_buf, sizeof(strerr_buf));
        if (ret) {
            printf("Failed to convert errno to string ret <%d> \n", ret);
        }
        printf("velraj errno <%d> value (%s) value using _r <%s> \n", errno, strerror(errno), strerr_buf);
        return(-1);
    }
    fclose(fp);
    printf("velraj errno = 2  value (%s)\n",  strerror(2));
    return(0);
}

Output:

Error: : No such file or directory
velraj errno <2> value (No such file or directory) value using _r <No such file or directory>



Friday, 15 December 2017

Days to Learn Advance C language, In debth C questions and answers




Day 1:


  1. Compilation :
                Find the compilation steps from .c format to executable format.
                Collect the files which was generated in each steps
                Analysis the file and understand the purpose of each file.


Day 2:


  1. What are all storage class is available in c,  the scope (visibility) and life-time of variables and/or functions within a C Program.
  2. Difference between static function & normal function.  With example programs.


Day 3:


  1. Memory Layout of C Programs ?
    1. Check the size of text, data, and bss segments of below programs.
      1. Check the following simple C program
                                                           #include <stdio.h>
                                               int main(void)
                                                          {
                                                                return 0;
                                                           }


    1.   Let us add one global variable in program, now check the size of bss (highlighted in red color)
                                                         #include <stdio.h>
                                              int global; /* Uninitialized variable stored in bss*/
                                              int main(void)
                                                         {
                                                             return 0;
                                                         }


  1. Let us add one static variable which is also stored in bss.
                                                        #include <stdio.h>
                                             int global; /* Uninitialized variable stored in bss*/
                                             int main(void)
                                                       {
                                                            static int i; /* Uninitialized static variable stored in bss */
                                                            return 0;
                                                        }

  1. Let us initialize the static variable which will then be stored in Data Segment (DS)
                                            #include <stdio.h>
                                            int global; /* Uninitialized variable stored in bss*/
                                            int main(void)
                                                      {
                                                           static int i = 100; /* Initialized static variable stored in DS*/
                                                           return 0;
                                                       }

  1.  Let us initialize the global variable which will then be stored in Data Segment (DS)
                                                       #include <stdio.h>
                                            int global = 10; /* initialized global variable stored in DS*/
                                            int main(void)
                                                       {
                                                           static int i = 100; /* Initialized static variable stored in DS*/
                                                           return 0;
                                                       }

Day 4:

  1. Read below to understand the heap memory:
    1. What is heap memory?
    2. Difference between malloc(), calloc() & realloc()
    3. What is Memory Leak? How can we avoid?
      1. Answer: Memory Leak
    4. what is the difference between memory leak & memory wastage
      1. Answer: Memroy Leak vs Memory wastage
    5. Why need to use free() ?
    6. what is heap corruption in c ?