C Programming Interview Questions with Answers

In this article, we’ll discuss the 50 most frequently asked C programming interview questions during technical interview rounds by various companies. The following C interview questions cover the level from easy to advanced. Even if you are a beginner in C, these hard questions for programming conversation in C will help you better understand and improve.

50 C Programming Interview Questions with Answers

1) How do you construct an increment statement or decrement statement in C?

There are two ways to do this. One of them is to use the increment operator ++ and the decrement operator -. For example, “x ++” means increasing x by 1. Similarly, “x -” means reducing x by 1. Another way to write incremental statements is to use the conventional plus sign or the minus sign. For “x ++” another way to write is “x = x +1”.

2) What is the difference between Call by Value and Call by Reference?

By using the Call by Value function, you send the variable value as a parameter to the function, while the Call by Reference function sends the variable address. In addition, in the Call by Value area, the parameter value is not affected by any operation, while in the case of Call by Reference, the process can affect the values within the function.

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

Putting comment symbols / * * / around code, also called “commenting”, is a way to isolate some codes that you think may cause errors in the program without removing the code. The thing is, if the code is really correct, just remove the comment symbols and continue. It also saves you the time and effort of having to re-enter the codes if they were deleted first.

4) What is the difference between the expression “++a”  and “a++”?

In the first expression, the increment will first be for the variable a, and the resulting value will be the one that will be used. This is also called prefix increment. In the second expression, the current value of variable a would be the one that will be used in the operation before the value of the value itself is increased. This is also called postfix increment.

5) What is a stack?

The stack is one form of data structure. Data is stored in stacks using the FILO (First In Last Out) method. In each particular case, only the top of the stack is available, which means that in order to retrieve the data stored in the stack, you must first extract those in the top. Stack data storage is also referred to as PUSH, while data retrieval is called POP.

6) What is a sequential access file?

When you write programs that store and retrieve data from a file, you can designate the file in various forms. The sequential access file consists in the fact that the data is saved in sequential order: one data is placed in a file after another. To access specific data in a sequential access file, data must be read one at a time until the correct one is reached.

7) What is variable initialization and why is it important?

This refers to the process in which the variable is assigned an initial value before it is used in the program. Without initialization, the variable would have an unknown value that can lead to unpredictable results when used in calculations or other operations.

8) What are the features of the C language?

The main features of C language are given below:

  • Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into parts
  • Portable: C is highly portable means that once the program is written can be run on any machine with little or no modifications.
  • Mid Level: C is a mid-level programming language as it combines the low- level language with the features of the high-level language.
  • Structured: C is a structured language as the C program is broken into parts.
  • Fast Speed: C language is very fast as it uses a powerful set of data types and operators.
  • Memory Management: C provides an inbuilt memory function that saves memory and improves the efficiency of our program.
  • Extensible: C is an extensible language as it can adopt new features in the future.

9) What is the difference between malloc() and calloc()?

calloc() malloc()
Description The malloc() function allocates a single block of requested memory. The calloc() function allocates multiple blocks of requested memory.
Initialization It initializes the content of the memory to zero. It does not initialize the content of memory, so it carries the garbage value.
Number of arguments It consists of two arguments. It consists of only one argument.
Return value It returns a pointer pointing to the allocated memory. It returns a pointer pointing to the allocated memory.

10) What is the difference between the local variable and global variable in C?

Following are the differences between a local variable and global variable:

Basis for comparison Local variable Global variable
Declaration A variable which is declared inside function or block is known as a local variable. A variable which is declared outside function or block is known as a global variable.
Scope The scope of a variable is available within a function in which they are declared. The scope of a variable is available throughout the program.
Access Variables can be accessed only by those statements inside a function in which they are declared. Any statement in the entire program can access variables.
Life Life of a variable is created when the function block is entered and destroyed on its exit. Life of a variable exists until the program is executing.
Storage Variables are stored in a stack unless specified. The compiler decides the storage location of a variable.

11) What is the use of a ‘\0’ character?

It is referred to as a terminating null character, and is used primarily to show the end of a string value.

12) What is the difference between the = symbol and == symbol?

The = symbol is often used in mathematical operations. It is used to assign a value to a given variable. On the other hand, the == symbol, also known as “equal to” or “equivalent to”, is a relational operator that is used to compare two values.

13) What is the modulus operator?

The modulus operator outputs the remainder of a division. It makes use of the percentage (%) symbol. For example: 10 % 3 = 1, meaning when you divide 10 by 3, the remainder is 1.

14) What is a nested loop?

A nested loop is a loop that runs within another loop. Put it in another sense, you have an inner loop that is inside an outer loop. In this scenario, the inner loop is performed a number of times as specified by the outer loop. For each turn on the outer loop, the inner loop is first performed.

15) Which of the following operators is incorrect and why? ( >=, <=, <>, ==)

<> is incorrect. While this operator is correctly interpreted as “not  equal to” in writing conditional statements, it is not the proper operator to be used in C programming. Instead, the operator  !=  must be used to indicate “not equal to” condition.

16) Compare and contrast compilers from interpreters.

Compilers and interpreters often deal with how program codes are executed. Interpreters execute program codes one line at a time, while compilers take the program as a whole and convert it into object code, before executing it. The key difference here is that in the case of interpreters, a program may encounter syntax errors in the middle of execution, and will stop from there. On the other hand, compilers check the syntax of the entire program and will only proceed to execution when no syntax errors are found.

17) How do you declare a variable that will hold string values?

The char keyword can only hold 1 character value at a time. By creating an array of characters, you can store string values in it. Example: “char MyName[50]; ” declares a string variable named MyName that can hold a maximum of 50 characters.

18) Can the curly brackets { } be used to enclose a single line of code?

While curly brackets are mainly used to group several lines of codes, it will still work without error if you used it for a single line. Some programmers prefer this method as a way of organizing codes to make it look clearer, especially in conditional statements. 

19) What are header files and what are its uses in C programming?

Header files are also known as library files. They contain two essential things: the definitions and prototypes of functions being used in a program. Simply put, commands that you use in C programming are actually functions that are defined from within each header files. Each header file contains a set of functions. For example: stdio.h is a header file that contains definition and prototypes of commands like printf and scanf. 

20) What is syntax error?

Syntax errors are associated with mistakes in the use of a programming language. It maybe a command that was misspelled or a command that must was entered in lowercase mode but was instead entered with an upper case character. A misplaced symbol, or lack of symbol, somewhere within a line of code can also lead to syntax error.

21) What are variables and it what way is it different from constants?

Variables and constants may at first look similar in a sense that both are identifiers made up of one character or more characters (letters, numbers and a few allowable symbols). Both will also hold a particular value.  Values held by a variable can be altered throughout the program, and can be used in most operations and computations. Constants are given values at one time only, placed at the beginning of a program. This value is not altered in the program. For example, you can assigned a constant named PI and give it a value 3.1415  .  You can then use it as PI in the program, instead of having to write 3.1415 each time you need it. 

22) How do you access the values within an array?

Arrays contain a number of elements, depending on the size you gave it during variable declaration. Each element is assigned a number from 0 to number of elements-1. To assign or retrieve the value of a particular element, refer to the element number. For example: if you have a declaration that says “intscores[5];”, then you have 5 accessible elements, namely: scores[0], scores[1], scores[2], scores[3] and scores[4].

23) Can I use  “int” data type to store the value 32768? Why?

No. “int” data type is capable of storing values from -32768 to 32767. To store 32768, you can use “long int” instead. You can also use “unsigned int”, assuming you don’t intend to store negative values.

24) Can two or more operators such as \n and \t be combined in a single line of program code?

Yes, it’s perfectly valid to combine operators, especially if the need arises. For example: you can have a code like ” printf (“Hello\n\n\’World\’”) ” to output the text “Hello” on the first line and “World” enclosed in single quotes to appear on the next two lines. 

25) Why is it that not all header files are declared in every C program?

The choice of declaring a header file at the top of each C program would depend on what commands/functions you will be using in that program. Since each header file contains different function definitions and prototype, you would be using only those header files that would contain the functions you will need. Declaring all header files in every program would only increase the overall file size and load of the program, and is not considered a good programming style.

26) When is the “void” keyword used in a function?

When declaring functions, you will decide whether that function would be returning a value or not. If that function will not return a value, such as when the purpose of a function is to display some outputs on the screen, then “void” is to be placed at the leftmost part of the function header. When a return value is expected after the function execution, the data type of the return value is placed instead of “void”.

27) What are compound statements?

Compound statements are made up of two or more program statements that are executed together. This usually occurs while handling conditions wherein a series of statements are executed when a TRUE or FALSE is evaluated. Compound statements can also be executed within a loop. Curly brackets { } are placed before and after compound statements.

28) What is the significance of an algorithm to C programming?

Before a program can be written, an algorithm has to be created first. An algorithm provides a step by step procedure on how a solution can be derived. It also acts as a blueprint on how a program will start and end, including what process and computations are involved.

29) Write a program to swap two numbers without using the third variable?

#include<stdio.h>      

#include<conio.h>      

main()      

{      

int a=10, b=20;    //declaration of variables.  

clrscr();        //It clears the screen.  

printf("Before swap a=%d b=%d",a,b);        
   
a=a+b;//a=30 (10+20)       

b=a-b;//b=10 (30-20)      

a=a-b;//a=20 (30-10)      

printf("\nAfter swap a=%d b=%d",a,b);      

getch();      

}

30) What would happen to X in this expression: X += 15;  (assuming the value of X is 5)

X +=15 is a short method of writing X = X + 15, so if the initial value of X is 5, then 5 + 15 = 20.

31) What is wrong in this statement?  scanf(“%d”,whatnumber);

An ampersand & symbol must be placed before the variable name whatnumber. Placing & means whatever integer value is entered by the user is stored at the “address” of the variable name. This is a common mistake for programmers, often leading to logical errors.

32) How do you generate random numbers in C?

Random numbers are generated in C using the rand() command. For example: anyNum = rand() will generate any integer number beginning from 0, assuming that anyNum is a variable of type integer.

33) What could possibly be the problem if a valid function name such as tolower() is being reported by the C compiler as undefined?

The most probable reason behind this error is that the header file for that function was not indicated at the top of the program. Header files contain the definition and prototype for functions and commands used in a C program. In the case of “tolower()”, the code “#include <ctype.h>” must be present at the beginning of the program.

34) What are comments and how do you insert it in a C program?

Comments are a great way to put some remarks or description in a program. It can serves as a reminder on what the program is all about, or a description on why a certain code or function was placed there in the first place. Comments begin with /* and ended by */ characters. Comments can be a single line, or can even span several lines. It can be placed anywhere in the program.

35) What is debugging?

Debugging is the process of identifying errors within a program. During program compilation, errors that are found will stop the program from executing completely. At this state, the programmer would look into the possible portions where the error occurred. Debugging ensures the removal of errors, and plays an important role in ensuring that the expected program output is met.

36) Write a program to print Fibonacci series without using recursion?

#include<stdio.h>    

#include<conio.h>    

void main()    

{    

 int n1=0,n2=1,n3,i,number;    

 clrscr();    

 printf("Enter the number of elements:");    

 scanf("%d",&number);    

 printf("\n%d %d",n1,n2);//printing 0 and 1    

 for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed    

 {    

  n3=n1+n2;    

  printf(" %d",n3);    

  n1=n2;    

  n2=n3;    

 }    

getch();    

}

38) What does the format %10.2 mean when included in a printf statement?

This format is used for two things: to set the number of spaces allocated for the output number and to set decimal places. The number before the decimal point relates to the allocated space, in this case it would allocate 10 spaces for the initial number. If the space occupied by the output number is less than 10, additional space characters will be inserted before the actual output number. The number after the decimal point determines the number of decimal places, in this case they are 2 decimal places.

39) What are logical errors and how does it differ from syntax errors?

Program that contains logical errors tend to pass the compilation process, but the resulting output may not be the expected one. This happens when a wrong formula was inserted into the code, or a wrong sequence of commands was performed. Syntax errors, on the other hand, deal with incorrect commands that are misspelled or not recognized by the compiler.

40) What is pointer to pointer in C?

In the case of the pointer to pointer concept, one pointer refers to the address of another pointer. A pointer to a pointer is a chain of indicators. Basically the pointer contains the address of the variable. The pointer to the pointer contains the address of the first pointer. We understand this concept on an example:

#include <stdio.h>  

 int main()  

{  

    int a=10;  

    int *ptr,**pptr; // *ptr is a pointer and **pptr is a double pointer.  

    ptr=&a;  

    pptr=&ptr;  

    printf("value of a is:%d",a);  

    printf("\n");  

    printf("value of *ptr is : %d",*ptr);  

    printf("\n");  

    printf("value of **pptr is : %d",**pptr);  

    return 0;  

}

41) What is || operator and how does it function in a program?

The || is also known as the OR operator in C programming. When using || to evaluate logical conditions, any condition that evaluates to TRUE will render the entire condition statement as TRUE.

42) Can the “if” function be used in comparing strings?

No. “if” command can only be used to compare numerical values and single character values. For comparing string values, there is another function called strcmp that deals specifically with strings.

43) What are preprocessor directives?

Preprocessor directives are placed at the beginning of every C program. This is where library files are specified, which would depend on what functions are to be used in the program. Another use of preprocessor directives is the declaration of constants.Preprocessor directives begin with the # symbol.

44) What will be the outcome of the following conditional statement if the value of variable s is 10?

s >=10 && s < 25 && s!=12

The outcome will be TRUE. Since the value of s is 10, s >= 10 evaluates to TRUE because s is not greater than 10 but is still equal to 10. s< 25 is also TRUE since 10 is less then 25. Just the same, s!=12, which means s is not equal to 12, evaluates to TRUE. The && is the AND operator, and follows the rule that if all individual conditions are TRUE, the entire statement is TRUE.

45) Describe the order of precedence with regards to operators in C.

Order of precedence determines which operation must first take place in an operation statement or conditional statement. On the top most level of precedence are the unary operators !, +, – and &. It is followed by the regular mathematical operators (*, / and modulus % first, followed by + and -). Next in line are the relational operators <, <=, >= and >. This is then followed by the two equality operators == and !=. The logical operators && and || are next evaluated. On the last level is the assignment operator =.

46) Write a program to check the prime number in C Programming?

#include<stdio.h>      
#include<conio.h>      

void main()      

{      

int n,i,m=0,flag=0;    //declaration of variables.  

clrscr();    //It clears the screen.  

printf("Enter the number to check prime:");      

scanf("%d",&n);      

m=n/2;      

for(i=2;i<=m;i++)      

{      

if(n%i==0)      

{      

printf("Number is not prime");      

flag=1;      

break;    //break keyword used to terminate from the loop.  

}      

}      

if(flag==0)      

printf("Number is prime");      

getch();    //It reads a character from the keyword.  

}

47) How do you determine the length of a string value that was stored in a variable?

To get the length of a string value, use the function strlen(). For example, if you have a variable named FullName, you can get the length of the stored string value by using this statement: I = strlen(FullName); the variable I will now have the character length of the string value.

48) Is it possible to initialize a variable at the time it was declared?

Yes, you don’t have to write a separate assignment statement after the variable declaration, unless you plan to change it later on.  For example: char planet[15] = “Earth”; does two things: it declares a string variable named planet, then initializes it with the value “Earth”.

49) Write a program to reverse a given number in C?

#include<stdio.h>      
#include<conio.h>      

main()      

{      

int n, reverse=0, rem;    //declaration of variables.  

clrscr(); // It clears the screen.     

printf("Enter a number: ");      

scanf("%d", &n);      

while(n!=0)      

{      

     rem=n%10;      

     reverse=reverse*10+rem;      

     n/=10;      

}      

printf("Reversed Number: %d",reverse);      

getch();  // It reads a character from the keyword.    

}

50) Why is C language being considered a middle-level language?

This is due to the fact that the C language is rich in functions, thanks to which it behaves like a high-level language, and at the same time can interact with the equipment using low-level methods. The use of a well-structured approach to programming in conjunction with the English words used in functions makes it work as a high-level language. On the other hand, C can directly access memory structures similar to assembly language procedures.

Leave a Reply

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

Shopping cart