Signup/Sign In
LAST UPDATED: NOVEMBER 15, 2022

25 Most Asked C Language Interview Questions

    We have prepared a list of top 25 questions on C Language that you can be asked in an interview for your better preparation. They are -

    Q1). Mention some features of the C language? Why is C so widely used? How is it different from C++ and Java?

    Features of C because of which it is highly used -

    • C combines the features of low-level and high-level languages. Hence, it is said to be a mid-level language.
    • A program written in C can be run on any system with little or no modifications.
    • It is a structured language. It can be broken into parts. It supports procedure oriented programming.
    • It is very fast.
    • It introduced many core concepts and data structures like arrays, functions, strings, etc. Therefore, it is called mother of all languages.
    • It supports pointers and recursion.

    Difference between C and C++-

    • C supports procedure oriented programming while C++ supports both object oriented and procedure oriented programming.
    • C++ is said to be a superset of C.
    • C focuses on method while C++ focuses on data.
    • C follows top-down approach while C++ follows bottom-up approach.

    Difference between C and Java-

    • Java does not supports pointers while C does.
    • C supports preprocessors while Java doesn't.
    • C follows top-down approach while Java follows bottom-up approach.

    Q2). What is preprocessor?

    Preprocessors are programs that process the source code before compilation. A preprocessor directive tells the compiler to preprocess the source code before compiling. It starts with a '#'. Examples of some preprocessor directives are #include, #define, etc. We can use these directives anywhere in our program.

    Q3). What is precedence of operators? What will be the output of the following statement(a = 10, b = 5): a + b * b - a / b % a ;? What is the difference between = and ==?

    Precedence of operators defines how an expression will be evaluated. Each operator has a certain priority in C and according to their priority, they are executed.

    The output is 33.000000

    = is assignment operator while == is used to check equality of the two operands.

    Q4). What are the differences between local and global variables in C?

    A local variable is declared inside a function. Its scope is the function only. It gets destroyed when we exit the function. Global variables are declared outside the function. Their scope is the complete program. Unlike local variables, they retain their value after a function in which they were used is exited.

    #include<stdio.h>
    int global = 5;
    void main(){
        int local = 10;
        return 0;
    }

    Q5). Why is static keyword used in C?

    A static variable has space allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once. It helps us to retain the variable's value of the last function call.

    When declared outside a function, static variables have scope only in the program in which they were declared.

    Q6). What do you mean by call by value and call by reference?

    In call by value, only the values of arguments are sent to the function while in call by reference, addresses of arguments are sent. In call by value, when we call a function, a copy of the arguments which are passed is made. Whatever changes we make are done in those copies and does not reflect back on the calling function. In call by reference, we pass the address of variables, so any changes made show in the calling function. To implement call by reference, we use pointers as arguments.

    Q7). What is recursion? Find the sum of n number using recursion.

    Recursion is a special way of nesting functions where a function calls itself inside it. We must have certain conditions in the function to break out of the recursion, otherwise recursion will occur infinite times.

    function1()
    {   
        // function1 body
        function1();
        // function1 body
    }

    Sum of n numbers -

    #include <stdio.h>
    int recur(int n){
        if (n <= 1)
            return n;
        return n + recur(n - 1);
    }
    
    int main(){
        int n = 5;
        printf("%d", recur(n));
        return 0;
    }

    Q8). What is a pointer? Explain its usage.

    A pointer is a variable used to store memory address. We have to declare and initialize it like any other variable. Pointers are very useful in our code because

    • They make accessing array elements easier.
    • We can implement linked lists, trees and graphs using pointers.
    • We use pointers for accessing dynamically allocated memory.
    • We can return more than one value in a function using pointers.

    Q9). What is Null pointer in C? Also explain what a dangling pointer is.

    When we assign NULL to a pointer, it means that it does not point to any valid address. NULL denotes the value 'zero'. A pointer that is assigned a NULL value is called a NULL pointer in C. Null pointer is a special reserved value of a pointer. A pointer of any type has this reserved value. Formally, each specific pointer type(int *, char *, etc) has its own dedicated null-pointer value. Conceptually, when a pointer has that Null value, it is not pointing anywhere.

    When a pointer is pointing at the memory address of a variable but after some time the variable is deleted from that memory location but the pointer is still pointing to it, then the pointer is known as a dangling pointer.

    Q10). What do you mean by static and dynamic memory allocation?

    Memory allocation done during compile time is called static memory allocation while dynamic memory allocation is done at run time or during execution of the program.

    Static memory allocation is less efficient than dynamic memory allocation and it causes wastage of space too. In dynamic memory allocation, we can reuse memory and free it when not required.

    Q11). Explain the functions used for dynamic memory allocation in C?

    malloc() is used for allocating memory at runtime. This function reserves a block of memory of the specified argument size and on success, returns a pointer of type void which points to the first byte of the allocated memory. This means that we can assign it to any type of pointer using typecasting. If it fails to allocate enough space as specified, it returns a NULL pointer. The memory allocated by malloc() contains garbage value.

    calloc() is another memory allocation function that is used for allocating memory at runtime. calloc function is normally used for allocating multiple blocks of memory to derived data types such as arrays and structures. If it fails to allocate enough space as specified, it returns a NULL pointer. The memory allocated by calloc() is initialized to zero.

    If we want to change the size of memory allocated by malloc() or calloc(), we use realloc(). Without losing the old data, it changes the size of the memory block. The first argument is a pointer pointing to the block of memory we allocated before and the second argument is the updated size of the memory block. On failure, it returns NULL.

    The memory we allocate dynamically exists till the end of the program. As a programmer, it is our duty to release that memory so that it can be used for other purposes and does not cause memory leaks. We do it using free().

    Q12). What are storage classes in C?

    The following storage classes are most often used in C programming,

    1. Automatic variables
    2. External variables
    3. Static variables
    4. Register variables

    A variable declared inside a function without any storage class specification, is by default an automatic variable. They are created when a function is called and are destroyed automatically when the function's execution is completed. Automatic variables can also be called local variables because they are local to a function. However, they can be accessed outside their scope by using pointers. By default, they are assigned garbage value by the compiler.

    A variable that is declared outside any function is a global Variable. Global variables remain available throughout the program execution. By default, initial value of the Global variable is 0(zero). One important thing to remember about global variable is that their values can be changed by any function in the program. The extern keyword is used with a variable to inform the compiler that this variable is declared somewhere else. The extern declaration does not allocate storage for variables.

    A static variable tells the compiler to persist/save the variable until the end of program. Instead of creating and destroying a variable every time when it comes into and goes out of scope, static variable is initialized only once and remains into existence till the end of the program. A static variable can either be internal or external depending upon the place of declaration. Scope of internal static variable remains inside the function in which it is defined.

    Register variables inform the compiler to store the variable in CPU register instead of memory. Register variables have faster accessibility than a normal variable. Generally, the frequently used variables are kept in registers. But only a few variables can be placed inside registers. One application of register storage class can be in using loops, where the variable gets used a number of times in the program, in a very short span of time.

    Q13). Can you compile a program without main()? If yes, give an example.

    Yes, we can.

    #include<stdio.h>
    #include<stdlib.h>
    int fun() // our custom main function
    {
        printf("Welcome to studytonight!\n");
        return 0;
    }
    void _start()
    {
        int x = fun(); //calling custom main function
        exit(x);
    }

    Q14). What are command line arguments?

    Using command line argument we can enter values from the command line at the time of execution. Command line argument is a parameter supplied to the program when it is invoked.

    int main(int argc, char *argv[])

    Here argc, short for argument count, refers to the number of arguments on the command line and argv[], short for argument vector, is a pointer array which holds pointers of type char which point to the arguments passed to the program.

    Q15). What is typecasting?

    Converting one datatype into another using the casting operator is known as type casting or type conversion.

    #include <stdio.h>
    int main() {
    
       int a = 17, b = 5;
       double c;
       c = (double) a / b;
       printf("Value of c : %f", c );
       return 0;
    }

    Q16). Write a program to swap two numbers without using a third variable.

    #include<stdio.h>
    void main()
    {
        int x = 10, y = 15;
        x = x + y;
        y = x - y;
        x = x - y;
        printf("x = %d and y = %d",x,y);
    }

    #include<stdio.h>
    void main()
    {
        int x = 6, y = 4;
        x = x ^ y;
        y = x ^ y;
        x = x ^ y;
        printf("x = %d and y = %d", x, y);
    }
    

    #include<stdio.h>
    void main()
    {
        int x = 6, y = 4;
        x = x * y;
        y = x / y;
        x = x / y;
        printf("x = %d and y = %d", x, y);
    }

    Q17). Write a program to print fibonacci sequence with and without recursion.

    Without recursion-

    #include <stdio.h>
    int main()
    {
        int a, b, c, n;
        printf("Enter number of terms: ");
        scanf("%d", &n);
        a = 0;
        b = 1;
        c = 0;
    
        printf("Fibonacci terms: \n");
        for(int i = 1; i <= n; i++)
        {
            printf("%d ", c);
            a = b;     // Copy n-1 to n-2
            b = c;     // Copy current to n-1
            c = a + b; // New term
        }
        return 0;
    }

    Using recursion-

    int fibonacci(int n) {
       if(n <= 1)
          return n;
       else 
          return (fibonacci(n - 1) + fibonacci(n - 2));
    }
    
    int main() {
        int n = 5;
        printf("Fibonacci of %d: " , n);
        for(int i = 0; i < n; i++) 
          printf("%d ",fibonacci(i));            
    }

    Q18). How will you check whether a number is prime or not? Write code for it.

    #include <stdio.h>
    int main() 
    {
        int n;        //Declare the nummber
        printf("Enter the number: ");
        scanf("%d",&n);    //Initialize the number
        if(n == 1){
            printf("1 is neither prime nor composite.");
            return 0;
        }  
        int count = 0;         //Declare a count variable
        for(int i = 2; i < n; i++)  //Check for factors
        {
            if(n % i == 0)
                count++;
        }
        if(count == 0)           //Check whether Prime or not
        {
            printf("%d is a prime number.", n);
        }
        else
        {
            printf("%d is not a prime number.", n);
        }
        return 0;
    }

    Q19). Write a program to print factorial of a number? How will you do it using recursion?

    declare variables n and fact = 1     //n is the number whose factorial is to be calculated and fact is the variable in which we will store the result
    read input from the user in n
    initialize loop iterator i = 1 and run the loop till i <= n
    do the following in each iteration of the loop
        fact = fact * i
        i++
    print fact

    #include<stdio.h>
    void main()
    {
       int i, n;
       long int fact = 1;
       printf("Enter the number: ");
       scanf("%d" , &n);
       for(i = 1; i <= n; i++)
       {
           fact = fact * i;
       }
       printf("Factorial of %d is %ld", n , fact);
    }

    Using recursion -

    #include <stdio.h>
    int fun(int n){
        if(n <= 1)
            return n;
        return n * fun(n - 1);
    }
    int main()
    {
        int x = 5;
        printf("%d", fun(x));
        return 0;
    }

    Q20). What are actual and formal parameters?

    The parameters passed to a function when it is invoked are called formal parameters while the parameters defined in a function definition are called actual parameters.

    #include <stdio.h>
    void fun(int a, char b){// these are formal parameters
        //body
    }
    int main()
    {
        int x = 5;
        char y = 'a';
        fun(x, y);  // these are actual parameters
        return 0;
    }

    Q21). Can you insert and delete an element from an array? Explain it with proper code.

    To insert element at given position in an array-

    • Input size of the array and its elements.
    • Input position at which you want to insert an element and its value.
    • Shift elements (from the index where you want to insert a new element to the last element) one place to the right.
    • Copy the new value at the given index.
    • Print final array.

    Below is a simple program to insert an element in an array.

    #include<stdio.h>
    int main()
    {
        
        int array[1000], position, n, value;
        printf("Enter number of elements in array: ");
        scanf("%d", &n);
        printf("Enter %d elements: ", n);
        for(int i = 0; i < n; i++)
            scanf("%d", &array[i]);
        printf("Enter the location where you want to insert new element: ");
        scanf("%d", &position);
        printf("Enter the value to be inserted: ");
        scanf("%d", &value);
    
        // shifting the elements from (position to n) to right
        for(int i = n - 1; i >= position - 1; i--)
            array[i + 1] = array[i];
        array[position - 1] = value;    // inserting the given value
    
        printf("Resultant array is: ");
        /* 
            the array size gets increased by 1 
            after insertion of the element
        */
        for(int i = 0; i <= n; i++) 
            printf("%d  ", array[i]);
        
        return 0;
    }

    Delete an element from an array-

    • Input number of elements of the array and the elements.
    • Input position where element is to be deleted. If this position is more than the size of the array, return. Deletion is not possible in that case.
    • Now that we know deletion is possible, shift elements from position - 1(according to array indexing) to the last index by one place to the left. Doing so, we will overide the value of the element at given index by its successor. This will result in deletion of the element from the array.
    • Print the array.

    Below is a simple program to delete an element from array where the position of element to be deleted is given by user.

    #include<stdio.h>
    int main()
    {
        
        int array[1000], position, n;
        printf("Enter number of elements in array: ");
        scanf("%d", &n);
        printf("Enter %d elements: ", n);
        for(int i = 0; i < n; i++)
            scanf("%d", &array[i]);
        printf("Enter the location where you want to delete element from:  ");
        scanf("%d", &position);
    
        if(position > n){
            printf("\nDeletion not possible.");
            return 0;
        }
        else 
            // updating the locations with next elements
            for(int i = position - 1; i < n - 1; i++)
                array[i] = array[i + 1];
    
        printf("Resultant array is: ");
        /* 
            the array size gets reduced by 1 
            after deletion of the element
        */
        for(int i = 0; i < n - 1; i++) 
            printf("%d  ", array[i]);
    
        return 0;
    }

    Q22). Differentiate between getch() and getche().

    getch() reads one character from the keyboard. We don’t care about the input here.We want that the window should wait for the user's input to proceed/end the program. getche() is similar to getch() but it can input alphanumeric values too. It also prints the value of the character we input on the screen which getch() doesn't.

    Q23). What is a memory leak? Give an example.

    When a piece of memory which was previously allocated by the programmer has not been deallocated properly by programmer, it is called a memory leak. The allocated memory in heap is not released back to the heap. That memory cannot be used for any other purpose.

    The memory we allocate dynamically exists till the end of the program. As a programmer, it is our duty to release that memory so that it can be used for other purposes and does not cause memory leaks.

    #include <stdlib.h>
    int fun()
    {
       int *ptr = (int *) malloc(sizeof(int));
       return 0;
    }

    Q24). What is the difference between declaring a header file with < > and ” “?

    <header> searches for a file named header in the standard list of system directories.

    "header" searches for a file named header first in the directory containing the current file and then in the system directories.

    Q25). Write a code to print the following pattern.

    We have curated a list of standard 25 pattern programming questions. You can find the codes for all the patterns here.

    Good luck for your interviews! You can follow our complete C tutorial for in-depth knowledge on these topics.

    Aditi Verma is a skilled author and content creator with expertise in programming languages such as C and C++, as well as Data Structure and DBMS. With an educational background in Computer Science, Aditi's writing is both informative and engaging.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS