Difficult c interview questions


Difficult c interview questions and answers with explanation
1
Can you write two functions in which one executes before main function and other executes after the main function?
Answer
Explanation:
Syntax:

#pragma startup [priority]
#pragma exit [priority]

Where, priority is optional integer value.
For user priority varies from 64 to 255
For c libraries priority varies from 0 to 63
Default priority is 100.

pragma startup always execute the function before the main function pragma exit always execute the function after the main function. Function declaration of must be before startup and exit pragma directives and function must not take any argument and return void. If more than one startup directive then priority decides which one will execute first. 

startup:
Lower value: higher priority i.e. functions will execute first. If more than one exit directive then priority decides which one will execute first.

exit:
Higher value: higher priority i.e. functions will execute first. For example

#include<stdio.h>

void india();
void usa() ;
#pragma startup india 105
#pragma startup usa
#pragma exit usa
#pragma exit india 105

int main(){
printf("\nI am in main");
    return 0;
}

void india(){
printf("\nI am in india");
}

void usa(){
printf("\nI am in usa");
}

Output:
I am in usa
I am in India
I am in main
I am in India
I am in usa

Explanation:
Above program there are two startup directives which will execute before the main function.

Function name India has priority 105
Function name usa has priority 100 (default)

So usa function will execute first than India function and above program there are two exit directive which will execute after the main function.

Function name India has priority 105
Function name usa has priority 100 (default)

So india function will execute first than usa function.
Hide
2
Can you write a c program which display position of mouse pointer?
Answer
Explanation:
#include<dos.h>
#include<stdio.h>
int main(){
union REGS i,o;
int x,y,k;

//show mouse pointer
i.x.ax=1;
int86(0x33,&i,&o);

//its value will false when we hit key in the key board
while(!kbhit())  {
i.x.ax=3;    //get mouse position
x=o.x.cx;
y=o.x.dx;
printf("(%d , %d)",x,y);
delay(250);
int86(0x33,&i,&o);
}
return 0;
}
Hide
3
Write the c program to switch the 256 color graphics mode?
Answer
Explanation:
#include<dos.h>
int main(){
   int x,y,b;
   union REGS i,o;
   i.h.ah=0;
   i.h.al=0x13;
   int86(0x10,&i,&o);
   return 0;
}
Hide
4
Can you declare such function which return type is pointer to a structure?
Answer
Explanation:
#include <stdio.h>
typedef union novel{
    int count;
    double volume;
}*P,*Q,T;

P iim(){
    static T t={625};
    Q q=&t;
    return q;
}

int main(){
    T *r;
    r=iim();
    printf("%-d",r->count);
    return 0;       
}

Output: 625
Hide
5
What is Ellipsis or … in c?
Answer
Explanation:
Ellipsis is there consecutive period (.) with no white space. With the help of ellipsis we can write a function of variable number of parameter. Example:

#include<stdio.h>
#include<stdio.h>
int number(int a,...){
   return a;
}
int main() {
  int i;
  i=number(5,4,3,2,1);
  printf("%d",i);
  return 0;
}

Output: 5
Ellipsis can be used as a variable number of parameters or a variable which stores variable number of arguments. Example:

int dynamic(int,...);
void main(){
    int x,y;
    x=dynamic(2,4,6,8,10,12,14);
    y=dynamic(3,6,9,12);
    clrscr();
    printf("%d %d ",x,y);
    getch();
}
int dynamic(int s,...){
    void *ptr;
    ptr=...;
    (int *)ptr+=2;
    s=*(int *)ptr;
    return s;
}

Here dynamic function is example of variable number of arguments while pointer ptr is example of variable which is storing variable number of arguments.

In header file stdarg.h has three macro va_arg, va_end, va_start ,with help of this macro we can know other parameter of a function in case of  variable number of parameters. Syntax:

void va_start (va_list ap,lastfix)
type va_arg(va_list ap,type)
void va_end(va_list ap);

va_list is list of data .

lastfix is a last fixed parameter used in a function of variable number of parameters.

type is used to know which data type are using in function of variable number of parameters .

va_arg always returns next parameter from right to left.

va_start set the pointer ap to the first parameter from right side of a function of variable number of parameters.

va_end help in the normal return. Example:

#include<stdio.h>
#include <stdarg.h>
void sum(char *msg, ...){
int total = 0;
va_list p;
int arg;
va_start(p, msg);

while ((arg = va_arg(p,int)) != 0) {
total += arg;
}

printf(msg, total);
va_end(p);
}

int main() {
sum("The total sum is %d\n", 5,7,11,8);
return 0;
}

Output: 31

Properties of variable number of arguments:

1. First parameter must be any other data type in case of variable number of arguments.

Invalid decalation of function:
int (...);

Valid decalaration of function:
int (char c,...);
void(int,float,...);

2. We cannot pass any other data type after the variable number of arguments.

Invalid declaration of function:
int (int a,...,int b);

3. We cannot give any blank space between any two periods (dot) in the ellipsis.

Invalid declaration:
int (float b,. . .);

4. In place of ellipsis we can pass the data either of same type or different type.

int flue(char c,...);
int main(){
    int x,y;
    x=flue('A',1,2,3);
    y=flue('1',1.0,1,'1',1.0f,1l);
    printf("%d %d",x,y);
    return 0;
}
int flue(char c,...){
    return c;
}

Output: 65 49
Hide
6
What is Pointer to array of string in c?
Answer
Explanation:
A pointer which pointing to an array which content is string, is known as pointer to array of strings. For example:

#include<stdio.h>
int main(){
char *array[4]={"c","c++","java","sql"};
char *(*ptr)[4]=&array;
printf("%s ",++(*ptr)[2]);
return 0;
}
Output: ava
Explanation:
In this example
ptr: It is pointer to array of string of size 4.
array[4]: It is an array and its content are string. Pictorial representation:

Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.
++(*ptr)[2]
=++(*&array)[2] //ptr=&array
=++array[2]
=++”java”
=”ava” //Since ptr is character pointer so it
// will increment only one byte
Note: %s is used to print stream of characters up to null (\0) character.
Hide
7
What is huge pointer in c?
Answer
Explanation:
The pointer which can point or access whole the residence memory of RAM i.e. which can access all the 16 segments is known as huge pointer.

Size of huge pointer is 4 byte or 32 bit.
What will be output of following c program?

#include<stdio.h>
int main(){
char huge * far *p;
printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));


return 0;
}
Output: 4 4 1
Explanation: p is huge pointer, *p is far pointer and **p is char type data variable.
Normalization of huge pointer:
Turbo C compiler is based on 8085 microprocessor in which physical address of memory is represented in 20 bit. Conversion of 4 byte or 32 bit huge address into 20 bit actual physical address is known as normalization. Formula to calculate physical address:
Example:
What will be physical address of huge address 0X59994444?
Answer: 
Huge address: 0X59994444
Offset address: 0x4444
Segment address: 0x5999
Physical address= Segment address * 0X10 + Offset address
=0X5999 * 0X10 +0X4444
=0X59990 + 0X4444
=0X5DDD4
In binary: 0101 1101 1101 1101 0100
Note: Each hexadecimal digit is represented in 4 bit binary number.
When any relation operation is performed between two huge pointers first it normalizes in actual physical address. Example:
What will be output of following c program?

#include<stdio.h>
int main(){
   int huge*p=(int huge*)0XC0563331;
   int huge*q=(int huge*)0xC2551341;

   if(p==q)
     printf("Equql");
   else
     printf("Not equal");
     return 0;
}
Output: Equal
Explanation:
As we know huge pointers compare its physical address. 
Physical address of huge pointer p
Huge address: 0XC0563331
Offset address: 0x3331
Segment address: 0XC056
Physical address= Segment address * 0X10 + Offset address
=0XC056 * 0X10 +0X3331
=0XC0560 + 0X3331
=0XC3891
Physical address of huge pointer q
Huge address: 0XC2551341
Offset address: 0x1341
Segment address: 0XC255
Physical address= Segment address * 0X10 + Offset address
=0XC255 * 0X10 +0X1341
=0XC2550 + 0X1341
=0XC3891

Since both huge pointers p and q are pointing same physical address so if condition will true.
What will be output of following c program?

#include<stdio.h>
int main(){
double near *p,far *q;
printf("%d %d %d",sizeof(q),sizeof(p),sizeof(*p));
return 0;
}
Output: 4 2 8
Explanation: q is far pointer, p is near pointer, *p is double data type constant. 
Don’t use huge pointer:
If you will increment huge pointer it will increment both offset and segment address unlike to far pointer which only increments offset address. So if you have little knowledge about huge pointer and you are using huge pointer then you can easily access and modify the IVT, device driver memory, video memory etc. This might be dangerous for your computer. 
Why there are three types of pointer in Turbo c compiler?
Answer:
Turbo c compiler is based on Dos operating system which is based on 8085 microprocessors. In 8085 microprocessor actual physical address is represented in 20 bit. But there are not any pointers which can point 20 bit address. Considering simplicity of calculations, access to actual physical address, security etc. c has introduced three type of pointer i.e. near, far and huge pointer.
Hide
8
Can you explain pointer to array of array in c by an example?
Answer
Explanation:
Examples of pointer to array of array in c

#include<stdio.h>
int main(){
static float farray[][3]={0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f};
float (*array[3])[3]={&farray[0],&farray[1],&farray[2]};
float (*(*ptr)[])[3]=&array;
printf("%f ",2[(*(**ptr+1))]);
return 0;
}
Output: 5.000000
Explanation:
In this example:
farray [][3]: It is two dimension array and its content are float constants. 
array [3]:It is one dimension array and its content are address of such one dimension array which content are float constant.
ptr: It is pointer to one dimension array which content are address of such one dimension array which content are float constant. Pictorial representation:
Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.
2[(*(**ptr+1))]
= (*(**ptr+1)) [2]
= (*(**&array+1)) [2]
= (*(*array+1)) [2]
= (*(array [0] +1)) [2]
= (*(&farray [0] +1)) [2]
=&farray [0] [1] [2]
=*&farray [1] [2]
=farray [1] [2]
It is 1*(3) +2=5th element of farray starting from zero which is 5.0f
Hide
9
What will happen if we will go beyond the range of float, double and long double data type?
Answer
Explanation:
If we will assign a value which is beyond the maximum value of that data type compiler will assign +INF if number is positive and –INF if number is negative. If we will assign a value which is less than minimum value of that data type then complier will assign a garbage value or zero. 
Double data type overflow:
For example: What will be output of following c code?

#include<stdio.h>
int main(){
    float pmax= 3.5e38f;
    float nmin=-3.3e38f;
    float min= 1.0e-38f;
    printf("%f  %f  %f",pmax,nmin,min);
    return 0;
}
Hide
10
Can you tell me different types of modifies of a variable in c?
Answer
Explanation:
We cannot use all modifiers with each data types in c. Following table represents permitted or meaningful modifiers which can be used with a particular data type.


Some important points regarding above table:

1. In case of array and pointer, size and sign modifier depends upon which data type you are using in the declaration of array and pointer. Only those SIZE and SIGN modifier are valid which are also valid for that data type. For example:

signed float * p;
short double arr[5];

Above declaration is invalid for c because signed and short modifier is not suitable for float and double data type respectively as shown above table.    

2. In the above table we have used word permitted and meaningful modifier because some modifier has permitted from only compilation point of view i.e. but compiler is not showing any error but it is meaning less statement of c. For example:

short float f;
long float f;
short double d;
void const display();
int static far i;
char interrupt c;

3. We can use pascal and cdecl modifier with primitive data type, pointer and array only for naming convention.  

Question: Consider the following c program:

char c;
int display();
int main(){
    int i;
    return 0;
}

Write down all default modifier of variable c, i and function display, as declared in the above c code.

Answer:
All default modifier of variable: c



Equivalent declaration: signed static char c;
All default modifier of variable: i


Equivalent declaration: signed auto int i;
All default modifier of function: display



Equivalent declaration: extern void cdecl near display();
Hide
11
What is residence memory of RAM?
Answer
Explanation:
RAM has divided into two parts:
(1) Extended memory (useless)
(2) Residence memory 

In Turbo C 3.0 compiler size of residence memory is 1MB.

Residence memory:

When any program is executed it is stored in the residence memory. For turbo c 3.0, it has 1MB residence memory i.e. when we open turbo c 3.0 it stores 1MB in the RAM.
Hide
12
Can you write a c program to add two numbers using function recursion?
Answer
Explanation:
#include <stdio.h>
int main(int argc, char *argv[]){
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%d",sum(a,b));
    return 0;
}
int sum(int a,int b){
    static int s=0;
    if(b > 0){
         s++;
         sum(a,b-1);
    }
    else if(a>0){
         s++;
         sum(a-1,b);
    }
    return s;
}
Hide
13
Can you give any example of function overloading in c?
Answer
Explanation:
#include <stdio.h>
#include<stdarg.h>

int fun(int a, ...);
int main(int argc, char *argv[]){
   fun(1,10);
   fun(2,"cquestionbank");
   return 0;
}
int fun(int a, ...){
  va_list vl;
  va_start(vl,a);

  if(a==1)
      printf("%d",va_arg(vl,int));
   else
      printf("\n%s",va_arg(vl,char *));
}
Hide
14
Can you tell me about near pointer in C by examples?
Answer
Explanation:
In TURBO C there are three types of pointers. TURBO C works under DOS operating system which is based on 8085 microprocessor.
1. Near pointer
2. Far pointer
3. Huge pointer
Near pointer:
The pointer which can points only 64KB data segment or segment number 8 is known as near pointer.

That is near pointer cannot access beyond the data segment like graphics video memory, text video memory etc. Size of near pointer is two byte. With help keyword near, we can make any pointer as near pointer. Examples:
(1)
#include<stdio.h>
int main(){
int x=25;
int near* ptr;
ptr=&x;
printf(“%d”,sizeof ptr);
return 0;
}
Output: 2
(2)
#include<stdio.h>
int main(){
int near* near * ptr;
printf(“%d”,sizeof(ptr),sizeof(*ptr));
return 0;
}
Output: 2 2
Explanation: Size of any type of near pointer is two byte.

Near pointer only hold 16 bit offset address. Offset address varies from 0000 to FFFF (in hexadecimal).
Note: In printf statement to print the offset address in hexadecimal, %p is used. Example:
#include<stdio.h>
int main(){
int i=10;
int *ptr=&i;
printf("%p",ptr);
return 0;
}
Output: Offset address in hexadecimal number format.

%p is also used to print any number in hexadecimal number format. Example:
#include<stdio.h>
int main(){
int a=12;
printf("%p",a);
return 0;
}
Output: 000C
Explanation: Hexadecimal value of 12 is C.
Consider the following two c program and analyze its output:
(1)
#include<stdio.h>
int main(){
int near * ptr=( int *)0XFFFF;
ptr++;
ptr++;
printf(“%p”,ptr);
return 0;
}
Output: 0003
(2)
#include<stdio.h>
int main(){
int i;
char near *ptr=(char *)0xFFFA;
for(i=0;i<=10;i++){
printf("%p \n",ptr);
ptr++;
}
return 0;
}
Output:
FFFA
FFFB
FFFC
FFFD
FFFE
FFFF
0000
0001
0002
0003
0004
Explanation: When we increment or decrement the offset address from maximum and minimum value respectively then it repeats the same value in cyclic order. This property is known as cyclic nature of offset address.
Cyclic property of offset address:
If you increment the near pointer variable then move clockwise direction. If you decrement the near pointer then move anti clockwise direction.
Hide
15
Can you declare any variable of size one bit in c?
Answer
Explanation:
#include <stdio.h>

typedef struct{
  unsigned int mt:1;
}myType;

int main(int argc, char *argv[]){
    myType a;
    printf("%d",a.mt);
    return 0;
}
Hide

9 comments:

Kodamasimham Pridhvi said...

very good information thank you

Anonymous said...

thanks alot,usefull info.

Naresh Naidu said...

Awesome Que and Mind Blowing Ans !!

Jayesh Pore said...

NO.1

Jignesh Dangar said...

Thanks for this information its really useful.

Sandeep Saurabh said...

Thanks for this information its really useful.

Unknown said...

good

ø-Kเттμ♥-ø said...

Thank you..

Elahi said...

very good questions