Top 50 Basic interview questions from C

Top 50 Basic interview questions from C

  1. What do you mean by dangling pointer?
    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.

  2. What are static variables and functions?
    Ans. Variables which are declared with static keyword are called static variables and same goes for our functions. Static restricts their scope to the function in which they are declared.

  3. What is the difference between Call by Value and Call by Reference?
    Ans. In call by value the variables passed to the function gets passed on as a copy of the original variable and hence any changes made to the variable won’t be reflected in the original variable where as call by reference is passing the value by its addresses and hence any changes made are directly reflected in the original values.

  4. Can you explain the difference between source code and object code?
    Ans. Source code is the code that we write as programmers on the editor using commands and keywords. The compiler then takes these source code and converts into object codes which is then understood by the processor. In C the source code is saved as .C file whereas object code is saved as .obj.

  5. How do you use ‘\0’ character?
    Ans. ‘\0’ is known as the null character and it is used to primarily to mark the end of a string. Every string in C is terminated with a null character.

  6. What is pre-processor?
    Ans. A pre-processor is a program that is executed by the processor before running the main code, after this step, the code is sent for compilation.

  7. Do you know what are command line arguments?
    Ans. Whenever we need to control a program from outside, we need to pass these arguments in the main function. int main(int argc,char *argv[]).

  8. 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")) { 
        } 
    } 
  9. 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.

  10. Point out the difference between delete and delete [].
    Ans. Delete removes a single object from the memory whereas delete [] removes the whole allocated memory to an array of objects. We should always use these very carefully as it might cause memory leaks.

  11. When do we use void keyword in function?
    Ans. When we are declaring a function, we need to decide what our function will return, it can either return something or choose not to return anything. This is when we need to put a void keyword before the function to define that we do not intend to return anything from the function.

  12. State the difference between prefix increment and postfix increment operators.
    Ans. ++a is known as the prefix increment which means the increment is done first then the variable is used.
    a++ is known as the post fix increment which uses the variable first and then increases its value.

  13. What is the difference between a null pointer and void pointer?
    Ans. When a pointer is declared without assigning a value then the pointer refers to null which does not point to a valid location. Void pointers are general purpose pointers which do not have any data type associated with them. It can refer to any type of variable.

  14. Do you know what is a pointer on a pointer?
    Ans. Yes, when a pointer refers to the address of another pointer and forms a multilevel pointing then the first pointer is called a pointer on a pointer.

  15. 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.

  16. Can you write a C program to find whether a number is odd or even?
    Ans. Even numbers do not have any remainder when divided by 2, for odd number the remainder is 1.

    #include <stdio.h>
    int main(void) {
    // your code goes here
    int a;
    scanf("%d",&a);
    if (a % 2 == 0)
        printf("even");
    else
        printf("odd");
    return 0;
    }
  17. What do you know about pre-processor directives?
    Ans. These are placed at the beginning of a C program which includes library functions and other necessary functions required to run our program. We include this using # symbol.

  18. What is the difference between structure and union in C.?
    Ans. A structure allocates memory for all its data types present in it while union only allocates the maximum memory of the data types present. Union uses less memory than structure. Modifying member in structure can be easily done by in union we have to take care of the changes and accordingly modify all other members. Only one element can be accessed at a time in Union whereas any data member can be accessed in structure.

  19. When do we use the register keyword?
    Ans. The register keyword is used for storing machine register variables. It stores mostly loop variables which makes run time faster in C programs.

  20. What is rvalue and ivalue?
    Ans. The expression on the left of = is called the lvalue and on the right is called the rvalue.

  21. Can you explain about self-referential structure?
    Ans. It is a time of structure pointer which refers or points to the variable with same structure data type. It is used in LinkedList, heaps etc. e.g. In linked list we have a next pointer which is self-referential structure.

  22. What are tokens in C?
    Ans. Keywords, Special Symbols, Strings, Operators used in C are called tokens.

  23. Why do we use sprint () function?
    Ans. It is called string print function. We use it to store the output on a character buffer specified in the function.
    Syntax – int sprint(char *str,const char *string);

  24. What is Implicit type casting?
    Ans. In this type of conversion from one data type to another the conversion is performed automatically by the compiler.
    Eg. -

    #include <stdio.h>
    void main ()
    {
    int a = 5;
    float b = x; //implicit type conversion
    printf(“%f”, b);
    }
    Output: 5.000000
  25. What is Explicit type casting?
    Ans. In this type of type conversion, the conversion has to be specified by the user. The compiler is not able to do this conversion by itself.
    e.g.-

    #include <stdio.h>
    void main ()
    {
    float a = 5.46;
    int b = (int) x; //explicit type conversion
    printf(“%d”, b);
    }
    Output: 5
  26. What are wild pointers in C?
    Ans. Uninitialized pointers in X are called Wild Pointers. There point to any arbitrary memory locations and can cause errors and memory leaks.

  27. Describe the difference between = and == symbols.
    Ans. == is called the comparison operator which checks for equality and returns true or false correspondingly.
    = is the assignment operator which is used to assign some values to a variable.

  28. What is the cyclic nature of the data types?
    Ans. In C some data types are such that when their values exceed a certain limit, their values changes to a value again which is in the limit. This cyclic nature helps to avoid compilation errors. Such data types are – int, float, char etc.

  29. What is the difference between global variables and static variables?
    Ans. Global variables are variables with global scope hence they can be accessed anywhere in the program. Static variables are defined statically i.e, their values can not be changed and the same variable is used through the program. They too can be accessed from anywhere in the program as they share only one copy of the data.

  30. What do you mean by memory leak?
    Ans. When we define a variable the value is stored in memory. Now at times of deletion when the variable is deleted, we might forget to delete the allocated memory. This undeleted memory in heap is called memory leak. This affects the performance of a program.

  31. What do you mean by while (0) and while (1)?
    Ans. while (0) refers that the loop will not get executed as it has got a false condition where as while (1) refers to that the condition is always true and the loop will run until any exit or break statement occurs. Any non-zero number in condition is a true condition whereas only 0 refers to false.

  32. What is the use of volatile keyword?
    Ans. This keyword prevents the compiler from optimizing the variable or object. Most of the compiler uses methods to make the program more efficient by modifying some of the objects abut using volatile keyword we can stop the compiler from optimizing that object.

  33. Can you explain the use of extern storage specifier?
    Ans. This specifier is used to declare objects that can be used by multiple source files. A variable defined with extern keyword is stored externally and its scope is throughout the directory and can be used by any source files.

  34. What do you mean by sequential access?
    Ans. When data is stored in sequential manner in files then while retrieving also, we have to sequentially read each file one by one and find the required information.

  35. How is data stored in stack data structure?
    Ans. In stack the data is stored in FILO fashion (First in Last Out). It can be simply understood by a stack of books. Stack data structure contains few methods like PUSH (to insert the data) and POP (to remove an element), TOP (returns the top element) etc.

  36. Describe modifier in C.
    Ans. Modifier is a prefix to the basic data types which is used to show the modification to the memory location of the variable. E.g. – int stores 4 bits of data generally, where as long int stores 8 bit of memory.

  37. How many such modifiers are there in C?
    Ans. There are total 5 modifiers in C –

    • Long
    • Short
    • Signed
    • Unsigned
    • Long long
  38. What is the use of rand () function in C?
    Ans. Rand () function is used to generate random numbers. It returns any random number in the int range.

  39. How can we add two pointers?
    Ans. Well, we cannot actually add two pointers since they point to two memory locations, memory locations cannot be added logically.

  40. What is modular programming?
    Ans. When we break our program into various modules such that it is easy to maintain each part individually then these modules form the basis of modular programming.

  41. What is a structure in C?
    Ans. A structure is a user defined data type which wraps ups data and forms a group. It can contain any types of data and it allocates the memory to all its data members present in it. It is defined by struct keyword.

    Syntax:   struct vehicle{
    Int wheels;
    Char model;
    Int driver[10];
    }
  42. What are macros?
    Ans. Macro is a segment of code which defined by #define and can be used anywhere in the program. It is used for variables which will be used multiple times in a program. E.g. - #define MOD 128, so, whenever the program will find MOD at time of compilation it will replace that with the value of MOD given in macro.

  43. Can you differ3entiate between getch() and getche().
    Ans. Getch() reads data from keyboard but does not use buffer where as getche() uses buffer and prints the data on the display.

  44. Can you explain tolower() function with an example.
    Ans. Tolower() function is designed to convert uppercase characters to lower case.

    #include <stdio.h>
    #include<ctype.h>
    int main(void) {
    char c;
    c='A';
    printf( " %c",  tolower(c));
    return 0;
    }
    Output: a
  45. Generate some random numbers in C.
    Ans. We use the rand() function here.

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
     int a,b;
     for(a=1;a<=5;a++)
     {
           b=rand();
           printf("%d\n",b);
     }
     return 0;
    }
    Output:1804289383
    846930886
    1681692777
    1714636915
    1957747793
  46. Can we assign same variable name to the variables which have different scope?
    Ans. Yes, same variables having different scopes can have same names.
    E.g, -

    Int a;
    Void func(){
    Int a;
    }
    Int main(){
    Int a;
    }
  47. Write a program to swap two numbers in C.
    Ans. We use a third variable here to swap the numbers.

    #include<stdio.h>      
    #include<conio.h>      
    main()      
    {      
     int a=5, b=21;    
     clrscr();       
     printf("%d %d",a,b);        
     int temp = a;
     a = b;
     b = temp;
     printf("%d %d",a,b);
    }
    Output: 5 21
    21 5
  48. Explain the # symbol in C.
    Ans. # is used to include directives, that are used to run our program. This is used to specify functions while start up and exit. It also supresses any warnings.

  49. 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.

  50. What are the funcitons to open and close the file in C.
    Ans. We use fopen() function to open a file and fclose() function to close a file in C.
    E.g, -

    #include <stdio.h>
    # include <string.h> 
    int main( )
    {
    FILE *fp ;
    char data[50];
    fp = fopen("test.c", "w") ;
    if ( fp == NULL )
    {
        printf( "Could not open file test.c" ) ;
        return 1;
    }
    fclose(fp) ;
    return 0;        
    }

Hope this article provides you with the basic C questions and to quick revise all the important concepts.

Previous post 20 Must Practice basic Java Programs for beginners
Next post Top 30 Java Programming interview questions

Leave a Reply

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