| by Dhananjay Yadav | 2 comments

C Programming Interview Questions and Answers

What is the purpose of printf() and scanf()?
printf () and scanf() are the two main functions in C Language that can be used to send output and receive input from the standard output and input respectively. They are defined in stdio.h

How to check equality between two variable in C language?
In C language the operator “==” is used to check equality of two values, while the symbol “=” is used for assignment. For example the expression: y==x will be evaluated to either 0 or 1 (depending on the actual value of x and y), while the expression y=x will assign y the value of x.

What is meant by increment operators in C?
In C language there is a short way of incrementing (++) or decrementing (–) a variable by 1. These operators are called increment operators. They can have the form of prefix (e.g., ++x) or postfix (e.g., x++).

1

What will be the value of the variable y in the following expression:
x=7;
y=5+x++;
The value of y will be 12, since the old value of x (here 7) will be added to 5 before x is incremented.

Write down the output of this C program:

#include 
int main(){
   int x, y;
   x=y=15;
   printf("x=%d,  y=%d\n", x,y);
  return 0;
}

The output will be:
x=15, y=15

What are the two operators that used in dynamic memory allocation in C?
malloc() and free(). The function malloc() can allocate a block of memory dynamically, while free can free this block.

How arrays are declared in C language?
Arrays in the C language are declared by giving the data type following by the variable name and finally the array size in two brackets. For example the expression:

int x[10];

declares an array called x of type integer with 10 elements.

Can two C structures added together using the plus operator?
no, in C language you cannot apply arithmetic operators directly on the structure. Instead, you will need to manipulate the structure element individually.

What is mean by function declaration and function definition in C language?
In C language , the function can be first declared to tell the compile about its signature. This will allows such functions to be called in program position before its actual definition. However, function definitions involve writing exactly what this function should do when it is executed.

How structures are declared and defined inside the C language?
Structures in C languages are usually declared by the word struct followed by the name of the structure. The elements of the structure are enclosed in curly brackets and the structure definition is terminated by a semicolon at the end. For example, the following lines are an example for declaring a structure called Point:

struct Point{
   int x;
   int y;
};

In C language, after a structure is declared it can be used after words by writing the word structure followed by the variable name, for example:

struct Point p1;

What is the purpose of C pre-processors in the C languages?
In C language the pre-processors directives are not part of the compiler, instead they instruct the compile to perform certain operations at compile time. For example, the include directive (#include) is used to ask the compile to include a header file to the currently compiled code.

How to declare an array of 100 integer values in C?
This can be declared as:

int a[100];

How to pass an array of type float to a Function?
Array can be passed to functions in many ways. For example they can be passed to functions as a pointer, for example:

void somefunc(int*a);

or as a sized array:

void somefunc(int a[10]);

It can also be passed as upsized array:

void somefunc(int a[]);

In this last approach, we will need to pass the array size as a seprate variable.

Declare a two dimensional array in C of size 10×50
It can be declared as follows:

int a[10][50];

The first parameter in brackets will represent the number of rows while the second parameter will represent the number of columns.

Describe the difference between for and while loops in C language.
In C language, as well as in many other computer languages, for loops can be used when the beginning and end of the loop is definitely known. In this case, it will be very convenient to use for loop. However, if we do not know exactly the start and end of the loop, we can use while loop instead.

What is meant by recursion in C language?
Recursion simply means a function that calls itself. In can view viewed as an alternative approach for using loops. All the calling parameter of the function is stored in the stack. The values are kept until the function is returned. A function that is used in a recursion should continue a trivial condition will stop the recursion from repeating forever. Otherwise and infinite loop could happen.

  1. Which of the following is not a logical operator in C language:
    !=
  2. ==,
  3. |= ,
  4. &&

The operator |= is not a logical operator. It is used as a combination of bitwise or and assignment operators.

What is the meaning of a pointer variables in C language?
In C language, a pointer variable is a variable that point to an address in memory. This is an efficient way in C language in order to access variable addresses from different locations in the program code.

Consider the following C program:

#include 
int main(){
   int x=7;
   int *px;
   px=&x;
  *px=18;
   printf("x=%d", x);
   return 0;
}

What will be the output of this program?
x=18

What is the purpose of the function sizeof() in C language?
The sizeof() in C language is used to calculate and return the size of variable and data types.

When one should use do while loop in C language?
Do while loop is used in C language to repeat the loop at least one time before the loop condition for termination. Therefore it is very helpful in certain scenarios where we need to execute the code at least one time before deciding to terminate or not.

What is the difference between const pointer and pointer variable in C language?
const pointers are defined in this way:

int *const ptr;

They can be used to refer to the address of other variables like this
ptr=&var
However, what we cannot do using const point is the increment operator. In other words, we cannot write something like this:
ptr++;
In contrast, pointer variables can be modified by either ways.

How to allocate dynamic two dimensional arrays in C language?
There are many different ways to dynamically allocate two dimensional arrays. May be the best way is to tread them as static arrays by allocating rows of pointers and each row is then assigned to a dynamic array representing the columns. So we can do it in these steps:

int** a;     
/*assume this array is of type int*/

a=(int**)malloc(sizeof(int*)*rows);    
/*rows represent the number of rows in the array*/
if(a==NULL) return;                            
/*make sure the memory is correctly allocated for rows*/
int i;
for(i=0;i< rows; i++)
  a[i]=(int)malloc(sizeof(int)*cols);    
/*allocate memory for each row*/

/*TODO: make sure memory is correctly allocated*/

What is the meaning of ternary operator and how can it be used in C language?
Ternary operator is a simple expression that can be used instead of if statement. It has the following syntax:
condition? value if true: value if value;
for example:

y=x>10? 2:1;

So y in this example with take either the value of 2 or 1 depending on whether x is greater than 10 or not.

How to open a file in C language?
In C language, we can use the open() function to open a file and get a point to it.

How to read/write data from/to a C file ?
There are many different ways to read/write data from and into a file. For example:
The functions: read(), write(), fscanf(), and fprintf() can be used to read/write data from/to the file.

What are the bitwise shift operators in C language?
Bitwise shift operators shift the bits of a variable. There are many types of them:
>> signed right shift operator.
>>> unsigned right shift operator.
<< left operators.
These operators can be used to manipulate variables by shifting the bit left or right.

How can you return a more than one variable in C language?
The standard way in C language is to return only one value from a function. But as a workaround we can user pointers and structures to return more than one value from the function.

Write a simple function in C language that takes two variables and then exchange their values ?

void exchange(int* var1, int* var2){
    int temp=*var1;
    *var1=*var2
    *var2=temp;
}

What is the purpose of the switch statement in C language?
Switch statement in C language are used to select one branch to execute in case of the existence of more than one alternatives.

When switch statement is preferred to be used over if-else statement?
If there are just a few branches then if-else statement can be used. Otherwise, switch can be much helpful.

What is the purpose of break and continue keywords?
break and continue are used in combination with loop statements (for, while, etc). The break statement will enforce the loop to exit from that point where break is called. But continue will ask the loop not to continue executing the remaining statements and jump back in a new iteration.

How can you specify command line arguments to your C program?
The main function can be supplied with two other variables: argc and argv and can be written in the form:
int main(int argc, char** argv){

}
The first variable will count the number of arguments, while the actual arguments will be supplied in argv.

What is the purpose of the default keyword in the C language?
The default keyword can be used in combination with switch statement to handle the default case that is not listed in any of the other cases.

How are nested loops suitable for accessing two dimensional arrays?
Two dimensional arrays consist of rows and columns. Each row can be viewed as another array with a dimension equal to the number of columns. So nested loops can look perfect for this scenario, when the outer loop will work on rows, while inner look handle columns.

How many dimensions we define for arrays in C language?
There is no limit for the number of dimensions that have be used for an array in C language. However, practically, we can use many dimension as much as the system resources permit.

What will be the output of the following C program?

#include 
int main(){
  int x=2, y=4;
 printf("%d", x==y? 5:7);
 return 0;
}

Output : 7

How the standard input and output files are represented in C language?
There are two keywords in the C language to represent standard input and output files: stdin and stdout. The former is used for standard input while the later can be used to refer to the standard output files.

Are arrays in C language passed by value or as a pointer?
When arrays are passed to functions, they are passed as pointers. One reason for this design is that arrays take much space and if they are passed by value, they will need to be copied in memory each time the function is called.

Does C language provide support for concurrency and multi-threading?
No, C does not support concurrency and multi threading. However, there are platform dependent libraries that can support that (e.g., pthreads in Linux and Linux like systems).

Dhananjay Yadav

Dhananjay Yadav

A Computer Science graduate, a blogger, a web developer worked on various web technologies.

Other Post by :