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.

The #pragma startup directive ensures that the specified function is executed before the main function, while #pragma exit ensures the function is executed after the main function. Function declarations must precede the startup and exit pragma directives, and the functions must not take any arguments, returning void. In the case of multiple #pragma startup directives, the execution order is determined by priority, where lower values take precedence.

startup:
For the #pragma startup directive, lower values indicate higher priority, meaning functions with lower priority values will execute first. In the case of multiple startup directives, the execution order is determined by priority, with the one having the lowest priority executing first.

exit:
For the #pragma exit directive, higher values represent higher priority. In other words, functions with higher priority values will execute first. If there are multiple exit directives, the execution order is determined by priority, with the one having the highest priority executing 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:
In the given program, there are two #pragma startup directives that will be executed before the main function. The order of execution between these directives is determined by their priority values: the one with the lower priority value will execute first.

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

In the given program, the function associated with the #pragma startup directive for the USA will execute before the one for India, as it has a lower priority value. Additionally, there are two #pragma exit directives, and these will execute after the main function. The order of execution among the exit directives will be determined by their priority values, with the one having the higher priority executing first.

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:
An ellipsis, represented by consecutive periods (...) with no white space, enables the creation of functions with a variable number of parameters in C. This feature allows the definition of functions that can accept a flexible number of arguments during each invocation. 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 serve a dual purpose in C programming. It can be used to denote both a variable number of parameters in a function declaration, allowing flexibility in the number of arguments, and as a variable (such as va_list) that stores a variable number of arguments within a function implementation. This versatile feature provides a mechanism for handling variable arguments in a concise and adaptable manner. 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;
}

In this context, the term "dynamic function" exemplifies a function designed to handle a variable number of arguments, while the pointer ptr serves as an illustration of a variable that stores a variable number of arguments.


The header file stdarg.h provides three important macros: va_arg, va_end, and va_start. These macros play a crucial role in handling variable numbers of parameters in a function.

  • 1. va_arg: This macro retrieves the next argument from the variable argument list.
  • 2. va_end: It performs cleanup tasks at the end of variable argument processing.
  • 3. va_start: This macro initializes the variable argument list for processing.
Collectively, these macros from the stdarg.h header—va_arg, va_end, and va_start—simplify the process of managing and retrieving additional parameters within functions designed to accommodate a variable number of arguments. 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 .

The term "lastfix" refers to the last fixed parameter utilized in a function that supports a variable number of parameters.

The term "type" is employed to determine the data type being utilized in a function that supports a variable number of parameters.


Certainly:

  • va_arg consistently returns the next parameter from right to left.
  • va_start establishes the pointer ap to the first parameter from the right side of a function with a variable number of parameters.
  • va_end aids in the normal return and cleanup process.

 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 functions with a variable number of arguments:

  1. 1 .The first parameter must be of any other data type when dealing with a variable number of arguments.

    Invalid function declaration: int (...);

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

  2. 2. It is not possible to pass any other data type after the variable number of arguments.

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

  3. 3. There should be no blank space between any two periods (dots) in the ellipsis.

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

  4. 4. In place of the ellipsis, data can be passed either of the same type or a 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 that points to an array where each element is a string is known as a pointer to an 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 a pointer to an array of strings, specifically of size 4.
array[4]: It is an array containing strings as its elements. 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 a stream of characters up to the null character (\0).

Hide
7
What is huge pointer in c?
Answer
Explanation:
The pointer that can point to or access the entire resident memory of RAM, encompassing all 16 segments, is known as a huge pointer.

The size of a huge pointer is 4 bytes or 32 bits.
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:
The Turbo C compiler, being based on the 8085 microprocessor, operates with a physical memory address represented in 20 bits. The process of converting a 4-byte or 32-bit huge address into a 20-bit actual physical address is referred to 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 by a 4-bit binary number.
When any relational operation is performed between two huge pointers, they are first normalized into actual physical addresses. 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 are aware, huge pointers compare their physical addresses.
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 to the same physical address, the if condition will evaluate to 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: 

In this scenario:

  • q is a far pointer.
  • p is a near pointer.
  • *p is a constant of double data type.

Avoid using huge pointers:

Incrementing a huge pointer will affect both the offset and segment addresses, unlike a far pointer which only increments the offset address. If you have limited knowledge about huge pointers and use them inadvertently, you may unintentionally access and modify critical areas like the Interrupt Vector Table (IVT), device driver memory, video memory, etc. This can pose a risk to the stability and integrity of your computer system. Exercise caution when working with such pointers to prevent unintended consequences.



Why there are three types of pointer in Turbo c compiler?
Answer:
Indeed, Turbo C compiler is rooted in the DOS operating system, which in turn is based on the 8085 microprocessor architecture. The 8085 microprocessor represents actual physical addresses in 20 bits. However, due to factors such as simplicity of calculations, ease of access to actual physical addresses, security considerations, etc., C introduces three types of pointers: near, far, and huge pointers. These pointers provide a balance between addressing the intricacies of memory and ensuring practicality and security in program development.

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

  • farray[][3]: Represents a two-dimensional array with float constants as its elements.

  • array[3]: Signifies a one-dimensional array containing addresses of other one-dimensional arrays, each holding float constants.

  • ptr: Acts as a pointer to a one-dimensional array, where each element is the address of another one-dimensional array containing float constants.

 Pictorial representation:
Note: In the figure above, the upper part of the box represents content, while the lower part signifies memory addresses. Please note that arbitrary addresses have been assumed for illustration purposes.
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 a value beyond the maximum limit of a data type is assigned, the compiler will assign +INF (positive infinity) if the number is positive and –INF (negative infinity) if the number is negative. Conversely, if a value less than the minimum limit of that data type is assigned, the compiler may 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:

Certainly:

We cannot use all modifiers with every data type in C. The following table outlines the permitted or meaningful modifiers that can be used with a specific data type.



Some important points regarding above table:

1. For arrays and pointers, the size and sign modifiers depend on the data type used in their declaration. Only those SIZE and SIGN modifiers that are valid for that specific data type are applicable. For example:

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

The provided declaration is invalid in C because the signed and short modifiers are not suitable for the float and double data types, respectively, as indicated in the table above. 

2.In the above table, the terms "permitted" and "meaningful modifier" are used because some modifiers may be permitted from a compilation point of view, meaning the compiler does not show any error. However, these modifiers may be meaningless in the context of C statements. For example:

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

3. Pascal and cdecl modifiers can be used with primitive data types, pointers, and arrays, but solely for naming conventions. 

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:

The RAM is divided into two parts:

  1. 1. Extended memory (unused)
  2. 2. Resident memory

In Turbo C 3.0 compiler, the size of resident memory is 1MB.


Resident Memory:

When a program is executed, it is stored in the resident memory. For Turbo C 3.0, it has 1MB of resident memory, meaning that when Turbo C 3.0 is opened, it occupies 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:
A near pointer specifically designates a data segment of 64KB or points to segment number 8.

A near pointer is limited to accessing data within a 64KB data segment, excluding areas such as graphics video memory and text video memory. The size of a near pointer is two bytes. By using the "near" keyword, any pointer can be designated as a 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: The size of any type of near pointer is consistently two bytes, as it can only accommodate a 16-bit offset address. The offset address ranges from 0000 to FFFF in hexadecimal notation. It's noteworthy that when printing the offset address in hexadecimal within a printf statement, the format specifier %p is utilized.

 Example:

#include<stdio.h>
int main(){
int i=10;
int *ptr=&i;
printf("%p",ptr);
return 0;
}
Output: Hexadecimal Representation of Offset Addresses.

%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: The hexadecimal representation of the decimal value 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: The cyclic nature of offset addresses becomes evident when incrementing or decrementing the offset address from its maximum and minimum values, respectively. This behavior results in the repetition of values in a cyclic order.
Cyclic property of offset address:
When you increment a near pointer variable, it corresponds to a clockwise movement, and conversely, decrementing the near pointer entails an anticlockwise 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

14 comments:

Unknown said...

very good information thank you

Anonymous said...

thanks alot,usefull info.

Unknown said...

Awesome Que and Mind Blowing Ans !!

Unknown said...

NO.1

Unknown said...

Thanks for this information its really useful.

Anonymous said...

Thanks for this information its really useful.

Unknown said...

good

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

Thank you..

illahi said...

very good questions

Unknown said...

Thanks boss for useful information...

Unknown said...

Thanks boss for useful information...

himanshu coder said...

1st problem is not executing. Why?
it only return i m in main , not India and Usa.

Anonymous said...

Hilarious..... But awesome....

Anonymous said...

It's a great possibility should you play on-line roulette at house but still need the social interaction of being in a land casino. The incontrovertible fact that|proven truth that} human dealers 1xbet are used means live vendor roulette is commonly solely out there to play with actual cash. Over the years, many individuals have tried to beat the casino, and turn roulette—a recreation designed to show a profit for the house—into one on which the player expects to win.