Top 25 C Programming Interview Questions & Answers

is a general-purpose programming language and though it is an old language, it is commonly used by businesses especially, enterprise applications where performance is the priority. 

In this article, we will focus on the top commonly asked C programming interview questions into the below sections: 

  1. Data Types and Variables 
  2. Arrays, Strings, Functions, Operators 
  3. Memory Management and Pointers 
  4. Libraries – File handling  

Data Types and Variables 

What are the basic data types in C? 

The basic data types in C are: 

  1. Integer(int) 
  2. Floating Point(float) 
  3. Character(char) 
  4. Void 

Integer(int)
It is used to store whole numbers, may have below types depending on their sign and size: 

  • int/signed int 
  • unsigned int 
  • short int/ signed short int 
  • unsigned short int 
  • long int/ signed long int 
  • Unsigned long int

Floating Point(float)
They are used to store real numbers(with decimal points). Floating point data types in C  have below types based on size:

  • float 
  • double 
  • long double 

Character(char)
They are used to store character values and can have below types based on sign: 

  • char/signed char 
  • unsigned char 

Void
The void data type is used to depict “No value” when a function returns nothing to the calling function, it is specified using Void.

What is the range of value that an ‘int’ data type variable can hold?

What is the enumerated data type in C? 

The enumerated data type is specified using keyword-’enum’. It is used for creating user-defined data types which have integral constants as their elements. 

In the below example we have created an enum data type ‘week’ which consists of integral constants – Sunday, Monday, Tuesday, etc. 

In the main program, we have created a variable called ‘today’ of enum data type ‘week’. This ‘today’ variable may store any one of the integral constants from the enum data type ’week’. 

Enumeration example
Image from programiz.com

Explain the String functions available in the C language

C provides many string functions in its standard library header file- ‘string.h’. The most commonly used string functions are: 

 

What is typecasting in C? 

Typecasting referto converting a variable’s data type into another data type explicitly by the programmer.

Type conversion can be performed in two ways: 

Implicit type conversion: 

Performed by the C compiler automatically, without any manual/programmer indication.  

Example: It is performed by the compiler during the execution of an expression where two or more data types are present. The small data types are converted into large data types so that data is preserved without any loss.

Implicit type conversion in C
Image from developerinsider.co

Explicit type conversion or typecasting

It is deliberately performed by the programmer in the code by mentioning the required data type and this is known as typecasting in C 

Example: In the below example, we have deliberately changed the type of ‘c’ from float to int by writing the expected data type in brackets before the variable name ’c’. 

Typecasting in C
Image from tutorialspoint.com

What is the difference between local and global variables? 

What is the difference between an actual parameter and a formal parameter? 

Example: 

Formal and Actual parameters
Image from quora.com

 

How can we store a negative integer? 

To derive a negative integer we calculate two’s complement of the positive number. 

This can be done in a two-step process- 

  • Calculate one’s complement of the number. 
  • Add one to the complement to receive the negative number. 

  Example 

               Number 6 in binary is  0110 

               Number 6’s one’s complement is 1001 

               Add one to this 1001+1 = 1010 which is -6. 

 

Arrays, Strings, Functions, Operators

What is the difference in data storage between an array and a pointer?  

Below are the differences between an array and a pointer in storage: 

 

What is the difference between call by value and call by reference?

Which function is used to compare two strings? 

strcmp() can be used to compare two strings character by character and return an integer.  It starts comparing the two strings character by character until the end of the string is reached which is null(‘\0’) or there is a mismatch. 

Return value from strcmp()
Image from programiz.com

What is the difference between a String and Array? 

Below are the differences between a String and Array in C: 

Can we convert a number into String and vice-versa? 

  • We can use itoa() function to convert an integer data type into a string data type. 
Prototype:  

char *  itoa ( int value, char * str, int base ); 

Where : 

value is the integer to be converted to a string 

str is the resulting char array 

base is the numerical base of the integer value 

Returns a pointer to the char string.
  • Similarly, we can use atoi() function to convert a string data type into an integer data type. 
Prototype: 

int atoi (const char * str); 

Where  

str  is a string constant which is received 

Returns the integer if the string is successfully converted, otherwise returns zero.

Explain the difference between strncat() and strcat() in C. 

Find code snippets describing the differences between strncat() and strcat() below: 

strcat(source, destination);
Example:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char src[]="Bombay";
    char city[]="+Nagpur";
    printf("Before concatenation destiona string = %s\n", src );
    strcat(src, city);
    printf("After concatenation destination string = %s", src );
}

What are the different operators in C programming language? 

There are 6 types of operators in C language:

Arithmetic

To perform arithmetic operations we use arithmetic operators. 

Example:  

Relational

To perform relational operations between operands. 

Example: 

Logical

To perform logical operations between operands. 

Example: 

Bitwise

To perform bit-by-bit operations on bits. 

Example: 

Assignment

To assign value to a variable. 

Example: 

Special 

These are special or miscellaneous operators. 

Example: 

 Read in detail about the operators present in the C language here. 

 

What is the difference between usage of the increment operator as ‘++i’ and ‘i++’? 

If we are using the operator ++ only with the operand and without assigning the value anywhere then in such cases, there is no difference in the usage and value of the operand. 

Example 1: 

i=5; 

i++; 

printf(“%d”, i); 

Output is : 6

Example 2: 

i=5; 

++i; 

printf(“%d”, i); 

Output again is: 6

The difference arises when we assign the operand into another variable after using the increment operator. This happens because operator precedence works on the expression and provides different output. 

Example 3: 

i=5; 

j=i++; 

printf(“%d”, i);

Now, in this case, due to the fact that ‘=’ has higher precedence than postfix ‘++’, first the assignment is executed. So j is assigned the value of i, i.e. 5. After this assignment ‘++’ is executed, so the value of i is now 6. 

i=6 and j=5. 

Example 4: 

i=5; 

j=++i; 

printf(“%d”, i);

Now, in this case, due to the fact that the prefix ‘++’ has higher precedence than ‘=’, first the ‘++’ is executed. So i is incremented first and the value of i is 6. Now the next operation of assignment is executed, j is assigned the value of i , i.e. 6. 

i=6 and j=6. 

What are bitwise operators in C language? 

Below are the bitwise operators in C with examples: 

Let’s assume A = 60, in binary : 0011 1100 

B =13, in binary : 0000 1101 

examples of bitwise operators
Image from tutorialspoint.com

 

Memory Management and Pointers 

What is a memory leak in C? 

While writing an application, the developer has allocated some memory which he missed to deallocate after use. This results in unused allocated memory – and such phenomenon is referred to as a memory leak in C. Unused allocated memory could affect the system’s performance and cause the speed to be slower. As a best practise, it is important to release all allocated memory after usage

Explain the difference between calloc() and malloc() functions.

malloc() and calloc() are functions to allocate memory dynamically at runtime. Below are the differences between the two:

Explain the advantages and disadvantages of pointers in C

Advantages of pointers in C: 

  • Pointers provide direct access to memory locations. 
  • They provide an alternate and better way to handle arrays. 
  • Pointers provide us the feature to dynamically allocate and deallocate memory. 
  • They help us to create and use complex data structures like trees, graphs, linked lists, stack, queue, etc. 

Disadvantages of pointers in C:

  • They are a complex feature to understand. 
  • If an incorrect value is updated in memory using pointers, this causes memory corruption.
  • If not properly deallocated, pointers may cause a memory leak. 
  • They may cause a memory segmentation fault(accessing a non-existent memory location) if left uninitialised.’ 

 You may read more about pointers here. 

What is a dangling pointer in C language? 

dangling pointer in C  is a normal pointer which is pointing to a memory location that is either deleted or deallocated. 

In the below example the pointer ‘ptr’ is pointing to location ‘a’ which has address ‘5000’. Now later in the program, we released ‘a’ because it is a local variable of a function, but the ‘ptr’ is still pointing to ‘a’. Hence, ‘ptr’ is a dangling pointer now.

Dangling pointer in C
Image from stackoverflow.com

What is the difference between static and dynamic memory allocation in the C language? 

What is the difference between NULL and VOID pointers?

A null pointer is a pointer to any data type which is assigned a NULL value. 

Void pointer is a data type itself, which remains void until some address reference is assigned to it. When we do not know the data type of the value which the pointer will point to, then we use a Void pointer. 

Below are the differences between NULL and VOID pointers. 

Libraries – File handling  

What are the different file functions in C? 

What is the difference between Random Access File and Sequential File in C?

When we want to access data from a sequential file, we need to traverse the data sequentially from the start till we reach the required data record. 

However, in a random access file, we can access the data randomly, without traversing the whole file. Therefore, random access files are faster to access, read and modify data. 

Explain the different file opening modes in C

Below are the different file opening modes in C: 

File opening modes
Image from guru99.com

Why do we use the rewind() function in C?

Rewind() is used to set the file pointer to the beginning of the file. 

Syntax: 

void rewind(FILE *stream); 

This function does not return any value. We should include the ‘stdio.h’ header file in the program to use this function.

 

Related Articles

Responses