Advanced c interview questions and answers



Advanced c interview questions with explanation
1
Do you memory representation of float data type in c?
Answer
Explanation:
Float numbers are stored in exponential form i.e.

(Mantissa)*10^ (Exponent)

Here * indicates multiplication and ^ indicates power.
In memory only Mantissa and Exponent is stored not *, 10 and ^.

Total size of float data type: 32 bit
Those bits are used in following manner:
Exponent bit: 8
Mantissa bit: 24
Mantissa is signed number, so 24 bit are used as:
Mantissa_sign bit: 1
Mantisaa_data bit: 23

For only mantissa:
Mantissa_sign bit will zero if number is positive and Mantissa_sign bit will one if number is negative.
Exponent is also signed number, So 8 bit are used as:
Exponent_sign bit: 1
Exponent_data bit: 7

Following figure illustrate how floating point number is stored in memory.


Five important rules:

Rule 1: To find the mantissa and exponent, we convert data into scientific form.

Rule 2: Before the storing of exponent, 127 is added to exponent.

Rule 3: Exponent is stored in memory in first byte from right to left side.

Rule 4: If exponent will negative number it will be stored in 2’s complement form.

Rule 5: Mantissa is stored in the memory in second byte onward from right to left side. Example:

Memory representation of:

         float a = -10.3f;

For this you have to follow following steps:

step1: convert the number (10.3) into binary form
Binary value of 10.3 is: 1010.0100110011001100110011001100110011…

step2: convert the above binary number in the scientific form. Scientific form of 1010.0100110011001100110011001100110011…=
1.01001001100110011001100 11001100110011…*10^3
Note: First digit i.e. 1, decimal point symbol, base of power i.e. 10, power symbol ^ and multiplication symbol * are not stored in the memory.

Step3: find exponent and mantissa and signed bit
Mantissa_data bit in binary = 0100100 11001100 11001101   (Only first 23 bit from left side) 

Mantissa_sign bit: 1 (Since it is a negative number)
Exponent in decimal: 3

Question:
Why we have taken right most bit of mantissa_data bit one instead of zero?

Step 5: Add 127 in the exponent and convert in the binary number form.

(Why 127? since size of exponent_data bit is 7 and maximum possible number in seven bit will 1111111 in binary or 127 in decimal)

Exponent= 127+3=130
Binary value of 130 in eight bit: 1000001 0
Exponent_data bit: 1000001 (Take first seven bit from left side)
Exponent_sign bit: 0 (Take rightmost bit)

Step 6: Now store the Mantissa_data bit, Mantissa_sign bit, Exponent_data bit and Exponent_sign bit at appropriate location as shown in the following figure. 



Note: Mantissa_data bits are stored from left to right while Exponent_data bits are stored from right to left.

How to check above memory representation is correct?

Answer:
We will take one char pointer and visit each byte of a float number and observe the output.

#include<stdio.h>
int main(){
    int i;
    float f=-10.3f;
    char *p=(char *)&f;
    for(i=0;i<4;i++)
         printf("%d   ",*p++);
    return 0;
}

Output: -51 -52 36 -63
Explanation:
Binary value of -51 in eight bit: 11001101
Binary value of -52 in eight bit: 11001100
Binary value of 36 in eight bit:  00100100
Binary value of -63 in eight bit: 11000001

This is exactly same as which we have represented in memory in the above figure.
Hide
2
Write a c program which changes the position of cursor?
Answer
Explanation:
#include<dos.h>
#include<stdio.h>
int main(){
union REGS i,o;
i.h.ah=2;   //positioning the cursor 
i.h.bh=0;
i.h.dh=30;   
i.h.dl=45;
int86(0x10,&i,&o);
printf("World");
return 0;
}
Hide
3
Do you know pragma directives in c?
Answer
Explanation:
Pragma is implementation specific directive i.e each pragma directive has different implementation rule and use. There are many type of pragma directive and varies from one compiler to another compiler .If compiler does not recognize particular pragma the it simply ignore that pragma statement without showing any error or warning message and execute the whole program assuming this pragma statement is not present.
For example suppose there is any pragma directive is #pragma world.

#include<stdio.h>
#pragma world
int main(){
    printf("C is powerful language ");
    return 0;
}

Output: C is powerful language
Explanation:
Since #pragma world is unknown for Turbo c 3.0 compilers so it will ignore this directive without showing any error or warning message and execute the whole program assuming #pragma world statement is not present.

List of pragma directives in turbo c 3.0:

1. #pragma startup
2. #pragma exit
3. #pragma warn
4. #pragma option
5. #pragma inline
6. #pragma argsused
7. #pragma hdrfile
8. #pragma hdrstop
9. #pragma saveregs
Hide
4
What is array of pointers in c?
Answer
Explanation:
Array whose content is address of another variable is known as array pointers.  For example:

#include<stdio.h>
int main(){
float a=0.0f,b=1.0f,c=2.0f;
    float * arr[]={&a,&b,&c};
    b=a+c;
    printf("%f",arr[1]);
    return 0;        
}
Hide
5
Can you declare such function which return type is pointer to an enum?
Answer
Explanation:
#include <stdio.h>
typedef enum color{a,b,c,d,e}co;
enum color eee(){
    static co x;
    x=b+c/2;
    return x;
}

int main(){
    int num;
    num=eee();
    printf("%#d",num);
    return 0;
}

Output: 2
Hide
6
Have you worked in text video memory in c?
Answer
Explanation:
Segment number 0XB is known as text video memory. This segment is divided into 25 rows and 80 columns which creates 80*25=2000 cells. Size of each cell is two byte. Each cell is divided into two parts each of size one byte.

(a) Text byte: First byte stores character information. It stores character as ASCII code.
(b) Color byte: Second byte stores color information of text byte character.

In other word we can say each even byte stores character and each odd byte stores color. Simple example:
#include<stdio.h>
int main(){
int i;
char far *ptr=(char *)0XB8000000;
*ptr='A';
*(ptr+1)=4;
return 0;
} 
Output: It will display character A in the red color as shown following screen dump:


Color scheme:

Color byte of size 8 bit stores the color information in the following manner. First four bits stores color information of character.

0000 0001: Blue color (1)
0000 0010: Green color (2)
0000 0100: Red color (4)
0000 1000: To increase the intensity of color. (8)
Note: Any other number will generate mixture of above four basic colors.
Next four bits stores color information of background of character.
0001 0000: Blue color (16)
0010 0000: Green color (32)
0100 0000: Red color (64)
1000 0000: To increase the intensity of color. (128)
Note: Any other number will generate after mixing of above four basic colors. Examples:
(1)What will be output of following c program?

#include<stdio.h>
int main(){
int i;
char far *ptr=(char *)0XB8000000;
*ptr='A';
*(ptr+1)=1;
*(ptr+2)='B';
*(ptr+3)=2;
*(ptr+4)='C';
*(ptr+5)=4;
return 0;
}
Output:



(2)What will be output of following c program?

#include<stdio.h>
int main(){
int i;
char far *ptr=(char *)0XB8000000;
*ptr='W';
*(ptr+1)=1;
*(ptr+2)='O';
*(ptr+3)=2;
*(ptr+4)='R';
*(ptr+5)=4;
*(ptr+6)='L';
*(ptr+7)=1;
*(ptr+8)='D';
*(ptr+9)=2;
return 0;
}
Output:

(3)What will be output of following c program?

#include<stdio.h>
//Mixture of basic color
int main(){
int i;
char far *ptr=(char *)0XB8000000;

*ptr='W';
*(ptr+1)=3;
*(ptr+2)='O';
*(ptr+3)=5;
*(ptr+4)='R';
*(ptr+5)=6;
*(ptr+6)='L';
*(ptr+7)=7;
*(ptr+8)='D';
*(ptr+9)=3;

return 0;
}
Output:



(4)What will be output of following c program?

#include<stdio.h>
//To increase the intensity of color.
int main(){
    int i;
char far *ptr=(char *)0XB8000000;
*ptr='P';
*(ptr+1)=1+8;
*(ptr+2)='O';
*(ptr+3)=2+8;
*(ptr+4)='I';
*(ptr+5)=3+8;
*(ptr+6)='N';
*(ptr+7)=4+8;
*(ptr+8)='T';
*(ptr+9)=5+8;
*(ptr+10)='E';
*(ptr+11)=6+8;
*(ptr+12)='R';
*(ptr+13)=7+8;

return 0;
}
Output:



(5)What will be output of following c program?

#include<stdio.h>
// for background color
int main(){
int i;
char far *ptr=(char *)0XB8000000;
*ptr='M';
*(ptr+1)=4+32;
*(ptr+2)='A';
*(ptr+3)=4+32;
*(ptr+4)='N';
*(ptr+5)=4+32;
*(ptr+6)='I';
*(ptr+7)=4+16;
*(ptr+8)='S';
*(ptr+9)=4+16;
*(ptr+10)='H';
*(ptr+11)=4+16;

return 0;
}
Output:
Hide
7
Can you declare pointer to structure in c?
Answer
Explanation:
A pointer which is pointing to a structure is known as pointer to structure. Examples of pointers to structure:
What will be output if you will execute following code?

#include<stdio.h>
struct address{
char *name;
char street[10];
int pin;
}cus={"A.Kumar","H-2",456003},*p=&cus;

int main(){
printf("%s %s",p->name,(*p).street);
return 0;
}
Output: A.Kumar H-2
Explanation:
p is pointer to structure address.-> and (*). Both are same thing. These operators are used to access data member of structure by using structure’s pointer.
Hide
8
What is pointer to array of character in c?
Answer
Explanation:
A pointer to such an array which contents is character constants is known as pointer to array of character constant.
What will be output if you will execute following code?

#include<stdio.h>
char display(char (*)[]);
int main(){
char c;
char character[]={65,66,67,68};
char (*ptr)[]=&character;
c=display(ptr);
printf("%c",c);

return 0;
}
char display(char (*s)[]){
**s+=2;
return **s;
}
Output: C
Explanation: Here function display is passing pointer to array of characters and returning char data type.
**s+=2
=>**s=**s+2
=>**ptr=**ptr+2 //s=ptr
=>**&character= **&character+2 //ptr=&character
=>*character=*character+2 //from rule *&p =p
=>character[0]=character[0]+2 //from rule *(p+i)=p[i]
=>character [0] =67
**s=character [0] =67
Note: ASCII value of ‘C’ is 67
Hide
9
Can you explain pointer to two dimensional arrays in c by an example?
Answer
Explanation:
Examples of pointers to 2 dimensional arrays:

#include<stdio.h>
void main(){
long array[][3]={7l,14l,21l,28l,35l,42l};
long int (*ptr)[2][3]=&array;
printf("%li ",-0[1[0[ptr]]]);
return 0;
}
Output: -28
Explanation:
-0[1[0[ptr]]]
=-1[0[ptr]][0] //From rule array[i]=i[array]
=-0[ptr][1][0]
=-ptr [0] [1] [0]
=-*ptr [0] [1] //From rule array[i]=*(array+i)
=-*(&array) [0] [1]
=-(&array) [0] [1][0]
=-(*&array)[1][0] //From rule *&p=p
=-array[1][0]
array[1][0] means 1*(3)+ 0 = 3rd element of array starting from zero i.e. 28
Hide
10
What is data segment in c?
Answer
Explanation:
All the segments are used for specific purpose. Like segment number 15 is used for ROM, segment number 14 is used for BIOS etc.  


We will discuss about how to access text video memory, graphics video memory in the pointer and union chapters of 255 bits color graphics programming. Segment number eight has special name which is known as data segment. This segment has been divided into four parts. This is very important for c programming


1. Stack area
All automatic variables and constants are stored into stack area. Automatic variables and constants in c:

1. All the local variables of default storage class.
2. Variables of storage calls auto.
3. Integer constants, character constants, string constants, float constants etc in any expression.
4. Function parameters and function return value.

Variables in the stack area are always deleted when program control reaches it out of scope. Due to this stack area is also called temporary memory area. For example:

What will be output of following c code?

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

Output: 5 5 5

Explanation: Since variable a is automatic variable, it will store in the stack area. Scope of variable a is within for loop. So after each iteration variable a will be deleted from stack and in each iteration variable a will initialize.  This two concepts are only for Turbo C 3.0 It follows LIFO data structure. That in the stack area of memory data is stored in last in first out. For example:

What will be output of flowing c code. (Turbo c 3.0)?

#include<stdio.h>
int main(){
    int a =5, b = 6, c = 7,d =8;
    printf("%d %d %d");
    return 0;
}

Output: 8 7 6
Explanation:

Default storage class of variables a, b, c and d is auto .Since it automatic variable it will be sorted in the stack area of memory. It will store in the stack as

Stack always follows LIFO data structure. In the printf function, name of variables is not written explicitly. So default output will content of stack which will be in the LIFO order i.e. 8 7 6.

It has two part one for initialize variable another for non-initialize variable. All initialize variables are more nearer than not initialized variable and vice versa. For example:

What will be output of following program (Turbo c 3.0)?

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

Output: 7 5 garbage value
Explanation:

Automatic variable a and c has initialized while b has not initialized. Initialize variables are more nearer than uninitialized variable .They will be stored in the stack. So due to LIFO first output will be 7 then 6 (since a is more nearer than b with respect to c) then any garbage value will be output which is present in the stack.
Note: Default storage class of any local variable is auto.


2. Data area:
All static and extern variable are stored in the data area. It is permanent memory spaAutomatic variable a and c has initialized while b has not initialized. Initialize variables are more nearer than uninitialized variable .They will be stored in the stack. So due to LIFO first output will be 7 then 6 (since a is more nearer than b with respect to c) then any garbage value will be output which is present in the stack.

Note: Default storage class of any local variable is auto.ce and variable will store in the memory unless and until program end. For example:

#include<stdio.h>
int main(){
    int i;
    for(i=0;i<3;i++){
         static int a=5;
         printf("%d",a);
    }
    return 0;
}

Output: 5 6 7

3. Heap area:
This memory area is use to allocate memory dynamically. In c we can allocate the memory space dynamically by using function malloc and calloc. It always allocates memory in the heap area. Its size is variable and depends upon free space in the memory.

4. Code area:
Function pointer can only access code area. Size of this area is always fixed and it is read only memory area.
Hide
11
Can you write a c program to check a given number is odd or even without using any conditional operators?
Answer
Explanation:
#include <stdio.h>
int fun();
int main(int argc, char *argv[]){         
    int num;
    scanf("%d",&num);
    if(num&1)
         printf("odd");
    else
         printf("even");
    return 0;
}
Hide
12
Write a c program to find out generic root of a number.
Answer
Explanation:
#include <stdio.h>
int fun();
int main(int argc, char *argv[]){        
    int num,x;
    scanf("%d",&num);
    printf("%d",(x=num%9)?x:9);
    return 0;
}
Hide
13
Can you write a function which executes after the main function in c?
Answer
Explanation:
#include <stdio.h>
#include<stdlib.h>

void fun();
int main(int argc, char *argv[]){
   atexit(fun);
   printf("cquestion");
   return 0;
}
void fun(){
  printf("bank");
}
Hide
14
What is a multilevel pointer in c?
Answer
Explanation:
A pointer is pointer to another pointer which can be pointer to others pointers and so on is known as multilevel pointers. We can have any level of pointers. Examples of multilevel pointers in c:
(1) What will be output if you will execute following code?

#include<stdio.h>
int main(){
int s=2,*r=&s,**q=&r,***p=&q;
printf("%d",p[0][0][0]);
return 0;
}
Output: 2
Explanation: 
As we know p[i] =*(p+i)
So,
P[0][0][0]=*(p[0][0]+0)=**p[0]=***p
Another rule is: *&i=i
So,
***p=*** (&q) =**q=** (&r) =*r=*(&s) =s=2
(2) What will be output if you will execute following code?

#include<stdio.h>
#define int int*
int main(){
    int *p,q;
p=(int *)5;
q=10;
printf("%d",q+p);
return 0;
}
Output: 25
Explanation: If you will see intermediate file you will find following code:

#include<stdio.h>
void main(){
int **p,q;
p=(int **)5;
q=10;
printf("%d",q+p);
return 0;
}

Explanations:
Here q pointer and p is a number.
In c, Address + number = Address
So, New address = old address + number * Size of data type to which pointer is pointing.
= 5 + 10 * sizeof (*int)
= 5+10*2 = 25.
Note: We are assuming default pointer is near. Actually it depends upon memory model.
Hide
15
What is function recursion in c programming? Can you write any program using function?
Answer
Explanation:
Calling of same function from its function body is known as function recursion. It is alternative of loop. Any c program which is possible using loop it must be possible using function recursion. Simple example:
Find the sum of all even numbers from 0 to 20 using function recursion. Program:

#include<stdio.h>
int main(){
int total;
total=sum(2);
printf("%d",total);
return 0;
}
int sum(int i){
static int even=0;
if(i<=20){
even=even+i;
sum(i+2); //calling same function
}
return even;
}

Output:
It is very difficult to understand the execution as well as to write the function recursion program.
How to write function recursion program in easier way:
I have already told it is very difficult to write function recursion program directly. If any person is writing such program directly he may be memorized that program. I am telling a very nice trick: how to write function recursion program in easier way?
As we know any c program which possible using loop it must be possible using function recursion.
Steps for how to write function recursion program:
Step1: First of all write same program using while loop and function. (Except main function)
Step 2: In that function make all local variable static.
Step 3: Replace while keyword by if.
Step 4: The increment or decrement of variable which is used for condition checking, replace with function call and pass the parameter of that incremented or decremented variable. Now understand by example:
Find the sum of all even numbers from 0 to 20 using function recursion.
Step 1: Write the same program using while loop and function. Here function is sum.

#include<stdio.h>
int main(){
int total;
total=sum(2);
printf("%d",total);
return 0;        
}
int sum(int i){
int even=0;
while(i<=20){
even=even+i;
i=i+2;
}
return even;
}

Step 2: Make local variable even as static variable

int sum(int i){
static int even=0;
while(i<=20){
even=even+i;
i=i+2;
}
return even;
}

Step 3: Replace while keyword by if keyword.

int sum(int i){
int even=0;
if(i<=20){
even=even+i;
i=i+2;
}
return even;
}

Step 4: Since here variable i has used in condition checking. So replace the statement i=i+2 by sum (i+2).

int sum(int i){
int even=0;
if(i<=20){
even=even+i;
sum(i+2);
}
return even;
}

Following only three simple change you can write any difficult function recursion program.

#include<stdio.h>
int main(){
int total;
total=sum(2);
printf("%d",total);
return 0;        
}
int sum(int i){
int even=0;
if(i<=20){
even=even+i;
sum(i+2);
}
return even;
}

One more example:
Write a program to find a factorial of given number using function recursion.
Step 1: write same program using while loop and function.

#include<stdio.h>
int main(){
int fact,num;
scanf("%d",&num);
fact=factorial(num);
printf("%d",fact);
return 0;        
}
int factorial(int num){
int fact=1; //make it static
while(num>0){ //replace while by if
fact=fact*num;
num--; // replace by function call as factorial(num-1);
}
return fact;
}

After following step1, step 2, step 3:

#include<stdio.h>
int main(){
int fact,num;
scanf("%d",&num);
fact=factorial(num);
printf("%d",fact);
return 0;        
}
int factorial(int num){
static int fact=1;
if(num>0){
fact=fact*num;
factorial (num-1);
}
return fact;
}

Note1: In step 3 while calling function don’t pass the parameter using unary operator like factorial (num--);
Note2: write the function in such a way parameter of calling function must be used in conditional statement. For example in the above program:

int factorial(int num)

num is function parameter and num is also used in the conditional if statement.
Hide

10 comments:

Anonymous said...

Question #4

I think it should be

printf("%f",*arr[1]);

Anonymous said...

1. Write a “C1GG filter” that reads a message entered by the user and translates
it into C1GG message:
Sample Run:
Enter message: Hey dude, C is rilly cool
C1GG message: H3Y DUD3, C 15 R1LLY C00L!!!!!
The program should convert the message to upper-case letters, substitute
digits for certain letters (A->4, B->8, I->1, O->0, S->5), and then append 5
exclamation marks.
2. Write a function that computes the value of the polynomial
5 4 3 2 3 2 5 7 6 x x x x x      . Write a program that asks the user to enter a
value for x , calls the function to compute the value of the polynomial, and
display the value returned by the function.
3. Write a program that reads a message and checks whether it is a palindrome
or not. Use array name as a pointer to keep track of positions in the array
(Ignore special characters).
Sample Run:
Enter a message: Madam, I am Adam.
The message entered is not a palindrome.
4. Write a program that prompts the user to enter two dates and then find which
date comes earlier on the calendar. Use structure to store date and function
to compare dates.
Sample Run:
Enter first date (mm/dd/yy) : 3/8/10
Enter second date (mm/dd/yy) : 5/17/11
3/8/10 is earlier than 5/17/11.
5. (C99) Write a program that converts a number in Cartesian coordinates to
polar form. The user will enter two values (real and imaginary parts of the
number). The program will display the values of r and  .

Anonymous said...

How can I get answers for those 5.plz anyone can help me.

Anonymous said...

#include
int factorial(int n)
{
int res=1;
int count;
for(count=1;count<=n;count++)
{
res=res*count;

}
printf("%d",res);
return res;
}

int main()
{
int f,res;
scanf("%d",&f);
res=factorial(f);
printf("%d",res);
}

Unknown said...

plz tell me how can i get these question answers pls tell me

Unknown said...

can anyone help me in this question plzz
The table below shows the normal boiling point of several substances.write a program that prompts the user for the observed boiling point of a substance in celsius and identifies the substance if the observed boiling point is within 5% of the expected boiling point.if the data input is more than 5% higher or lower than any of the boiling points in the table,the program should output the message substance unknown.
SUBSTANCE NORMAL BOILING POINT (c)
Water 100
Mercury 357
Copper 1187
Silver 2193
Gold 2660

Unknown said...

cn i get the answer for the above ques plz............its urgent

Unknown said...

Getting error for 6th question as"cannot cast from unsigned long to char"plz help

Anand Hosamani said...

Dude its very simple , please let me know if you still need its answer at anandhosamani@gmail.com

Unknown said...

write a c program to print number of two's in two table?????