Logo F2FInterview

C Advanced Interview Questions

Q   |   QA

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:

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={"S. Charvi","H-2",456003},*p=&cus;

int main(){
printf("%s %s",p->name,(*p).street);
return 0;
}

Output: S. Charvi 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.

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

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

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. 

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: