## INDEX

Advanced c interview questions with explanation
1
Do you memory representation of float data type in c?
Explanation:

Floating-point numbers are stored in exponential form, expressed as (Mantissa) * 10^(Exponent), where '' denotes multiplication and '^' represents power. In memory, only the Mantissa and Exponent are stored, excluding '', 10, and '^'.

For the float data type with a total size of 32 bits, the bits are allocated as follows:

• Exponent bits: 8
• Mantissa bits: 24

As the Mantissa is a signed number, the 24 bits are further divided into:

• Mantissa_sign bit: 1
• Mantissa_data bits: 23

For the mantissa alone:

The Mantissa_sign bit is set to zero if the number is positive and one if the number is negative.
Regarding the exponent, which is also a signed number:

Exponent_sign bit: 1
Exponent_data bits: 7

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

Certainly, you've summarized the rules effectively:

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

2. Rule 2: Add 127 to the exponent before storing it.

3. Rule 3: Store the exponent in memory's first byte, moving from the right to the left side.

4. Rule 4: If the exponent is a negative number, store it in 2’s complement form.

5. Rule 5: Store the mantissa in memory, starting from the second byte and moving from right to left.

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: The memory representation of floating-point numbers excludes the storage of the first digit (1), the decimal point symbol, the base of power (10), the power symbol (^), and the multiplication symbol (*).

Step 3: To find the exponent and mantissa along with the signed bit.

• Mantissa_data bit in binary: 0100100110011001101101 (considering only the first 23 bits from the left side).

Mantissa_sign bit: 1 (indicating a negative number)
Exponent in decimal: 3

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

Certainly, let's add 127 to the exponent and convert the result into binary:

Now, in binary form:

$13{0}_{10}=1000001{0}_{2}$

So, after adding 127 to the exponent, the binary representation is $1000001{0}_{2}$.

(The choice of 127 is related to the size of the exponent_data field, which is 7 bits. The maximum value that can be represented with 7 bits is 127 in decimal, or 1111111 in binary.

So, for an exponent of 3:

$\text{Exponent}=127+3=130$

In binary, 130 is represented as 10000010. The exponent_data bit is then derived by taking the first seven bits from the left side (1000001), and the exponent_sign bit is the rightmost bit (0).

Step 6: Storing the Mantissa_data bit, Mantissa_sign bit, Exponent_data bit, and Exponent_sign bit at their respective locations.

• Mantissa_data bit: $0100100110011001101101$ (only the first 23 bits from the left side)
• Mantissa_sign bit: 1 (indicating a negative number)
• Exponent_data bit: $1000001$ (first seven bits from the left side)
• Exponent_sign bit: 0 (rightmost bit)
as shown in the following figure.

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

How to check above memory representation is correct?

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 bits: 11001101
• Binary value of -52 in eight bits: 11001100
• Binary value of 36 in eight bits: 00100100
• Binary value of -63 in eight bits: 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?
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?
Explanation:

#pragma is an implementation-specific directive, meaning that each pragma directive has distinct rules and applications depending on the compiler being used. The types of pragma directives can vary from one compiler to another. If a compiler encounters a pragma directive it does not recognize, it typically ignores that specific pragma statement without issuing any error or warning messages. The compiler proceeds to execute the entire program, assuming that the 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:
As the #pragma world directive is unknown to Turbo C 3.0 compilers, they will simply ignore this directive without displaying any error or warning messages. The compiler proceeds to execute the entire program, treating the #pragma world statement as if it 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?
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?
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?
Explanation:

Segment number 0XB is identified as text video memory, which is partitioned into 25 rows and 80 columns, forming a total of 80 * 25 = 2000 cells. Each cell has a size of two bytes, with each byte further divided into two parts:

(a) Text byte: The first byte stores character information, representing characters using ASCII codes.

(b) Color byte: The second byte stores color information for the corresponding character.

In simpler terms, each even byte in a cell stores the character (ASCII code), and each odd byte stores the color information for that character. 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:

The color byte, with a size of 8 bits, stores color information in the following manner:

First four bits: Represent the color information of the character.

0000 0001: Blue color (1)
0000 0010: Green color (2)
0000 0100: Red color (4)
0000 1000: Intensity to increase the color (8)
This color byte structure allows for the representation of different colors and intensities for characters in text video memory.

The color byte, an 8-bit value, determines both the character and background colors as follows:

• Character Color Information (First Four Bits):

• 0000 0001: Blue color (1)
• 0000 0010: Green color (2)
• 0000 0100: Red color (4)
• 0000 1000: Intensity to increase the character color (8)

Note: Any other number will result in a mixture of the above four basic colors for the character.

• Background Color Information (Next Four Bits):

• 0001 0000: Blue color (16)
• 0010 0000: Green color (32)
• 0100 0000: Red color (64)
• 1000 0000: Intensity to increase the background color (128)

Note: Any other number will lead to a mixture of the above four basic colors for the background.

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?
Explanation:
A pointer that points to a structure is referred to as a pointer to a structure. Examples of pointers to structure:
What will be output if you will execute following code?

#include<stdio.h>
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:
The pointer $�$ is a pointer to the address of a structure. Both the -> and (*) operators serve the same purpose; they are used to access the data members of a structure through the structure's pointer.
Hide
8
What is pointer to array of character in c?
Explanation:
A pointer that points to an array whose elements are character constants is known as a pointer to an array of character constants.
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: The function display takes a pointer to an array of characters as a parameter and returns a data type of char.
**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?
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?
Explanation:
Each segment is designated for a specific purpose; for instance, segment number 15 is allocated for ROM, and segment number 14 is designated for BIOS.

We will delve into the details of accessing text video memory and graphics video memory in the upcoming chapters on pointers and unions in the context of 255-bit color graphics programming. It's noteworthy that segment number eight holds a special designation, known as the data segment. This segment is further divided into four parts, and understanding its structure is crucial for C programming.

### Stack Area

The stack area accommodates all automatic variables and constants. In C, this encompasses:

1. 1. Local variables with the default storage class.
2. 2. Variables declared with the storage class auto.
3. 3. Constants, including integer constants, character constants, string constants, float constants, etc., used in expressions.
4. 4. Function parameters and the return value of functions.

Variables stored in the stack area are automatically deallocated when program control exits their scope. Consequently, the stack area is often referred to as the 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: As variable 'a' is an automatic variable, it resides in the stack area. Its scope is confined to the for loop, resulting in the variable being deleted from the stack after each iteration and then re-initialized in the subsequent iteration. These concepts are specific to Turbo C 3.0. Furthermore, Turbo C 3.0 adheres to the Last In, First Out (LIFO) data structure, signifying that data in the stack area is stored in a last-in, first-out manner.
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:

The default storage class of variables 'a', 'b', 'c', and 'd' is auto. As automatic variables, they are stored in the stack area of memory. It will store in the stack as

The stack adheres to the Last In, First Out (LIFO) data structure. In the printf function, as the names of variables are not explicitly provided, the default output will display the contents of the stack in LIFO order, i.e., 8, 7, 6.

The memory segment has two parts: one for initialized variables and another for uninitialized variables. Initialized variables are located closer to each other than uninitialized variables, 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 variables 'a' and 'c' are initialized, while 'b' is not initialized. Initialized variables are stored closer to each other than uninitialized variables in the stack. Consequently, the first output will be 7, followed by 6 (as 'a' is closer than 'b' in relation to 'c'), and finally, any garbage value present in the stack will be output. It's essential to note that the default storage class for any local variable is auto.

2. Data area:
The data area encompasses all static and extern variables, constituting permanent memory space. Automatic variables, such as 'a' and 'c,' are initialized, while 'b' remains uninitialized. Initialized variables are positioned closer to each other than uninitialized variables within the stack. Consequently, the Last In, First Out (LIFO) principle dictates the order of output: 7 is followed by 6 (as 'a' is closer than 'b' relative to 'c'), and finally, any existing garbage value in the stack is output..

Note: The default storage class of any local variable is auto. This variable will persist in memory until the program concludes.

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

### Heap Area:

The heap area is utilized for dynamic memory allocation. In C, memory space can be dynamically allocated using functions like malloc and calloc, and this allocation always takes place in the heap area. The size of the heap is variable and contingent upon the available free space in memory.

Code Area:
The code area is exclusive to function pointers and can only be accessed by them. This area maintains a fixed size and is designated as read-only memory.

Hide
11
Can you write a c program to check a given number is odd or even without using any conditional operators?
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.
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?
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?
Explanation:
A pointer that points to another pointer, and can itself be a pointer to yet another, forming a chain of pointers, is referred to as a multilevel pointer. There can be multiple levels of pointers, allowing for a flexible and hierarchical structure.. 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.
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 the default pointer is near. However, the actual default pointer type depends on the memory model in use.

Hide
15
What is function recursion in c programming? Can you write any program using function?
Explanation:
Function Recursion:
Calling the same function from within its own function body is termed function recursion. It serves as an alternative to loops. Any C program that can be implemented using a loop can also be achieved through 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:
Understanding the execution and crafting a program using function recursion can be a formidable task.

Writing a function recursion program directly can be challenging. However, here's a helpful trick to make it easier: consider that any C program achievable with a loop is also possible using function recursion.

Steps for Writing a Function Recursion Program:

1. 1. Begin by writing the same program using a while loop and a function (excluding the main function).
2. 2. Make all local variables in that function static.
3. 3. Replace the while keyword with if.
4. 4. Substitute the increment or decrement operation of the variable used for condition checking with a function call, passing the parameter as the 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;
}

Note 1: In step 3, while calling the function, avoid passing the parameter using a unary operator, such as factorial(num--).

Note 2: Ensure that the function is written in a manner where the parameter of the calling function is utilized in a conditional statement. For example in the above program:

int factorial(int num)

Note: The function parameter 'num' is utilized in the conditional if statement.
Hide

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:
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?????