C Programming Interview Questions and Answers

In this tutorial, we will discuss C interview questions with their answers. These questions are from the core and important concepts of the C programming language. To attempt these questions rightly, you will have to master the basic concepts of the C programming language.

We have divided the C Interview question answers into 3 parts as C Interview questions for beginners, C Programming Interview Questions for intermediate and Advance C Language Interview Questions.

If you read through these questions and also give the right answers to these following questions then it will help you to prepare better for placement and interviews. These questions are based on different types of concepts in C. It will help you in getting placed at big companies such as TCS, Infosys, Wipro, Accenture etc.

C programming Interview Questions for Beginners

Below are the C interview questions with answers for beginners.

Q.1 What is the output of the following program?

#include <stdio.h>
int main(void)
{
int data;
int  val= 1;
int arr[4] = { 2, 3, 1, 4};
data = 4 * 4 + arr[val++] - (9 / val);
printf("%d", data);
return 0;
}

Ans. 15

Q.2 What is the use of ‘\b’ in the following program?

#include <stdio.h>
int main(void)
{
printf("Advance\be");
return 0;
}

Ans. The ‘\b’ is an escape sequence that is used to move the cursor backwards. It normally does what backspace does on your keyboard. The above will print Advance to the screen.

Q.3 If you open a file through the fopen() function then which parameters will fseek() require to work with that file?

Ans. First, the number of bytes to search for. Second, the point of origin of that file and third, a file pointer to that file.

fopen() function
The main purpose of the fopen() function is to open a file to perform several operations which includes reading, writing, editing etc.

Syntax

FILE *fopen(const char *filename, const char *operation_mode);

The function returns null if the execution does not succeed and if it succeeds then it returns a pointer to FILE.

fseek() function
The main purpose of the fseek() function is to set the file pointer of the stream to a given offset. To put it short, you can use this function for writing data into a file at a specified location.

Syntax

int fseek(FILE *stream, long int offset, int pos) 

Q.4 What is the output of the following program?

#define add(data) data+data
int main()
{
int data;
data = 36/add(10);
printf("%d",data);
return 0;
}

Ans. 13

Q.5 Suppose, a block of memory is allocated. Can you free that block of memory which is allocated? If yes, how?

Ans. Yes, you can free the previously allocated block of memory with the help of free() function.

free() function in C
The free() function is mainly used to deallocate a block of memory which was allocated previously. It calls malloc, calloc or realloc to perform the task. And it does not return any value.

Declaration

void free(void *pointer)

Example:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   char *st;

   st = (char *) malloc(20);
   strcpy(st, "C");
   printf("%s => %u\n", st, st);

   st = (char *) realloc(st, 20);
   strcat(st, " Quiz");
   printf("%s => %u", st, st);
   free(st);
   
   return(0);
}

Output:-
C => 27021328
C Quiz => 27021328

Q.6 In C programs, why is it a bad practice to make use of the gets() function?

Ans. The problem in using the gets() function is that it does not know how big the buffer is. That’s why it continues to read until it finds a newline or encounters EOF and it may result in buffer overflow.

gets() function
It reads a line from stdin and it stores the line into the string that is pointed by the str. It is a bad practice to use the gets() function in your program code. Because it keeps reading the line until it reaches a newline character.

Declaration

char *gets(char *str)

Example:-

#include <stdio.h>

int main () {
   char st[50];

   printf("Enter something: ");
   gets(st);
   printf("%s", st);
   return(0);
}

Output:-
Enter something: Nice Day
Nice Day

Q.7 Why is the following program dangerous?

#include<stdio.h>
int main(int argc, char** argv)
{
char buffer[100];
strncpy(buffer, argv[1], 100);
printf(buffer);
return 0;
}

Ans. In that above program, we pass an attacker-controlled buffer as an argument to a printf() function. With this, the attacker can perform writing to arbitrary memory addresses. You must use the format string to determine the number of arguments in the printf() function.

Q.8 What is the output of the following program?

#include <stdio.h>
int main()
{
  int arr[3] = {2,5,9};
  int *pt = (int*)(&arr+1);
  printf("%d",*(pt-1));
  return 0;
}

Ans. 9

Q.9 What is the output of the following program?

int main(){
float  d ;
int a=20,b=3;
d = ( int ) a / b ;
printf("%f",d);
return 0;
}

Ans. 6.000000

Q.10 Can you tell me the errors that occurred on the following program?

#include <stdio.h>
int main()
{
int b=20,a=20;
int res = add(b,a);
printf("%d",res)
return 0;
}

Ans. The function add() is not defined and there is no semicolon at the end of the printf() function.

Q.11 In any C program, can you execute that program without including any header files into it?

Ans. In some compilers, yes you can. But it will give you a warning to include the following header file to the program code. But in some compilers, you can’t. Because you will have to include a header file named stdio.h.
If you are using some predefined functions such as free(), strcat(), time() etc then you have to use their own header files in which they are defined.

Example:-

int printf(const char *format, ...);
int main()
{
  printf( "Have a nice day!" );
  return 0;
}

Output:-
Have a nice day!

Q.12 What is the output of the following program?

int main()
{
 char *point = "Amen";
 printf("%c", *&*&*point);
 return 0;
}

Ans. A

Q.13 What is the output of the following program?

#include <stdio.h>
int main()
{
while (printf("Have a good day!"))
return 0;
}

Ans. Have a good day!!

Intermediate Level C Interview Questions

Q.14 Can you tell me the ways to convert a string to integer value?

Ans. First Method:-
You can use the existing atoi() function in C. The main purpose of this is to convert the string argument to an integer.

Declaration

int data = atoi(string1);

Example:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   int data;
   char st[20];
   strcpy(st, "123");
   data = atoi(st);
   printf("%d",data);

   return(0);
}

Output:-
123

Second Method:-
You can also use the sscanf() function. The main purpose of this function is to read formatted input from a string.

Declaration

int sscanf(const char *str, const char *format, ...)

Example:-

sscanf(string1,”%d”,&data)

Q.15 Suppose, you included the same header files twice in your program. Then, how can you avoid including the same header files twice in your program code?

Ans. You can make use of the ifndef and define preprocessor directives. Suppose, the header file is header.h then you can include the header file like below:-

#ifndef header.h
#define header.h
#endif

In the above, the ifndef will check if the header.h is defined or not. If it is not then it will define the header.h file.

Q.16 What is the output of the following program?

#include<stdio.h>
int main()
{
    for(;;)
    printf("Hi, John Here!");
    return 0;
}

Ans. The above program will print the Hi, John Here! text infinitely.

Q.17 Can you tell the difference between const char* ptr and char const* ptr?

Ans.

NOTE:- A pointer should always point to the same address but the value at the location is changed.

const char* ptr
The character is pointed by the pointer variable ptr. You cannot change the value but you can initialize the ptr with another memory location.

Example:-

#include<stdio.h>
#include<stdlib.h> 
int main()
{
  char x ='X', y ='Y';
  const char *pt = &x;
  printf( "The value pointed by pt-> %c\n", *pt);
  pt = &y;
  printf( "The value pointed by pt-> %c\n", *pt);
}

Output:-
The value pointed by pt-> X
The value pointed by pt-> Y

char const* ptr
The pointer ptr is constant. You cannot assign ptr with another memory location but you can change the value of the character pointed by ptr.

Example:-

#include<stdio.h>
#include<stdlib.h> 
int main()
{
  char x ='X', y ='Y';
  char *const pt = &x;
  printf( "The value pointed by pt: %c\n", *pt);
  printf( "pointing to: %d\n", pt);
  *pt = y;
  printf( "The value pointed by pt: %c\n", *pt);
  printf( "pointing to: %d\n", pt);
}

Output:-
The value pointed by pt: X
pointing to: 936799398
The value pointed by pt: Y
pointing to: 936799398

Q.18 How can you allocate a single block of memory dynamically in the C programming language?

Ans. In C, you can make use of malloc() function to allocate a single block of memory dynamically in C. This function is defined in the stdlib.h header file. It returns a pointer to the allocated memory.

Syntax of malloc()

ptr = (cast-type*) malloc(byte-size)

Example:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   char *st;

   st = (char *) malloc(20);
   strcpy(st, "Good");
   printf("%s => %u\n", st, st);

   st = (char *) realloc(st, 20);
   strcat(st, " Day");
   printf("%s => %u", st, st);
   free(st);
   
   return(0);
}

Output:-
Good => 36782096
Good Day => 36782096

Q.19 Suppose, you created a file named Tech.txt. Then what will be the name of that file after executing the below program?

#include <stdio.h>
int main()
{
int res;
char o[] = "Tech.txt";
char n[] = "random.txt";
res = rename(o, n);
if (res == 0)
puts("success");
else
perror("Error");
return 0;
}

Ans. random

Q.20 Can you tell me the errors occurred on the following program?

#include <stdio.h>
int main()
{
int b=20,a=20;
int res = add(b,a);
printf("%d",res)
return 0;
}

Ans. The function add() is not defined and there is no semicolon at the end of the printf() function.

Q.21 Write a code to print the following pattern?

####
###
##
#

Ans.

#include <stdio.h>
int main() {
int i, j, rows=4;
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("#");
}
printf("\n");
}
return 0;
}

Q.22 What is the output of the following program?

#include <stdio.h>
int main()
{
typedef struct Tech{
char string[14];
int data;
}tech;
tech s1, s2 = { "B\bBook!", 14};
s1 = s2;
s1.string[0] = 't';
printf("%s %d", s1.string, s1.data);
return 0;
}

Ans. Book! 14

Q.23 You can include header files into the C program in 2 ways using <> and “”. What is the difference between <> and “”?

Ans. If you declare the header file using <> then the compiler will search for that header file in the built-in library. But if you declare the header file using “” then the compiler will search for that header file in the current working directory.

We have created our own header file named header.h.

header.h

#ifndef header_file  
#define header_file  
int x = 22;  
#endif

Below is the source code which uses the header file:-

#include <stdio.h>  
#include "header.h"   
int main()  
{
printf("Value of x: %d", x);  
return 0;  
}

Output:-
Value of x: 22

Q.24 In C, can you add pointers to each other?

Ans. No, you cannot add pointers together. It is not possible. Pointers contain addresses and you cannot retrieve the value out of this operation. If you add addresses then it won’t make sense. But you can subtract two addresses to find the offset between those addresses. It is a very useful one. You can also crash the code by adding pointers or it can result in overflow.

Advance C Language Interview Question Answer

Q.25 Is it possible to print a string with the % symbol in it? If yes, how?

Ans. Yes, you can.

To print ‘%’, you have to use ‘%%’. Follow the code below:-
printf(“This shirt has 30%% discount!”);

Q.26 Can you tell what the below statement is doing?

typedef int REAL, *REALP, ONE[10];

Ans. “REAL a” is used to define a type int. “REALP *b” would define pointer b of type int **. “ONE a1” would define a1 as an array of 10 int.

Q.27 Can you describe what the below expression is doing?

struct {int a[3];} arr[] = {2,1,3};

Ans. The above expression will create an array arr of 1 element. Then each of the elements of arr contain a struct field of int array of 3 elements. arr[0].a[0] is 2, arr[1].a[1] is 1 and arr[2].a[2] is 3. There is nothing wrong with the expression.

Q.28 Is the below expression correct?

int arr[40] = {1,2,3,[37]=38,39,40};

Ans. Yes, the above expression is correct. The above statement will initialize arr[1], arr[2], arr[3] …, arr[38], arr[39], arr[40] to 1,2,3,4,…,38,39,40. And the remaining elements of that array will be initialized to 0.

Q.29 What is the output of the below program code?

#include <stdio.h>
int fun(int num)
{
   static int i = 1;
   if (num >= 5)
  	return num;
   num=num+i;
   i++;
   return fun(num);
}
int main(){
  int n = fun(2);
  printf("%d",n);
}

Ans. 5

Q.30 For comparing strings in C, can you use the if function?

Ans. No, you cannot use the if function to compare strings. It is only used to compare numeric and single character values. For comparing strings, you can use the strcmp() function.

Declaration

int strcmp(const char *s1, const char *s2)

If the return value of the function is less than 0 then s1 is less than s2 and if it is greater than 0 then s2 is less than s1. And it is equal to 0 then s1 and s2 both are the same.

Example:-

#include <stdio.h>
#include <string.h>
int main() {
  char s1[] = "abcd", s2[] = "abCd";
  int result;
  result = strcmp(s1, s2);
  printf("%d\n", result);
  return 0;
}

Output:-
1

Q.31 Are the uninitialized pointer and the null pointer the same in C?

Ans. Null pointer is a pointer which points to nothing. Value of the null pointer is 0. Below we made a pointer to point to null:-

int *ptr = NULL;
char *ptr = NULL;

The uninitialized pointer is also known as a wild pointer. This pointer points to an arbitrary memory location. It is a bad practice to use the uninitialized pointer. It can have a valid pointer value in it.

Example:- null pointer

#include <stdio.h>  
int main()  
{  
int *ptr=NULL;  
printf("value => ptr: %d",*ptr);  
return 0;  
} 

The above program will not print anything back to the screen because of the null pointer.

Example:- uninitialized pointer

int main()
{
int *p;
int x = 10;
return 0;
}

Q.32 What will be the output of the following program?

#include <stdio.h>
int main()
{
  int a = 0xf;
  int b = 0x63;
  printf("%x",(a^b));
  return 0;
}

Ans. 6c

Q.33 What is the output of the following program?

#include <stdio.h>
#define A 20
#define B 5
int main()
{
int (*ptr)[A][B];
printf("%d",  sizeof(*ptr));
return 0;
}

Ans. 400

Q.34 Is it possible to override a defined macro? If yes, then how?

Ans. Yes, you can. For this, you have to use #ifdef and #undef preprocessors. Follow the code below:-

#ifdef A
#undef A
#endif
#define SIZE 5

Above, we defined a macro named SIZE. It can be undefined using the #undef and it can be define again using the #define.

Q.35 What is the output of the following program?

#include <stdio.h>
int foo(unsigned int n, unsigned int r) {
  if (n  > 0) return (n%r +  foo (n/r, r ));
  else return 0;
}
int main()
{
int res;
res = foo(340,10);
printf("%d",  res);
return 0;
}

Ans. 7

Summary

In this tutorial, we discussed some interview or job level questions of the C programming language and we also gave their answers. These questions will help you to prepare better for the interview. And you will also get better knowledge of the C programming language through this.