Commonly asked interview questions on c

Commonly asked interview questions on c

  1. Why do we still use C? Isn’t it quite old?
    Ans. C is considered to be the mother of all modern-day languages. It was initially created for the purpose of making operating systems. Specifically, it was used in UNIX operating system. It is as fast as assembly languages and hence are the first priority for system development. Other programs that usually use C for their development are – Assembler, text editors, databases, Utility tools etc.
  2. What are storage class specifiers and how many of them do you know?
    Ans. A storage class defines the scope and lifetime of variables or functions in C. There are four different storage classes in C –

    • Auto
    • Register
    • Static
    • Extern
  3. What do you mean by scope of a variable?
    Ans. It is the part of a program which can be directly accessed. In C all identifiers are statically scoped. C has basically 4 types of scope rules – File Scope, Block Scope, Function Prototype, Function Scope.
  4. Can you print Hello! World without semicolon?
    Ans. In C printf function returns the number of characters written in stdout and performs the function.

    #include <stdio.h> 
    int main(void) 
    { 
    if (printf ("Hello World")) { 
    } 
    }
  5. Do you know about pointers? Can you explain about far pointer?
    Ans. Yes, Pointers are basically variables which refers to the address of a particular value.
    A pointer which can access all 16 segments of the RAM is known far pointer.
  6. Can you explain what is dangling pointer and how we can avoid it?
    Ans. If a pointer is pointing to some address of a variable and at that moment another pointer is used to delete that variable or memory occupied then the first pointer points to a memory location which might not be accessible. The first pointer is known as Dangling pointer.
    It can easily be removed by making the first pointer point to null.
  7. What is Static Memory Allocation?
    Ans. Static memory allocation is when the memory is allocated at compile time. This memory cannot be increased while running a program. E.g., int arr[10]; the size of this array cannot be increased during run time. This is called static memory allocation.
  8. Then what is dynamic memory allocation?
    Ans. This memory allocation occurs during run time and can be easily changed. C uses malloc (), calloc (), realloc () for dynamic memory allocation. This memory is implemented using data section. Less memory space is wasted in this memory allocations.
  9. Can you give the difference between malloc () and calloc ()?
    Ans.
Malloc() Calloc ()
Malloc () function will create a single block of memory specified by the user. Calloc () can assign multiple blocks of memory.
Malloc() is faster but less secure It is slower but more secure
It does not initialize memory It initialises memory to 0
Returns only the starting address but does not make it 0 Returns the starting address and makes it 0
  1. Do you know about enumerations?
    Ans. Enumerations are list of integer constants with name. It is defined in C as enum.
    enum week{Mon,Tue,Wed,Thur,Fri,Sat,Sun}
  2. What is Union? Why do we need Union when we already have structures?
    Ans. Union is a data type that helps in storing multiple types of data in a single unit. It is different than structures as in structures the size of memory that is allocated is the sum of all the elements present in them which might use a lot of extra space. Union only allocates memory of the largest variable present in it. Although in Union, we can only access one variable at a time.

    union unionstruct  
    {  
    int a; //union members declaration.  
    float b; //assigns memory to only size of float 
    char c;  
    };
  3. Can you briefly explain at where do the variables are stored in C?
    Ans. The following describes where each type of data is stored in C.
    global variables – data
    static variables – data
    constant data types – code and/or data
    local variables (declared and defined in functions) – stack
    variables declared and defined in main function – stack
    pointers – data or stack, depending on the context.
    dynamically allocated space (using malloc, calloc, realloc)- heap
  4. What is auto keyword and what is it’s use?
    Ans. Each variable defined inside a function is called a local variable, these local variables contain are known as automatic variables(auto). We do not need to explicitly define them as local. If it contains no value then it has garbage value.
  5. Can you write a program without the use of main function?
    Ans. Well, I can write a program without main function, but it will only get compiled. It cannot be executed without a main function.
  6. Write a C program to check whether a given number is palindrome or not.
    Ans. A number is palindrome if it is equal to its reverse

    #include<stdio.h>    
    #include<conio.h>    
    main()    
    {    
    int n,r,sum=0,temp;
    scanf("%d",&n);    
    temp=n;    
    // a number is palindrome if it is equal to it's reverse
    while(n>0)   //getting reverse of the number 
    {    
    r=n%10;    
    sum=(sum*10)+r;    
    n=n/10;    
    }    
    if(temp==sum)    
    printf("palindrome ");    
    else    
    printf("not palindrome");  
    }
Previous post 10 simple c++ programs for beginners
Next post 20 Must Practice basic Java Programs for beginners

Leave a Reply

Your email address will not be published. Required fields are marked *