Top 100+ C interview Questions and answers for freshers

C is a powerful procedure-oriented and general-purpose programming language and also supports imperative and structured paradigms.

In this article, we’ll walk you through the most common C interview questions and answers for freshers especially.

Interview questions are for testing your basic knowledge of the subject and practical skills.

So, without wasting any more time, let’s check out the 100 C interview questions and answers for the freshers.

c interview questions and answers for freshers

Top 100 C Interviews Questions and Answer

Who is the father of C language?

Dennis Ritchie is known as the father of the C language. Click here for more info

When was C language developed?

C language was developed in 1972 in the Bell Laboratory ( also known as AT&T Bell Laboratory). Click here for more info

What is C language?

C is a powerful procedure-oriented and general-purpose programming language and also supports imperative and structured paradigms. Click here for more info

Why is C known as a mother language?

Because most of the compilers are written using C language and most of the high-level languages like C++, Java, Python etc are mostly written in C.

What are the features of the C language?

Following are the features of the C language

Simple, Robust, Portable, Structured, Fast, Extensible, Memory Management, Secure.

Why is C called a mid-level programming language?

‘C’ language is called mid-level or middle-level language because it can be used as system programming to develop operating systems as well as application programming for developing applications.

What is an auto keyword in C?

auto keyword is storage class/keyword. It is used to declare local variables.

Some important points related to auto keyword

  • used to define local variables and can be used by default
  • used for forward declaration of nested functions
  • can result in non-contiguous memory allocation

What is a token?

A token is the smallest individual unit of a computer programming language.

C has the following types of token

For more details on tokens, click here.

Can we compile a program without main() function?

Yes, a program can be compiled using #define, but can not be executed.

What is the acronym for ANSI?

American National Standard Institute is a private and non-profit organization that manages the development of voluntary consensus standards for products, services, processes, systems, and personnel in the United States

What is the newline escape sequence?

‘\n’ is the newline escape sequence.

What is the maximum length of an identifier?

32 characters are the maximum length of an identifier.

What is the structure?

It is a user-defined data type in the C programming language and is used to group items of different data types into a single type.

struct keyword is used to declare a structure.

What is a union?

Union is a user-defined data type and is a collection of different data types of variables in the same memory location.

What is the difference between C and C++?

CC++
C contains 32 keywordsC++ contains 63 keywords
C supports procedural programming.C++ supports both procedural and object-oriented programming paradigms.
scanf() and printf() functions are used for input/output in Cscanf()cin and cout are used for input/output in C++.
Function and operator overloading is not supported in C.Function and operator overloading is supported by C++.
C is a function-driven language.C++ is an object-driven language
Functions in C are not defined inside structures.Functions can be used inside a structure in C++.
Header file used by C is #include <stdio.h>.Header file used by C++ is #include <iostream.h>.
Virtual and friend functions are not supported by C.Virtual and friend functions are supported by C++.
Instead of focusing on data, C focuses on method or process.C++ focuses on data instead of focusing on method or procedure.
C provides malloc() and calloc() functions for dynamic memory allocation, and free() for memory de-allocation.C++ provides ‘new‘ operator for memory allocation and “delete” operator for memory de-allocation.
Direct support for exception handling is not supported by C.Exception handling is supported by C++.

What is the difference between top down approach and bottom up approach in programming languages?

  • The top-down or also known as the top-to-bottom approach focuses on breaking down a big problem into smaller and understandable chunks
  • The bottom-up approach first focuses on solving the smaller problems at the fundamental level and then integrating them into a whole and complete solution.

What is the difference between C and Java?

CJava
C was developed by Dennis M. Ritchie between 1969 and 1973.Java was developed by James Gosling in 1995.
C is a Procedural Programming Language.Java is an Object-Oriented language.
C is a middle-level language.Java is a high-level language
C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system.Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).
Generally breaks down into functions.Breaks down into Objects.
Can be used for system programming as well as Application programming.This is not the case in Java.
Memory allocation can be done by malloc in CMemory allocation can be done by a new keyword in Java.
C supports pointers.Java does not support pointers.
Call by value and call by reference is supported in C.It only supports a call by value.
C is platform dependent.Java is platform-independent.
It supports user-based memory management.It internally manages the memory.
It follows a top-down approach.Java follows a bottom-up approach.
Overloading functionality is not supported by C.Java supports method overloading which helps in code readability.
C supports Preprocessors.Java does not support Preprocessors.
Union and structure data types are supported by C.Java does not support unions and structures.
C supports the storage classes.Whereas Java does not support the storage classes.
Go-to statements are supported in C language.Java does not support go-to statements.
Virtual keywords are supported by C.Virtual keywords are not supported by Java.
Overloading functionality is not supported by C.Java supports method overloading which helps in code readability.
Default members of C are public.Default members of Java are private.
Data hiding is done by using static in C.Data hiding is done by using private in Java.

What is meant by programming language and give some examples?

Programming language means a language to interact with hardware/machine.

C, C++, Java, Python, PHP etc

What is embedded C?

Embedded C is used to develop micro-controller based applications. The extensions in the Embedded C language from normal C Programming Language is the I/O Hardware Addressing, fixed-point arithmetic operations, accessing address spaces, etc. Embedded C Program has five layers of Basic Structures.

  1. Pre-processor directives
  2. Main function
  3. Comment
  4. Global Declaration
  5. Local Declaration

What do you mean by high level, middle level and low level languages and give an example for each?

High-level languages

  • Very programmer friendly language.
  • Provide almost everything that the programmer might need to do as already build into the language.
  • Easy to debug and maintain.
  • Example: Java, Python

Middle-level languages

  • It fills the gap between the high-level and low-level language.
  • Don’t provide all the built-in functions, but provide all necesary building blocks that we need to produce the result we want.
  • Easy to debug and maintain as compared to low-level language
  • Example: C, C++

Low-level languages

  • It is machine friendly language.
  • Provide nothing other than access to the machine’s basic instruction set.
  • Hard and complex to debug and maintain.
  • Example: Assembly language.

What is the difference between structured oriented, object oriented and non-structured oriented programming language?

Structured oriented programming language

  • Large programs are divided into small programs called modules.
  • Main focus is on functions and procedures that operate on data
  • Data moves freely around the systems from one function to another
  • Program structure follows “Top Down Approach”
  • Example: C, Pascal, ALGOL and Modula-2

Object-oriented programming language

  • Programs are divided into objects.
  • Main focus is on the data that is being operated and not on the functions or procedures.
  • Data is hidden and cannot be accessed by external functions.
  • Program structure follows “Bottom UP Approach”.
  • Example: C++, JAVA and C# (C sharp).

Non-structure oriented programming language

No specific structure for programming.

Example:  BASIC, COBOL, FORTRAN

What is compiler?

A compiler is a translator/convertor which converts the source code written in a high-level language into machine code in one go and return the error if any.

What is the difference between assembler, compiler and interpreter?

Assembler, compiler and interpreter are known as language processors.

Compiler : 

  • Reads the complete source program written in high-level language as a whole in one go and translates it into an equivalent program in machine language is called a Compiler.  
  • Example: C, C++, C#, Java. 
  • The source code is translated to object code successfully if it is free of errors.
  • The compiler point out all the error at the end with line number of errors if any.
  • The error must be removed and the program must be compiled again before execution.

Assembler :

  • It used to translate the program written in Assembly language into machine code.
  • The source program is an input to the assembler that contains assembly language instructions.
  • The output generated by the assembler is the object code or machine code understandable by the computer.


 

Source: geeksforgeeks.org

Interpreter :

  • Interpreter translates every single statement of the source program into machine code and executes immediately before moving on to the next line.
  • If there is an error, the interpreter ends its process at that statement and shows an error message.
  • The interpreter moves on to the next line for execution only after the removal of the error.
  • An Interpreter directly executes instructions written in a programming or scripting language without previously converting them to an object code or machine code.            

Example: Perl, Python and Matlab.

Source: geeksforgeeks.org

What is printf()?

printf() is a function that is used for printing the values or messages.

What is scanf()?

scanf() is used for getting the input from the users.

List out some of C compilers.

1) Borland Turbo C

Borland turbo C

2) Tiny C Compiler

3) Portable C Compiler

4) GCC

5) Clang

What is header file in C language?

A header file is a file with an extension. h which contains C function and macro declarations and definitions as given in the library.

There are two types of header files:

  1. In built header files e.g stdio.h, conio.h etc.
  2. User define header files.

Is C language case sensitive?

Yes, it is case sensitive.

What is data type in C?

The data type determines the types of data that a variable can hold. So basically, data types specify the type of data which can be entered/assigned in the particular variables/identifiers.

What is the difference between int, char, float and double data types?

int:-

  • Used for whole numbers.
  • eg: 23, 59, -78, -13 etc
  • Default size is 2 bytes

char:-

  • Used for character
  • eg: ‘a’, ‘A’, ‘5’, ‘-8’ etc
  • Default size is 1byte

float:-

  • Used for real numbers.
  • eg: 23.5, 59.5, -78.0, -13.0 etc
  • Default size is 4 bytes

Double data types:-

  • It is also used for real numbers.
  • eg: 23.5, 59.5, -78.0, -13.0 etc
  • Default size is 8 bytes

What is the use of sizeof() function in C?

a shot of dev knowledge

What is the sizeof() function in C?

The sizeof()function is used to calculate the size (in bytes)that a data type occupies in ​the computer’s memory.

What is modifier in C?

Modifiers are keywords that are prefixed with basic data types to modify the memory allocated for a variable.

What are different types of modifiers in C?

Following are the types of modifiers in C

  • long
  • short
  • signed
  • unsigned
  • long long

What is enum in C?

  • Enumeration or enum is a user-defined data type in C that is used to assign names to integral constants because the names make a program easy to read and maintain.
  • The keyword ‘enum’ is used to declare new enumeration types in C.

What is void in C?

void() is a return type in C which return nothing.

What is constant in C?

  • Constants are like a variable, except that their value never changes during execution once defined.
  • Constants are also called literals.
  • It can be of any data types.

What are the types of constants in C?

  • Integer Constant
  • Real constant
  • Single Character Constants
  • String Constants
  • Backslash character constant(Escape sequence)

What is variable in C?

A variable is the name of memory location which is used for datastore and its value can be changed and reused n number of times during the program execution.

Can variable name start with numbers?

No, the variable name must start with alphabets or underscore(_) only.

What is the difference between variable declaration and variable definition in C?

Variable declaration

  • It tell the compiler about the data type and size of a variable.
  • Can be declared n number of times.
  • It is used for identification of a variable and assignment of properties.
  • Memory does not allocated.

Variable definition

  • Variable definition allocates memory to the variable.
  • Can be defined only once.
  • It is used for assignments of storage space to a variable.

What are the different types of variable in C?

  • Local variable
  • Global variable
  • Environment variable

What is local variable in C?

  • Local variables are having scope/life only within the function.
  • Local variables are declared within the function and can’t be accessed outside the function.

What is global variable in C?

  • Global variables are having scope throughout the program.
  • Global variables are declared outside the main function and can be accessed from anywhere in the program.
  • What are all decision control statements in C?

    • The decision control statements are the statements where some decision/condition is checked before executing the block of code.
    • These are 3 types
      • if statements
      • if else statements
      • nested if statements

    What are all loop control statements in C?

    • Loop control statements are used to iterate the block of code until the given condition is true.
    • Once the condition is not satisfied or become false, control comes out of the loop statements.
    • C provide 3 types of loops
      • for
      • while
      • do-while

    What is the difference between while and do-while loops in C?

    While loop is executed only when the given condition is true. Also, known as the Entry control loop

    The do-while loop is executed for the first time even if the condition is false because after executing the while loop for the first time, the condition is checked.

    The do-while loop is also known as the Exit control loop

    What is the difference between single equal “=” and double equal “==” operators in C?

    = is an assignment operator which assigns the value of RHS to LHS

    == is a comparison operator which shows that LHS is equal to RHS

    What is the difference between pre increment operator and post increment operator?

    Pre-increment (++i)

    • Change then use
    • The value is incremented by one before assigning the value to the variable.
    • Example: ++a, ++i etc.

    Post-increment (i++)

    • Use then change
    • The value is assigned to the variable and then incremented by one
    • Example: a++, i++ etc.

    What is the difference between pre decrement operator and post decrement operator?

    Pre-decrement (–i)

    • Change then use
    • The value is decremented by one before assigning the value to the variable.
    • Example: a–, i– etc.

    Post-decrement (i–)

    • Use then change
    • The value is assigned to the variable and then decremented by one
    • Example: a–, i– etc.

    What is “&” and “*” operators in C?

    & (ampersand) operator is used to get the address of the variable.

    For example: &x will give the address of x

    *” (asterisk) operator is used as a pointer to a variable and pointer variable stores the memory address of another variable

    For example: * a where * is a pointer to the variable a.

    What will happen if break statement is not used in switch case in C?

    if the break statement is not used in the switch case in C then after that case, it will fall through and execute all the cases below it.

    Example

    
    #include <stdio.h>
    
    int main() {
        char ch;
        int n1, n2;
    
        printf("Enter an operator (+, -, *, /): ");
        scanf("%c", &ch);
        
        printf("Enter two operands: ");
        scanf("%d %d",&n1, &n2);
        
        
    
        switch(ch)
        {
            case '+':
                printf("%.1d + %.1d = %.1d",n1, n2, n1+n2);
                
            case '-':
                printf("%.1d - %.1d = %.1d",n1, n2, n1-n2);
                
    
            case '*':
                printf("%.1d * %.1d = %.1d",n1, n2, n1*n2);
                
    
            case '/':
                printf("%.1d / %.1d = %.1d",n1, n2, n1/n2);
                break;
            
            default:
                printf("Error! operator is not correct");
        }
    
        return 0;
    }

    Output

    Enter an operator (+, -, *, /): +
    Enter two operands: 5
    7
    5 + 7 = 12
    5 - 7 = -2
    5 * 7 = 35
    5 / 7 = 0

    Why is default statement used in switch case in C?

    The default statement is used when none of the cases matched, the controller executes the default statements.

    What is the use of “goto” statement?

    A goto statement is a jump statement that allows jumping within the scope of a variable length.

    What value will be assigned to the variable x if a = 10, b = 20, c = 30, d = 40 for the expression X = a/b+c*d-c?

    #include <stdio.h>
     
    int main()
    {
       int a = 10, b = 20, c = 30, d = 40, x ;
     
       x = a/b+c*d-c;
       printf(" Value of X = %d\n", x);
       return 0;
    }

    Output

    Value of X = 1170

    What is the value assigned to the following variables?
    int n1 = 13/3; int n2 = 13%3;

    n1=4

    n2=1

    Reason: Both the variable n1 and n2 is of integer type and can have integer value only not the fractional values.

    What is the difference between auto variable and register variable in C?

    • Both auto variable and register variable are local variables.
    • Register variables are stored in register memory.
    • auto variables are stored in main CPU memory.
    • Register variables will be accessed very faster than the auto variables since they are stored in register memory rather than main memory.
    • Only limited variables can be used as register variables since register size is very low i.e., 16 bits, 32 bits or 64 bits

    What is the difference between auto variable and static variable in C?

    • auto and static variables are local variables.
    • Static variables can hold the value of the variable between different function calls.
    • Scope of auto variable is within the function only.
    • auto variable can’t hold the value of the variable between different function calls.

    Where should type cast function not be used in C?

    A typecast function can not be used with const as constant values cannot be modified and with volatile declaration as the operating system might keep changing the values of volatile variables.

    How many arguments can be passed to a function in C?

    • n number or can say any number of arguments can be passed to a function
    • stack has enough memory to store the arguments but program may crash when stack overflows.

    What is static function in C?

    • The static keyword makes a function local function that can be accessed only by the program/file where it is declared and defined.
    • Other programs/files can’t access these static functions.

    If you want to execute C program even after main function is terminated, which function can be used?

    atexit()” function can be used if we want to execute any function after the main function is terminated.

    Syntax: int atexit (void (*fun)(void));

    The function “fun” will be called automatically without any arguments once the main function of the program is terminated.

    Is it possible to call atexit() function more than once in a C program?

    Yes, we can call atexit() function more than once but it will be executed in reverse order.

    What is exit() function in C?

    exit() function terminates the program and returns the status of the code.

    What is the difference between exit() and return in C?

    exit() function terminates the program and returns the status of the code whereas the return function returns the controller to the point from where the function was called.

    exit() function
    return function

    What is the use of “#define” in C?

    #define is a preprocessor directive that is used to define constant value.

    What is the syntax for comments in C?

    Syntax

    The syntax for comment is:

    /* Single line comment */

    OR

    /*
     *This is a syntax of 
    * multiline comment
     */

    What is “##” operator in C?

    • ## is a pre-processor macro in C.
    • This is used to concatenate 2 tokens into one token.

    What is pragma in C?

    • pragma is a preprocessor macro in C and is used to provide additional information to the compiler in C.
    • This type of directive is compiler specific directives i.e., they may vary from compiler to compiler.

    How will you override an existing macro in C?

    To override an existing macro, we need to undefine the existing macro using “#undef”. Then, we need to define the same macro again with the new value.

    How to check whether macro is defined or not in a C program?

    #ifdef preprocessor directive is used to check whether the macro is defined or not in a C program.

    What is the difference between memcpy() and strcpy() and memmove() functions in C?

    • memcpy() copies the exact number of bytes you specify.
    • strcpy() copies until it reads a NULL value from one string to another.
    • memmove() function is used to copy a specified number of bytes from one memory to another or to overlap on same memory.

    Is there any inbuilt/library function in C to remove leading and trailing spaces from a string? How will you remove them in C?

    • No!, there is no inbuilt function in C to remove leading and trailing spaces from a string.
    • We need to write a function to remove the leading and trailing spaces.
    • Logic to remove spaces.
      • check first non-space character in given string
      • copy that string from where non space character is found.
      • check whether any spaces are available in copied string from the end of the string.
      • If space is found, copy ‘\0’ in that space until any character is found. (‘\0’indicates the end of the string).
      • Thats all.

    What is the difference between strcpy() and strncpy() functions in C?

    • strcpy( ) function copies whole content of one string into another string.
    • strncpy( ) function copies portion of contents of one string into another string.
    • If destination string length is less than source string, entire/specified source string value won’t be copied into destination string in both cases.

    Can array subscripts have negative value in C?

    No

    What is the difference between array and string in C?

    • An array can hold values of similar data types.
    • string can hold only char data type and ended with null character (‘\0’)

    What is pointer in C?

    • Pointer is a variable that holds the address of another variable instead of value.
    • Syntax
      • return_type *variable_name;

    What is null pointer in C?

    • The null pointer points to an empty location in memory.
    • Examples
      • int *n =  NULL;
      • char *ch = NULL;

    What is NULL in C?

    • NULL is a macro that is defined in C header files having value 0.
    • Syntax
      • #define NULL (void *) 0;

    What is void pointer in C?

    • It is a special type of pointer which has no associated data type with it and can hold address of any type and can be typcasted to any type.
    • void ponter is also called as general purpose pointer.

    What is dangling pointer in C?

    When a pointer is pointing to a non-existing memory location is called a dangling pointer.

    What is wild pointer in C?

    • store the memory addresses just like the normal pointer variables but point the unallocated memory .
    • wild pointers point any random memory location.

    What is file pointer in C?

    • A file pointer is a pointer that is used to handle and keep track of the files being accessed.
    • A file pointer definition is given in stdio.h header file.
    • Syntax
      • FILE *fp where FILE is a data type and fp is a file pointer
    • fopen() and fclose() functions are used for the operation.

    When can void pointer and null pointer be used in C?

    • Void pointer is used for storing address of other variable irrespective of its datatype.
    • Null pointer is used for assigning 0 to a pointer variable of any type. 

    What is const pointer in C?

    • A constant pointer or const pointer cannot change the address of the variable to which it is pointing.
    • It cannot point to any other variable if its pointing to some variable.
    • Syntax
      • <data_type> *const <variable_name>;  
    • Declaration
      • int *const nptr;  

    Is pointer arithmetic a valid one? Which arithmetic operation supposed to be not valid in pointer? Why?

    • Pointer arithmetic is not valid one.
    • Addition, multiplication and division are not allowed in pointer operation.
    • Substration is done to know how many elements are available between the two pointers.

    Is void pointer arithmetic a valid one? Why?

    Void Pointer arithmetic is not a valid one but as the void pointer is a generic pointer and it is not referring int, char or any other data type specifically. Therefore, we need to cast the void pointer to a specific data type before applying arithmetic operations.

    What is the difference between null and zero?

    • NULL is a macro which is defined in C header files.
    • Zero (0) is a value.

    What is the difference between null pointer and uninitialized pointer in C?

    Uninitialized pointers are called wild pointers which points to a random memory location whereas a null pointer is a pointer which is pointing to nothing.

    Can array size be declared at run time?

    Arrays, by definition, are fixed-size memory structures so can’t be declared at run time.

    What is memory leak in C?

    A memory leak occurs when memory is allocated but not released back to the operating system.

    What happens when we try to access null pointer in C?

    When a null pointer in C is accessed it can point to random memory location results in programs misbehave.

    What is meant by segmentation fault or memory fault in C?

    Segmentation fault or segfault is a common condition that causes programs to crash because of illegal/invalid memory access.

    What is meant by core dump in C?

    • It is an error indicating memory corruption when a piece of code tries to do read and write operation in a read only location in memory or freed block of memory.

    What is the size of int pointer and char pointer in C?

    • Pointer variable size is not depending on data type.
    • Pointer variable always stores the address of other variable which is always integer data type.
    • Any pointer variable (int, char, double, etc) size will be 2 for 16 bit processor, 4 for 32 bit processor and 8 for 64 bit processor.

    What are library functions and their use in C language? Can we write our own functions and include them in C library?

    • Library functions or also known as built-in functions in C language which are grouped together and placed in a common place which is libraryof the languages.
    • The use of library function is to get the pre-defined output instead of writing our own code to get those outputs.
    • Yes. We can write our own functions and include them in C library.

    Can variable name be start with underscore in C?

    Yes.

    Can variable name have special symbols in C?

    Yes but only underscore(_) and $. No other special symbols are allowed.

    What is inline function in C?

    A function whose definitions are small and be substituted at the place where its function call has happened is called the inline function.

    Is it possible to print “Hello World” without semicolon in C? How?

    Yes, we can write/print anything without a semicolon.

    #include<stdio.h>  
     int main()    
    {    
       while(!printf("hello world")){}    
    return 0;  
    }  

    What is extern and static function in C?

    Extern Function:- It is used to extend the visibility of variables/functions but its use is implicit and when extern is used with a variable, it can be declared, not defined.

    Static Function:- function that has a scope that is limited to its object file.

    What is the difference between pointer and array in C?

    A pointer is a variable that holds the address of other objects/variables and an array is a collection variable of the same data types.

    Can a variable be both volatile and constant in C?

    Yes, a variable can be both volatile and constant in C.

    What is the use of main() function in C?

    Every programs execution starts from main() function only.

    1 thought on “Top 100+ C interview Questions and answers for freshers”

    Leave a Comment

    Ads Blocker Image Powered by Code Help Pro
    Ads Blocker Detected!!!

    We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

    Please Reload After Disabling Ad\'s