C interview questions for experienced


C interview questions for experienced and explanations
1
Can you write a c program to display mouse?
Answer
Explanation:
Before write c code to display mouse pointer we must have basic knowledge of some important structure and union of c language:

The header file dos.h defines two important structures and one union. They are:

1. 
struct BYTEREGS {
unsigned char al, ah, bl, bh;
unsigned char cl, ch, dl, dh;
   };
2. struct WORDREGS {
unsigned int ax, bx, cx, dx;
unsigned int si, di, cflag, flags;
   };
3. union REGS {
struct WORDREGS x;
struct BYTEREGS h;
  };

Note: Try to remember above structures and union.

There is also one very important function int86 () which has been defined in dos.h header file. It is general 8086 software interrupt interface. It will better to understand it by an example:

C code to display mouse pointer:

#include <dos.h>
#include <stdio.h>
int main(){
union REGS i,o;
i.x.ax=1;
int86(0x33,&i,&o);
return 0;
}

Explanation: To write such program you must have one interrupt table. Following table is only small part of interrupt table:




This table consists for column. They are:
(1)Input
(2)Output
(3)Service number
(4)Purpose

Now look at the first row of interrupt table. To show the mouse pointer assign ax equal to 1 i.e. service number while ax is define in the WORDREGS

struct WORDREGS {
unsigned int ax, bx, cx, dx;
unsigned int si, di, cflag, flags;
};

And WORDRGS is define in the union REGS

union REGS {
struct WORDREGS x;
struct BYTEREGS h;
};

So to access the structure member ax first declare a variable of REGS i.e.

REGS i, o;

Note: We generally use i for input and o for output

To access the ax write i.x.ax (We are using structure variable i because ax is input (See in the interrupt table)

So to show mouse pointer assign the value of service number to it:

i.x.ax=1;

To provide this information to microprocessor
we use int86 function. It has three parameters

1. Interrupt number i.e. 0x33
2. union REGS *inputregiste i.e. &i
3. union REGS *outputregiste i.e. &o;

So write: int86 (0x33, &i, &o);
Hide
2
Can you write c program which shutdown the window operating system?
Answer
Explanation:
Step 1: Write the following program in TURBO C.

#include <dos.h>
int main (void){
    system("shutdown -s");
return 0;
}

Step 2: Save the above file. Let file name is close.c
Step 3: Only compile the above program.
Step 4: Now close the turbo c compiler and open that directory in window operating system where you have saved the close.c (default directory c:\tc\bin)
Step 5: Double click on its .exe file (close.exe)
After some time your window operating system will shutdown.
Hide
3
Can you explain #pragma warn directive in c by example?
Answer
Explanation:
In c there are many warning messages which can be on or off with help of #pragma warn.
Syntax:

#pragma warn +xxx
#pragma warn –xxx
#pragma warn .xxx

Where
+ means on
- means off
. means on/off (toggle)

xxx is indicate particular warning code in three alphabet. Example: rvl is warning code which means function should return a value.

#include<stdio.h>
#pragma warn –rvl
int main(){
    printf("It will not show any warning message");
    return 0;
}

Output: It will not show any warning message

When you will execute the above program then compiler will not show the warning message function should return a value because rvl warning is off.
List of warning code:

S.N.
Warning message
Code
ANSI Violations
1
Assigning 'type' to 'enumeration'
eas
2
Bit fields must be signed or unsigned int
bbf
3
Both return and return with a value used
ret
4
Declare type 'type' prior to use in prototype
dpu
5
Division by zero
zdi
6
Hexadecimal value contains more than 3 digits
big
7
Initializing 'enumeration' with 'type'
bei
8
'identifier' is declared as both external and static
ext
9
Ill-formed pragma
ill
10
Initialization is only partially bracketed
pin
11
Redefinition of 'macro' is not identical
dup
12
Suspicious pointer conversion
sus
13
Undefined structure 'structure'
stu
14
Void functions may not return a value
voi
Frequent Errors
1
Code has no effect
eff
2
Function should return a value
rvl
3
Parameter 'parameter' is never used
par
4
Possible use of 'identifier' before definition
def
5
Possibly incorrect assignment
pia
6
Unreachable code
rch
Less Frequent Errors
1
Ambiguous operators need parentheses
amb
2
Array variable 'identifier' is near
ias
3
Call to function with no prototype
pro
4
Call to function 'function' with no prototype
pro
5
Condition is always false
wccc
6
Condition is always true
wccc
7
'identifier' declared but never used
use
8
'identifier' is assigned a value that is never used
aus
9
No declaration for function 'function'
nod
10
Structure passed by value
stv
11
Superfluous & with function
amp
Portability Warnings
1
Constant is long
cln
2
Constant out of range in comparison
rng
3
Conversion may lose significant digits
sig
4
Non portable pointer comparison
cpt
5
Non portable pointer conversion
rpt
6
Mixing pointers to signed and unsigned char
ucp
Hide
4
What is pointer to array in c?
Answer
Explanation:
A pointer which holds base address of an array or address of any element of an array is known as pointer to array. For example:

(a)
#include<stdio.h>
int main(){
    int arr[5]={100,200,300};
    int *ptr1=arr;
    char *ptr2=(char *)arr;
    printf("%d   %d",*(ptr1+2),*(ptr2+4));
    return 0;       
}

Output: 300   44

(b)
#include<stdio.h>
int main(){
    static int a=11,b=22,c=33;
    int * arr[5]={&a,&b,&c};
    int const * const *ptr=&arr[1];
    --ptr;
    printf("%d ",**ptr);
    return 0;        
}

Output: 11
Hide
5
Can you declare such function which return type is pointer to a pointer?
Answer
Explanation:
#include<stdio.h>
int** ibm(){
    static int a[]={4,8,12,16};
    static int *pointer=a;
    static int **p=&pointer;
    return p;
}
int main(){
    int **ptr;
    ptr=ibm();
    printf("%d",++**ptr);
    return 0;        
}

Output: 5
Hide
6
Is it possible to rename any function in c?
Answer
Explanation:
Yes, we can rename any function using typedef keyword. It is useful when function declaration is too complex and we have to give any simple name or if we have to create more numbers of function of the same type.

typedef void govinda(int);
int main(){
    govinda one,two;
    one(1);
    two(2);
    return 0;        
}
void one(int x){
    printf("FROM ONE %d",x);
}
void two(int y){
    printf("\nFROM TWO %d",y);
}

Output:
FROM ONE 1
FROM TWO 2
Hide
7
Can you explain pointer to union in c by a good example?
Answer
Explanation:
A pointer which is pointing to a structure is known as pointer to structure. Examples of pointers to union:
What will be output if you will execute following code?

#include<stdio.h>
union address{
char *name;
char street[10];
int pin;
};
int main(){
union address emp,*p;
emp.name="ja\0pan";
p=&emp;
printf("%s %s",p->name,(*p).name);
return 0;
}
Output: ja ja
Explanation:
p is pointer to union address.-> and (*). Both are same thing. These operators are used to access data member of union by using union’s pointer. %s is used to print the string up to null character i.e. ‘\0’
Hide
8
Can you declare pointer to three dimensional arrays in c?
Answer
Explanation:
Examples of pointers to 3 dimensional array:

#include<stdio.h>
int main(){
const array[2][3][3]={0,1,2,3,4,5,6,7,8,9,10,11,12};
int const (*ptr)[2][3][3]=&array;
printf("%d ",*(*(*ptr)[1]+2));
return 0;
}
Output: 11
Explanation:
array [2][3][3]:It is three dimensional array and its content are constant integers.

ptr: It is pointer to such three dimensional array whose content are constant integer. 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) [1] +2)
=*(*(*&array) [1] +2)
=*(*array [1] +2)
=*(array [1] [0] +2)
=array [1] [0] [2] 
I.e. array element at the 1*(3*3) +0(3) + 2=11th position starting from zero which is 11.
Hide
9
Can you write a c program to draw a line using graphics video memory in c using far pointer?
Answer
Explanation:
Segment number 0XA is known as graphics video memory. This segment has 720 columns and 400 rows. Intersection of rows and columns is known as pixel. Size of each pixel is one byte which stores color information. Examples:

(q)What will be output of following c program?
#include <dos.h>
#include<stdio.h>
int main(){
int j;
union REGS i,o;
char far *ptr=(char *)0XA0000000;
i.h.ah=0;
i.h.al=0x13;
int86(0x10,&i,&o);
for(j=1;j<=100;j++){
*(ptr+j)=4;
}
return 0;
}
Output: One red color line in the graphics console as shown in the following figure:



In the above program what is following codes?
union REGS i,o;
i.h.ah=0;
i.h.al=0x13;
int86(0x10,&i,&o);
Answer: 
The task of this code is to switch the 256 bit color graphics console window from default console window of Turbo c++ IDE.
Copy the following code and execute the code in Turbo c compile and see what is displaying.
(1)What will be output of following c program?
#include <dos.h>
#include<stdio.h>
int main(){
int j,k;
union REGS i,o;
char far *ptr=(char *)0XA0000000;
i.h.ah=0;
i.h.al=0x13;
int86(0x10,&i,&o);
for(k=0;k<100;k++){
for(j=1;j<=319;j++){
*(ptr+j+k*320)=k;
}
}
return 0;
}
(2)What will be output of following c program?
#include <dos.h>
#include<stdio.h>
int main(){
int j,k;
union REGS i,o;
char far *ptr=(char *)0XA0000000;
i.h.ah=0;
i.h.al=0x13;
int86(0x10,&i,&o);
for(k=0;k<100;k++){
for(j=1;j<=319;j++){
*(ptr+j+k*320)=k;
}
}
return 0;
}
Hide
10
Can you give me any example of pointer to array of structure in c?
Answer
Explanation:
Pointer to array of structure: A pointer to an array which contents are pointer to structure is known pointer to array of structure.
What will be output if you will execute following code?

#include<stdio.h>
struct emp{
char *name;
int id;
};
int main(){
static struct emp e1={"A",1},e2={"B",2},e3={"C",3};
struct emp(*array[])={&e1,&e2,&e3};
struct emp(*(*ptr)[3])=&array;
printf("%s %d",(**(*ptr+1)).name,(*(*ptr+1))->id);
return 0;
}
Output: B 2
Explanation:
(**(*ptr+1)).name
=(**(*&array+1)).name //ptr=&array
=(**(array+1)).name //from rule *&p =p
=(*array[1]).name //from rule *(p+i)=p[i]
=(*&e2).name //array[1]=&e2
=e2.name=”B” //from rule *&p =p
(*(*ptr+1))->id
=(**(*ptr+1)).id //from rule -> = (*).
=e2.id=2
Hide
11
Do you know, what is the meaning and use of static keyword in c?
Answer
Explanation:
Keyword static is used for declaring static variables in c. This modifier is used with all data types like int, float, double, array, pointer, structure, function etc.

Important points about static keyword:

1. It is not default storage class of global variables. For example, analyze the following three programs and its output.

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

Output: 0

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

Output: 0

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

Output: Compilation error

At first glance if you will observe the output of above three codes you can say default storage class of global variable is static. But it is not true. Why? Read extern storage class.

2. Default initial value of static integral type variables are zero otherwise null. For example:

#include <stdio.h>
static char c;
static int i;
static float f;
static char *str;  
int main(){
    printf("%d %d %f %s",c,i,f,str);
    return 0;
}

Output: 0 0 0.000000 (null)

3. A same static variable can be declared many times but we can initialize at only one time. For example:

(a)
#include <stdio.h>
static int i;        //Declaring the variable i.
static int i=25;     //Initializing the variable.
static int i;        //Again declaring the variable i.
int main(){
    static int i;    //Again declaring the variable i.
    printf("%d",i);
    return 0;
}

Output: 25

(b)
#include <stdio.h>
static int i;        //Declaring the variable
static int i=25;     //Initializing the variable
int main(){
    printf("%d",i);
    return 0;
}
static int i=20;     //Again initializing the variable

Output: Compilation error: Multiple initialization variable i.

4. We cannot write any assignment statement globally. For example:

#include <stdio.h>
static int i=10;   //Initialization statement
i=25;              //Assignment statement
int main(){
    printf("%d",i);
    return 0;
}
            
Output: Compilation error

Note: Assigning any value to the variable at the time of declaration is known as initialization while assigning any value to variable not at the time of declaration is known assignment.

(b)
#include <stdio.h>
static int i=10;
int main(){
    i=25;       //Assignment statement
    printf("%d",i);
    return 0;
}

Output: 25

(5) A static variable initializes only one time in whole program. For example:

#include <stdio.h>
static int i=10;
int main(){
    i=5;
    for(i=0;i<5;i++){
         static int a=10; //This statement will execute
                          //only time.
         printf("%d",a++);//This statement will execute
                          //five times.
    }  
    return 0;
}

Output: 10 11 12 13 14

(6)If we declared static variable locally then its visibility will within a block where it has declared. For example:

#include<stdio.h>
int main(){
    {                       
         static int a=5;     
         printf("%d",a);
    }                       
    //printf("%d",a);   variable a is not visible here.
    return 0;   
}

Output: 5

7. If declared a static variable or function globally then its visibility will only the file in which it has declared not in the other files. For example:

(a)
#include<stdio.h>
static float a=144.0f; //global to all function
int main(){
    {                        
         printf("%d",a); //variable a is visible here.
       //printf("%d",b); variable b is not visible here.
    }                       
    printf("%d",a);   //variable a is visible here.
    //printf("%d",b);    variable b is not visible here.
    return 0;
}
static int b=5;    //Global to only calculation function
void calculation(){
    printf("%d",a);   //variable a is visible here.
    printf("%d",b);   //variable b is visible here.
}
   
(b) Consider a c program which has written in two files named as one.c and two.c:

//one.c
#include<conio.h>
static int i=25;
static int j=5; 

int main(){
    sum();
    return 0;
}

//two.c
#include<stdio.h>
extern int i; //Declaration of variable i.
extern int j; //Declaration of variable j.
/**
Above two lines will search the initialization statement of variable i and j either in two.c (if initialized variable is static or extern) or one.c (if initialized variable is extern) 
*/
extern void sum(){
    int s;
    s=i+j;
    printf("%d",s);
}

Compile and execute above two file one.c and two.c at the same time:

In Turbo c compiler

Step 1: Write above two codes in the file named as one.c and two.c (You can give any name as you like) and save it.

Step 2: In Turbo c++ IDE click on Project -> Open project menu as shown in following screen dump:



Step 3: After Clicking on open project you will get following screen:



In Open project File text field write any project name with .prj extension. In this example I am writing project name as CProject.PRJ. Now press OK button.

Step 4: After pressing OK button you will get following screen:


Now click on Project -> Add item menu.

Step 5: After clicking Add item you will get following screen:



In the name text field write down all c source code file one by one i.e. first write one.c and click on Add button. Then write two.c and click on Add button and so on


Step 6: At the end click on Done button. After clicking on done button you will get following screen:


At the lower part of window you can see project name, list of files you have added etc.

Step7: To compile the two files press Alt+F9 and to run the above program press Ctrl+F9
Note: To close the project click on Project -> Close project.

Output: Compilation error: Unknown symbol i and j.

Hence we can say variable i and j which has initialized into two.c is not visible in file one.c. This example proves visibility of globally declared static variable is file.

Note: In the above example function sum which was declared and defined in two.c has also storage class extern. So we can call from other file (one.c).If it will static then we cannot call function sum since static storage class is only visible to the file where it has declared.

(8)If we static variable has declared locally or globally its scope will always whole the program. For example:

(a) //locally declaration of static variable
#include<stdio.h>
void visit();
int main(){
    int i=0;
    {                    //Opening inner block
         static int a=5;  //locally declaration
         XYZ:;            //Label of goto statement
         printf("%d  ",a);
         a++;
         i++;
    }                     //closing inner block.   
    visit();
    /* printf("%d",a); Variable a is not visible here but
    it is alive. */
    if(i<5)
             goto XYZ;
    return 0;
}
void visit(){ 
}

Output: 5 6 7 8 9

Explanation: When program control will come out of inner block where variable a has declared then outside of inner block variable a is not visible but its scope is outside the program i.e. variable a hasn't dead .If with help of goto statement control again comes inside the inner block it prints previous incremented values which was not possible in case of auto or register variables.

(b)
//Locally declarations of variable   
There are two c source code files:
//one.c
#include<stdio.h>
#include<conio.h>
void main(){
    int i;
    for(i=0;i<3;i++){
         {
             static int a=5;
             printf("%d\n",a);
             a++;
         }
         visit();
    }
    getch();
}

//two.c
#include<stdio.h>
void visit(){
    printf("Don’t disturb, I am learning storage class");
    /* printf("%d",a); Variable a is not visible here but
    It is alive. */
}

Now compile and execute both files together:
Output:
5
disturb, I am learning storage class
6
disturb, I am learning storage class
7
disturb, I am learning storage class

Explanation: When control goes to another file and comes even that variable didn’t dead and it prints previous incremented value.

Note: In both examples if you will declare static variable globally you will get same output.

9. A static variables or functions have internal linkage. An internal linkage variables or functions are visible to the file where it has declared.  
Hide
12
How singed int is stored in the memory?
Answer
Explanation:
Total size of unsigned int: 16 bit
Those eight bits are use as:
Data bit: 15
Signe bit: 1

Note: In c negative number is stored in 2’s complement format. Example:

(1)Memory representation of:

signed int a=7;            (In Turbo c compiler)
signed short int a=7 (Both turbo c and Linux gcc compiler)
Binary equivalent of data 7 in 16 bit:  00000000 00000111
Data bit: 0000000 00000111 (Take first 15 bit form right side)
Sign bit: 0  (Take leftmost one bit)

First eight bit of data bit from right side i.e. 00000111 will store in the leftmost byte from right to left side and rest seven bit of data bit i.e. 0000000 will store in rightmost byte from right to left side as shown in the following figure:   



(2)Memory representation of:

signed int a=-7;  (In Turbo c compiler)
signed short int a=-7 (Both turbo c and Linux gcc compiler)
Binary equivalent of data 7 in 16 bit:  00000000 00000111
Binary equivalent of data -7 will be its 2’s complement:
1’s complements of 00000000 00000111 is 11111111 11111000
2’s complement will be:


Binary equivalent of data -7 in 16 bit: 11111111 11111001
Data bit: 1111111 11111001 (Take first 15 bit form right side)
Sign bit: 1    (Take leftmost one bit)
First eight bit of data bit from right side i.e. 00000111 will store in the leftmost byte from right to left side and rest seven bit of data bit i.e. 1111111 will store in rightmost byte from right to left side as shown in the following figure:   
Hide
13
What is offset address of pointers in c?
Answer
Explanation:
Each segment has divided into two parts.
1. Segment no (4 bit)
2. Offset address (16 bit)


So, in the other words we can say memory address of any variable in c has two parts segment number and offset address.

In turbo c 3.0 a particular segment number offset address varies from 0x0000 to 0xFFFF. Suppose physical address of any variable in c is 0x500F1. Then its segment number is 5 and offset address is 00F1.

Write a program to find the offset address of any variable?

#include<stdio.h>
int main(){
int x;
printf("%u ",&x); //To print offset address
printf("%p ",x); //To print segment address
printf("%p ",&x); //To print offset address
printf("%fp ",&x); //To print segment address : offset address
return 0;
}
Hide
14
What is difference between .com program and .exe program?
Answer
Explanation:
Both .com and .exe program are executable program but .com program execute faster than .exe program. All drivers are .com program. .com file has higher preference than .exe For example:

Open the command prompt in window OS. (Type CMD in Run)
In the command prompt type notepad and press enter key you will get the notepad. Since it executes notepad.exe

Repeat the same task but now first create any .com file in the same working directory. We can create it as open the notepad save it as notepad.com, set save as type is All files or we can create the .com file from command prompt.Then type notepad in command prompt and press the enter key you will get error message like:   
C:\notepad.com is not a valid Win32 application.


It proves that .com has higher precedence than .exe program. Com file is binary execute used in MS DOS. Com program keeps only code and data. It stores code and data in one segment. In Turbo C memory model should tiny to create .com program. We will discuss later how to create .com file in in turbo c.
Hide
15
Can you write any c program which print "Hello" ten times without using any conditional operators?
Answer
Explanation:
#include <stdio.h>
int main(int argc, char *argv[]){
    int i=0;
    while(10 - i++)
         printf("Hello");
    return 0;
}

Or
#include <stdio.h>
int main(int argc, char *argv[]){
    int i;
    for(i=0;10 - i;i++)
    printf("Hello");
    return 0;
}
Hide

8 comments:

Anonymous said...

good for practice,thanks for posting

Anonymous said...

its goog

Anonymous said...

Really good one

kamaljit singh said...

Good collection..

Saurabh said...

In question 11, example 3(a) ans should be 0.

sakshi Gupta said...

Good question thank you

ravinder nenavath said...

Very good.

Sivakumar M said...

very good describe about negative integers