Interview Questions On C

Introduction

In this article, you can find more useful interview questions on c programming. If you master all these questions, I can guarantee you that you will definitely get success in all type of c programming interviews.

Also Read: The while loop in C Programming

So there are thousands of interview questions on c programming. It is not possible to write all these interview questions on c language all at once. Maybe you will find these questions easy.

Q. 1 What is the difference between for loop and while loop?

Before using for loop, we must know the number of iterations. But, in case of while loop, there is no need to know in advance about the number of iterations.

So, Let’s see the example,

for(i=1;i<=10;i++)
{
   printf("\nInterview Questions on C");
}

After compiling and running this program, the string “Interview Questions on C” will be printed 10 times on console. So, we can easily see that we have specified the number of iterations in advance. As a result of this, for loop has some limitations.

For example

while((ch=fgetc(fp))!=EOF)
{
    printf("%c",ch);
}

In the above example of while loop, we are reading a file character by character and storing that character in the variable ch. When the value of the variable ch is EOF, then and only then this while loop will be terminated. So, here we don’t know how many iterations are there. It depends on the file that we are reading.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

So, this is the reason, most programmer would prefer while loop instead of for loop. But, let me tell one thing, we can use for loop like while loop.

For example,

for(;(ch=fgetc(fp))!=EOF;)
{
    printf("%c",ch);
}

As you can see, we have used for loop just like while loop. But, this does not look good. Because for loop has its own advantage and while loop has its own. So, it is better to use them differently.

Q.2 What is the difference between the while and do-while loop?

Such type of questions are the most common interview questions on c programming. The first and the main difference is the syntax of both while and do-while loop. So, let’s see the following syntax.

//while loop
while(expression)
{
   body of while loop
}
//do-while loop
do 
{
   body of do loop
}
while(expression);

In the while loop, the condition expression is evaluated first and then the body of this while loop will be executed. But, in the do-while loop, the body of the do loop will be executed first and then condition expression will be evaluated.

Also Read: Reverse a Number in C

Q. 3 List and explain at least eight string handling functions.

  1. strcat(): This function concatenates second string to the first string. For example, strcat(s1,s2) concatenates s2 at the end of the s1. The s1 and s2 are two strings.
  2. strncat(): When we need to concatenate two strings up to length ‘n’, then we can use this function. For example, strncat(s1,s2,3) concatenates the first 3 characters s2 at the end of the s1.
  3. strcpy(): This is the string copy function. It copies the contents of string2 to string1. For example, strcpy(s1,s2) copies contents of s2 to s1.
  4. strncpy(): This is same as strcpy(). There is the only difference and that is this function copies only first ‘n’ characters of s2 to the s1.
  5. strcmp(): As the name suggest, strcmp() means string comparison. By using this function we can compare two strings. That means which string is greater or less or equal. This function will return an integer value. For example, strcmp(s1,s2). If this function returns the value which is less than 0, that means string s1 is less than string s2. And if this value is greater than 0, then s1 is greater than s2 and both the strings will be equal if this function returns the value equal to 0;
  6. strncmp(): It is same as strcmp(). The comparison is in between two string up to ‘n’ characters.
  7. strcmpi() : Here, ‘i’ means ignore case. This function will say “Vijay” and “viJay” are the same.
  8. strncmpi(): It is the combination of strncmp() and strncmpi(). This function compares only the first ‘n’ characters and ignores the case.

Q. 4 What is the use of strchr() function?

This is the string handling functions in c programming. It finds the first occurrence of a character in a given string. Basically, it will find the first location of the character in a given string.

Q. 5 What is recursion?

It is the process in which functions calls itself. For example,

int main()
{
    printf("Hi");
    main(); // main() is calling main() function.
}

Q. 6 What is Address of operator?

The ‘&’ the address-of operator. For example, suppose we have one variable i.e.a. So, printf(“%u”,&a); will print the address of the variable a to the console.

Q. 7 What is the pointer variable?

There are various occasions where we want to store the address of one variable to another variable. In such type of situations, we can use the pointer variable. Means pointer is the variable which stores the address of another variable. For example,

int main()
{
    int a = 5;
    int *ptr;
    ptr = &a;    // ptr contains the address of the variable a
}

Q. 8 What are the meaning of actual and formal parameters?

For example,

int main()
{
    int
    add(a,b);   //calling function
}
int add(int x, int y)   // called function
{
}

In the above code, actual arguments are a and b . The arguments which can be passed from the calling function, are known as actual arguments. The arguments or parameters written inside the called function or function definition, are known as formal arguments or parameters. Here, x and y are formal parameters.

Also Read: Factorial Program in C Programming

Q. 9 What is the difference between call by value and call by reference?

In a call by value, we can pass the values and In call by reference, we can pass addresses of the variable.

In call by value, if we make any changes in formal arguments, then there will not be any change in actual arguments. But, in call by reference, whatever changes we do in formal arguments, you can see the changes in actual arguments.

For example,

int main()
{
  ------------
  swapv(a,b);  // call by value
  swapr(&a,&b); // call by reference
  -----------
}
void swapv(int x, int y)
{
}
void swapr(int *x, int *y)
{
}

Q. 10 What is the difference between printf() and fprint()?

When we want to display formatted output to the console, then print() can be used. The fprintf() is a file handling function. So, we can write formatted output to the file using fprintf() function.

Q. 11 How to display a anything without using semicolon?

For example,

int main()
{
   if(printf("Interview Questions on C Programming"))
   {
   }
  return 0;
}

In the above c programming code, we have used printf() statement inside the if condition. That is why there is no need to write semicolon at the end of the printf() statement. If we write the printf() statement independently, then semicolon is necessary.

Also Read: LCM of Two Numbers in C Programming

The output of the above code is: Interview Questions on C Programming.

Q. 12 What is sscanf() function in c?

There are very tricky interview questions on c programming and this is question is one of them. Most people get confuse between scanf() and sscanf() functions.

The function scanf() reads formatted data from the console with the help of keyboard. The sscanf() also reads formatted data but not from the console and keyboard. Then how is it possible? It reads formatted data from the string which is already declared and initialized in the program.

For example,

#include<stdio.h>
#include<string.h>
int main()
{
    char *str = "Interview Questions on C";
    char str1[15], str2[15], str3[15], str4[15];
    sscanf(str, "%s %s %s %s", str1,str2,str3,str4);
    printf("String 1: %s", str1);
    printf("String 2: %s", str2);
    printf("String 3: %s", str3);
    printf("String 4: %s", str4);
    return 0;
}

In the above code, we have already declared and initialized one string i.e. “Interview Questions on C” and we have used sscanf() function to read formatted data from this string.

Output of this C program:

String 1: Interview String 2: Questions String 3: on String 4: C

Q. 13 What is void pointer?

The void means the absence of any data type. The integer pointer stores address of integer variable, float pointer stores address of float variable and so on. But we can store the address of any data type in the void pointer. The void pointer can not be dereferenced directly.

For example, see the following the program

int main()
{
   int a = 5;
   float f = 2.5;
   void *vptr;
   vptr = &a;//This is called referencing
   /* printf("a = %d", *vptr); Error, because dereferencing is not possible with void pointer.*/
   printf("a = %d", (int*)vptr); // Explicit casting
   vptr = &f;
   printf(" f = %f", (float*)vptr);
   return 0;   
}

Output of this program : a = 5 f = 2.5

Q. 14 What is the difference between array and structure?

You can find the number of interview questions on c structures and arrays. The array is a variable where we can store a similar type of data, but the structure is also a variable where we can store dissimilar type of data.

Q. 15 Which function can we use to generate random numbers?

Such kind of interview questions on c programming is not so easy. Because no one teaches you these tricky questions in college.

Also Read: GCD of Two Numbers in C Programming

To generate random numbers in c programming, we can use rand() function. This function will return pseudo-random numbers. The range of these numbers is in between 0 and RAND_MAX. The value of RAND_MAX is a platform-dependent.

Q. 16 How can we open a batch file using c program?

Such kind of interview questions on c file handling programming is very important. We can open any file using fopen() function. The basic difference between any files for this function is just their extensions. For the text file, we write .txt, similarly for the batch file, we have to write the extension .bat. So, there is no big deal. But, in an interview, such questions on c programming always confuse you.

For example, see the following program.

int main()
{
   FILE *fp;
   fp = fopen("abc.bat","r"); //abc file is in current directory
}

Q. 17 Is it possible to open a folder in c programming?

Yes, it is possible. Opening a folder means opening a directory. So, for a writing program for this, we must include one header file and that is “dirent.h”.

Just like structure FILE, we will have to use a structure DIR and we will have to create directory pointer of this type DIR. In this header file, there is one structure is defined and that is dirent.

Structure definition is

 struct dirent
 {
    ino_t     d_ino;     //file serial number
    char      d_name[ ]; //name of entry
 };

Following are functions which are useful for us and they are

  1. DIR opendir(const char *); Opens a directory
  2. struct dirent readdir(DIR *); Read the directory
  3. int closedir(DIR *); Close the directory

For example, here is the C Program for opening a folder:

#include<stdio.h>
#include<dirent.h>
int main()
{
   struct dirent *de;
   DIR *dr=opendir("C:\\vijay\\website");
   while((de=readdir(dir))!=NULL)
   {
     printf("%s\n",de->d_name);
   }
   closedir(dr);  
   return 0;
}

In this way, we can open any directory using a c program.

Q. 17 Write a c program to open any website?

int main()
{
  char site[]="explorer https://";
  char url[25];
  printf("Enter website url\n");
  scanf("%s",url);
  strcat(site,url);
  system(site);
  return 0;
}

Q. 18 What is the size of void pointer?

The size of the void pointer depends on the system we are using. If the system is 32 bit, then the size of the void pointer is 4 bytes and for 64 bit, the size is 8 bytes.

Q. 19 What is the difference between structure and union in terms of storage?

In structure, every data member has its own memory. But, in case of union, all members share the same memory.

Q. 20 What is the difference between fgetc() and fputc() functions?

Both these functions are file input-output functions in c language. The fgetc() reads a single character from a file and fputc() writes a single character to the file.

Also Read: C Language Program to Count the Number of Lowercase Letters in a Text File

I hope guys, you have understood all these interview questions on c programming. There are thousands of interviews questions on c programming and it is not possible to include all those in one article. I have tried to include very basic and interesting interview questions on c programming.

So thank you very much for reading all these questions and best of luck for your interview.

Leave a Comment