Tough c interview questions


Tough c interview questions and answer with explanation
1
What is pascal and cdecl keyword in c language? Or
What are the parameter passing conventions in c?
Answer
Explanation:

C supports two parameter passing conventions:

  1. 1. Pascal: In this convention, the function name (though not mandatory) is often in uppercase. Parameters from the function call are passed to the corresponding parameters in the function definition, following the order.
  2. 2. Cdecl: This convention allows the function name to be either in uppercase or lowercase. However, the first parameter in the function call is passed to the last parameter in the function definition. Cdecl is the default parameter passing convention in C.
Examples: 
1. What will be output of following program?

#include<stdio.h>
int main(){
static int a=25;
void cdecl conv1() ;
void pascal conv2();
conv1(a);
conv2(a);
return 0;
}
void cdecl conv1(int a,int b){
printf("%d %d",a,b);
}
void pascal conv2(int a,int b){
printf("\n%d %d",a,b);
}

Output: 25 0
0 25

(2) what will be output of following program?

#include<stdio.h>
void cdecl fun1(int,int);
void pascal fun2(int,int);
int main(){
    int a=5,b=5;
    fun1(a,++a);
    fun2(b,++b);
    return 0;
}
void cdecl fun1(int p,int q){
    printf("cdecl:  %d %d \n",p,q);
}
void pascal fun2(int p,int q){
    printf("pascal: %d %d",p,q);
}
Output:
cdecl:  6 6
pascal: 5 6
(3) What will be output of following program?

#include<stdio.h>
void cdecl fun1(int,int);
void pascal fun2(int,int);
int main(){
    int a=5,b=5;
    fun1(a,++a);
    fun2(b,++b);
    return 0;
}
void cdecl fun1(int p,int q){
    printf("cdecl:  %d %d \n",p,q);
}
void pascal fun2(int p,int q){
    printf("pascal: %d %d",p,q);
}
Output:
cdecl:  6 6
pascal: 5 6
(4) What will be output of following program?

#include<stdio.h>
void convention(int,int,int);
int main(){
    int a=5;
    convention(a,++a,a++);
    return 0;
}
void  convention(int p,int q,int r){
    printf("%d %d %d",p,q,r);
}
Output: 7 7 5
(5) What will be output of following program?

#include<stdio.h>
void pascal convention(int,int,int);
int main(){
    int a=5;
    convention(a,++a,a++);
    return 0;
}
void pascal  convention(int p,int q,int r){
    printf("%d %d %d",p,q,r);
}
Output: 5 6 6
(6) What will be output of following program?

#include<stdio.h>
void pascal convention(int,int);
int main(){
    int a=1;
    convention(a,++a);
    return 0;
}
void pascal  convention(int a,int b){
    printf("%d %d",a,b);
}
Output: 1 2
(7) What will be output of following program?

#include<stdio.h>
void convention(int,int);
int main(){
    int a=1;
    convention(a,++a);
    return 0;
}
void convention(int a,int b){
    printf("%d %d",a,b);
}
Output: 2 2
Hide
2
Can you declare such function which return type is pointer to a structure?
Answer
Explanation:
#include <stdio.h>
typedef struct film{
    int size;
    int pixel;
    float price;
}xyz,pqr;

struct film *jadu(){
    static xyz one={231,12,900.0},*p=&one;
    return p;
}

int main(){
    pqr *ptr;
    ptr=jadu();
    printf("%d",ptr->pixel);
    return 0;
}

Output: 12
Hide
3
Can you write a c code to create dos command like dir?
Answer
Explanation:
Step 1: Write following code.

#include <stdio.h>
#include <dos.h>
int main(int count,char *argv[]){
struct find_t q ;
    int a;
    if(count==1)
         argv[1]="*.*";
         a = _dos_findfirst(argv[1],1,&q);
    if(a==0){
         while (!a){
             printf("  %s\n", q.name);
             a = _dos_findnext(&q);
         }
    }
    else{
        printf("File not found");
    }
return 0;   
}

Step 2: Save the as open.c (You can give any name)
Step 3: Compile and execute the file.
Step 4: Write click on My computer of Window XP operating system and select properties.
Step 5: Select Advanced -> Environment Variables
Step 6: You will find following window:

Click on new button (Button inside the red box)


Step 7: Write following:
Variable name: path
Variable value: c:\tc\bin\open.c  (the path where you have saved)

Step 8: Open command prompt and write list and press enter button.
Hide
4
Write a c program which restricts the movement of pointer?
Answer
Explanation:
//restrict the x and y coordinate
#include <dos.h>
#include <stdio.h>
int main(){

union REGS i,o;

//show mouse pointer
i.x.ax=1;
int86(0x33,&i,&o);

//x coordinate restriction
i.x.ax=7;
i.x.cx=20;
i.x.dx=300;
int86(0x33,&i,&o);

//y coordinate restriction
i.x.ax=8;
i.x.cx=50;
i.x.dx=250;
int86(0x33,&i,&o);
return 0;
}
Hide
5
What is use of #pragma inline directive in c language?
Answer
Explanation:

The #pragma inline directive informs the compiler that the program's source code incorporates inline assembly language code. In C, the assembly language program can be seamlessly integrated using the asm keyword.

Hide
6
How much are you comfortable with complex pointer declaration in c?
Answer
Explanation:
Rule 1: Establish the priority of pointer declarations by carefully considering both precedence and associativity, as outlined in the accompanying table.

In the context of expressions:

  • () operates as a bracket or function operator.
  • [] serves as an array subscription operator.
  • * functions as a pointer operator, distinct from its use as a multiplication operator.
  • The identifier, representing the name of the pointer variable, takes precedence with the highest priority.
  • Data types, including modifiers like signed int or long double, are not operators but are integral components of expressions.

For a clearer understanding, let's delve into examples:

(1) How to read following pointer?
char (* ptr)[3]
Answer:

Step 1: Both () and [] operators possess equal precedence, and their priority is determined by the rule of associativity. Since their associativity is left-to-right, the first priority is assigned to ().

Step 2: Within the brackets, * and ptr share equal precedence. According to the right-to-left rule of associativity, the first priority is assigned to ptr, and the second priority goes to *.
Step 3: Allocate the third priority to [].

Step 4: As data types have the lowest priority, designate the fourth priority to char.

Now read it following manner:
Ptr is a pointer to a one-dimensional array of size three, where each element in the array holds char-type data. 
(2) How to read following pointer?
float (* ptr)(int)
Answer:
Determine priorities based on precedence and associativity.




Now read it following manner:
ptr is a pointer to a function that takes an int type parameter and returns a float type data.
Rule 2: Independently assign priorities to each function parameter, and read and understand them separately. Illustrated through the following example.

(3) How to read following pointer?
void (*ptr)(int (*)[2],int (*) void))
Answer:
Assign priorities by adhering to the rules of precedence and associativity.


Now read it following manner:

ptr is a pointer to a function where the first parameter is a pointer to a one-dimensional array of size two, containing int type data. The second parameter is a pointer to another function, which takes no parameters (void) and returns an int data type, and the overall function returns void.
(4) How to read following pointer?
int ( * ( * ptr ) [ 5 ] ) ( )
Answer:
Assign priorities by following the rules of precedence and associativity.


Now read it following manner:
ptr is a pointer to an array of size five, where each element is a pointer to a function that takes no parameters (void) and returns an int type data.
(5) How to read following pointer?
double*(*(*ptr)(int))(double **,char c)
Answer:
Assign priorities by following the rules of precedence and associativity.



Now read it following manner:
ptr is a pointer to a function that takes an int type parameter and returns a pointer to a function. This inner function has two parameters: the first is a pointer to a pointer of double data type, and the second is a char type data. The return type of this inner function is a pointer to double data type.
(6) How to read following pointer?
unsigned **(*(*ptr)[8](char const *, ...)
Answer:
Assign the priority considering rule of precedence and associative.

Now read it following manner:
ptr is a pointer to an array of size eight, where each element is a pointer to a function. The function takes two parameters: the first is a pointer to a character constant, and the second is a variable number of arguments. The return type of this function is a pointer to a pointer of unsigned int data type.

Hide
7
What is the meaning of multilevel pointers in c?
Answer
Explanation:
A multilevel pointer in C refers to a situation where a pointer points to another pointer, and this chain of pointers can extend to multiple levels. This concept allows for a flexible and dynamic structure, enabling the creation of pointers pointing to other pointers and so forth, at various levels. The term "multilevel pointers" encompasses this hierarchy of pointer relationships, allowing for versatility in memory management and data structures.
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

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: The default assumption here is that the pointer is near, though it's important to recognize that this default choice can vary based on the memory model in use. Different memory models, such as near, far, or huge, impact how pointers operate in terms of memory addressing and access. It's crucial to consider the specific memory model in a given context to accurately interpret pointer behavior.

Hide
8
Are you familiar with memory model in turbo c?
Answer
Explanation:

C supports six memory models, each catering to different memory allocation strategies. In Turbo C++ IDE, you can view and modify these memory models through the following steps:

  1. Open Turbo C++ IDE.
  2. Navigate to the Options menu.
  3. Choose Compiler, then go to Code generation.

The available memory models are: (a) TINY (b) SMALL (c) MEDIUM (d) COMPACT (e) LARGE (f) HUGE

To change the memory model, follow these steps:

  1. Go to Options menu.
  2. Select Compiler.
  3. Navigate to Code generation.
  4. Choose the desired memory model.
  5. Click OK to apply the changes.
Properties of memory mode in C:
(1) The default type of pointer in C is determined by the memory model. Specifically, in C, a pointer to a function is denoted as "code."

Data: In C, a pointer that points to a variable is termed a "data" pointer.
Examples:
What will be output of following c program?

#include<stdio.h>
int main(){
int *ptr;
printf("%d",sizeof ptr);
return 0;
}
Output: The output depends on the memory model. Explanation: If the memory model is TINY, SMALL, or MEDIUM, the default pointer is near, resulting in an output of 2. Otherwise, the output will be 4.

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

#include<stdio.h>
int main(){
char (*fun)();
printf("%d",sizeof fun);
return 0;
}
Output: The output depends on the memory model. Explanation: The function fun is a pointer to a function. If the memory model is TINY, SMALL, or COMPACT, the default pointer is near, resulting in an output of 2. Otherwise, the output will be 4.
(3)What will be output of following c program?

#include<stdio.h>
int main(){
int near *p,*q;
printf("%d , %d",sizeof(p),sizeof(q));
return 0;
}
Output: 2 (Dependent on the memory model). Explanation: The pointer p is a near pointer, while the type of pointer q will depend on the default type of pointer determined by the memory model.
(4)What will be output of following c program?

#include<stdio.h>
int main(){
char huge **p;
printf("%d , %d",sizeof(p),sizeof(*p));
return 0;
}
Output: 4 (Dependent on the memory model). Explanation: The pointer p is a huge pointer, while the type of pointer *p will depend on the default type of pointer determined by the memory model.

(5)Write a c program to find the memory model of you computer?

#include<stdio.h>
int main(){
   #if defined __TINY__
   printf("Memory model is: TINY");
   #elif defined __SMALL__
   printf("Memory model is:SMALL ");
   #elif defined __MEDIUM__
   printf("Memory model is:MEDIUM ");
   #elif defined __COMPACT__
   printf("Memory model is:COMPACT ");
   #elif defined __LARGE__
   printf("Memory model is:LARGE ");
   #elif defined __HUGE__
   printf("Memory model is:HUGE ");
   #endif
   return 0;
}
Hide
9
What is far pointer in c?
Answer
Explanation:
A far pointer in C is a pointer that can access or point to any location within the entire addressable memory space of RAM, typically allowing access to all 16 segments. Far pointers are designed to handle larger memory models and are particularly useful in scenarios where the data or functions are spread across different segments.
The size of a far pointer in C is typically 4 bytes or 32 bits, although this can vary based on the specific system and compiler configurations.
(1) What will be output of following c program?

#include<stdio.h>
int main(){
int x=10;
int far *ptr;
ptr=&x;
printf("%d",sizeof ptr);
return 0;
}
Output: 4
(2)What will be output of following c program?

#include<stdio.h>
int main(){
int far *near*ptr;
printf("%d %d",sizeof(ptr) ,sizeof(*ptr));
return 0;
}
Output: 4 2
Explanation: ptr represents a far pointer, whereas *ptr denotes a near pointer.
(3)What will be output of following c program?

#include<stdio.h>
int main(){
int far *p,far *q;
printf("%d %d",sizeof(p) ,sizeof(q));
return 0;
}
Output: 4 4
The first 16 bits of a far pointer typically store the segment number, while the next 16 bits store the offset address. This structure allows far pointers to effectively reference memory locations beyond the 64KB limit imposed by near pointers, making them suitable for larger memory models.
What is segment number and offset address?

#include<stdio.h>
int main(){
int x=100;
int far *ptr;
ptr=&x;
printf("%Fp",ptr);
return 0;
}
Output: 8FD8:FFF4

In the given context, the hexadecimal numbers 8FD8 represent the segment address, and FFF4 represents the offset address.

Note: The %Fp format specifier is utilized in the printf function to print the offset and segment addresses of a pointer in hexadecimal number format.

Additionally, the header file dos.h provides three macro functions for handling far pointers:

  1. 1.FP_OFF(): Retrieves the offset address from a far address.
  2. 2. FP_SEG(): Retrieves the segment address from a far address.
  3. 3. MK_FP(): Creates a far address from segment and offset addresses.
Examples:
(1)What will be output of following c program?
#include <dos.h>
#include<stdio.h>
int main(){
int i=25;
int far*ptr=&i;
printf("%X %X",FP_SEG(ptr),FP_OFF(ptr));
return 0;
}
Output: Any segment and offset address in hexadecimal number format respectively.
(2)What will be output of following c program?
#include <dos.h>
#include<stdio.h>
int main(){
int i=25;
int far*ptr=&i;
unsigned int s,o;
s=FP_SEG(ptr);
o=FP_OFF(ptr);
printf("%Fp",MK_FP(s,o));
return 0;
}
Output: 8FD9:FFF4 (Assume)
Note: The offset address, segment address, and far address of any far pointer are determined by the operating system and cannot be predicted or guessed. The values are assigned and managed by the operating system based on its memory management scheme.
Limitation of far pointer:

Indeed, altering the segment address of a given far address through arithmetic operations is not possible. Unlike near pointers, where simple arithmetic operations can directly manipulate addresses within a single segment, far pointers are constrained by their segmented structure.

Attempting to increment a far address beyond the maximum value of its offset address will not result in a direct jump to another segment. Instead, the offset address will wrap around in a cyclic manner, repeating within the same segment. Far pointers necessitate a more intricate approach when working across segment boundaries, often involving manipulation through specific macros or functions provided by the operating system. Example:

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

#include<stdio.h>
int main(){
int i;
char far *ptr=(char *)0xB800FFFA;
for(i=0;i<=10;i++){
printf("%Fp \n",ptr);
ptr++;
}
return 0;
}
Output:
B800:FFFA
B800:FFFB
B800:FFFC
B800:FFFD
B800:FFFE
B800:FFFF
B800:0000
B800:0001
B800:0002
B800:0003
B800:0004
The cyclic behavior of a far pointer within the same segment, where the offset address wraps around upon reaching its maximum value, is referred to as the "cyclic nature" of far pointers. This characteristic is a result of the segmented memory model used by far pointers in C, and it highlights the limitations and unique behavior associated with addressing within a specific segment.

Important points about far pointer:
1. Far pointers in C facilitate comparisons using relational operators that take into account both the offset address and segment address. Examples:

#include<stdio.h>
int main(){
int far *p=(int *)0X70230000;
int far *q=(int *)0XB0210000;
if(p==q)
printf("Both pointers are equal");
else
printf("Both pointers are not equal");
    return 0;
}

Output: The two pointers are not equal.
(2)What will be output of following c program?

#include<stdio.h>
int main(){
int far *p=(int *)0X70230000;
int far *q=(int *)0XB0210000;
int near *x,near*y;
x=(int near *)p;
y=(int near *)q;
if(x==y)
printf("Both pointer are equal");
else
printf("Both pointer are not equal");
    
    return 0;
}

Output: The two pointers are equal.
2. Far pointers do not undergo normalization.

Hide

10
Can you explain pointer to array of union in c?
Answer
Explanation:
A pointer to an array whose elements are pointers to unions is referred to as a pointer to an array of unions.
What will be output if you will execute following code?
union emp{
char *name;
int id;
};
int main(){
static union emp e1={"A"},e2={"B"},e3={"C"};
union emp(*array[])={&e1,&e2,&e3};
union emp(*(*ptr)[3])=&array;
printf("%s ",(*(*ptr+2))->name);
return 0;
}
Output: C
Explanation:
In this example:
e1, e2, e3: They are variables of union emp.
array[]: This represents a one-dimensional array of size three, where each element contains the address of a union named emp.

ptr: This is a pointer to an array of unions.
(*(*ptr+2))->name
=(*(*&array+2))->name //ptr=&array
=(*(array+2))->name //from rule *&p=p
=array[2]->name //from rule *(p+i)=p[i]
=(&e3)->name //array[2]=&e3
=*(&e3).name //from rule ->= (*).
=e3.name //from rule *&p=p
=”C”
Hide
11
Can you declare pointer to array of pointer to string in c?
Answer
Explanation:
A pointer to an array of pointers to strings is a pointer that points to an array, where each element is a pointer to a string. Example of Pointer to array of pointer to string:

#include<stdio.h>
int main(){
static char *s[3]={"math","phy","che"};
typedef char *( *ppp)[3];
static ppp p1=&s,p2=&s,p3=&s;
char * (*(*array[3]))[3]={&p1,&p2,&p3};
char * (*(*(*ptr)[3]))[3]=&array;
p2+=1;
p3+=2;
printf("%s",(***ptr[0])[2]);
return 0;
}
Output: che
Explanation: 
ptr: is pointer to array of pointer to string.
P1, p2, p3: are pointers to array of string.
array[3]: is array which contain pointer to array of string.
Pictorial representation:
Note: In the above figure, the upper part of the box represents content, and the lower part represents memory addresses. Arbitrary addresses have been assumed for illustration purposes.
As we know p[i]=*(p+i)
(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]
=(***(&array))[2] //ptr=&array
=(**array)[2] //From rule *&p=p
=(**(&p1))[2] //array=&p1
=(*p1)[2]
=(*&s)[2] //p1=&s
=s[2]=”che”
Hide
12
What is extern keyword in c?
Answer
Explanation:

The extern keyword in C is utilized for declaring external variables. This modifier is applicable to various data types, including int, float, double, arrays, pointers, structures, functions, and others. It signifies that the variable is declared but not defined in the current scope and will be provided by another source during linking.


Important points about the extern keyword:

  1. 1. It serves as the default storage class for all global variables and functions.
 For example, Analyze following two c code and its output:

(a)
#include <stdio.h>
int i;    //By default it is extern variable
int main(){
    printf("%d",i);
    return 0;
}

Output: 0

(b)
#include <stdio.h>
extern int i;    //extern variable
int main(){
    printf("%d",i);
    return 0;
}


Output: Compilation error - Undefined symbol 'i'.

Question: In both programs, the variable i is declared as an extern variable. However, why is the output different? Please refer to the second and third points for clarification.

(c)
#include <stdio.h>
void sum(int,int//By default it is extern.
int main(){
    int a=5,b=10;
    sum(a,b);
    return 0;
}
void sum(int a,int b){
    printf("%d”",a+b);
}

Output: 15

2When we use the extern modifier with any variables, it serves as a declaration only, indicating that the variable is defined elsewhere. In the second case, the compiler is showing an error for an unknown symbol i because the memory is not allocated for this extern variable in the current scope. To define a variable, i.e., to allocate memory for extern variables, it is necessary to initialize them.
In essence, using extern informs the compiler that the variable is declared elsewhere, and the actual memory allocation and definition will be provided in another part of the program or in a different file during linking. For example:

#include <stdio.h>
extern int i=10;    //extern variable
int main(){
    printf("%d",i);
    return 0;
}

Output: 10

  1. 3. If you do not use the extern keyword with global variables, the compiler will automatically initialize the extern variable with its default value.

  2. 4. The default initial value of an extern integral type variable is zero, or null for other types.

 For example:

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

Output: 0 0 0.000000 (null)

5. We cannot initialize extern variable locally i.e. within any block either at the time of declaration or separately. We can only initialize extern variable globally. For example:

(a)
#include <stdio.h>
int main(){
extern int i=10; //Try to initialize extern variable
                 //locally.
    printf("%d",i);
    return 0;
}
Output: Compilation error: Cannot initialize extern variable.

(b)
#include <stdio.h>
int main(){
    extern int i; //Declaration of extern variable i.
    int i=10;     //Try to locally initialization of
                  //extern variable i.
    printf("%d",i);
    return 0;
}
Output: Compilation error - Multiple declarations of variable 'i'.

6. If a variable is declared as an extern variable, the compiler searches for its initialization. If it is initialized, which can be either extern or static, it is acceptable. Otherwise, the compiler will generate an error. For example:

(a)
#include <stdio.h>
int main(){
    extern int i; //It will search the initialization of
                  //variable i.
    printf("%d",i);
    return 0;
}
int i=20;    //Initialization of variable i.

Output: 20

(b)
#include <stdio.h>
int main(){
extern int i; //It will search the any initialized
              //variable i which may be static or 
              //extern.
printf("%d",i);
    return 0;
}
extern int i=20; //Initialization of extern variable i.

Output: 20

(c)
#include <stdio.h>
int main(){
extern int i; //It will search the any initialized
              //variable i which may be static or 
              //extern.
    printf("%d",i);
    return 0;
}
static int i=20; //Initialization of static variable i.

Output: 20

(d)
#include <stdio.h>
int main(){
    extern int i;   //variable i has declared but not
                    //initialized
    printf("%d",i);
    return 0;
}

Output: Compilation error- Unknown symbol i.

7. A particular extern variable can be declared multiple times, but it can only be initialized once. For example:

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

Output: 25

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

Output: Compilation error - Multiple initialization of variable 'i'..

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

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

Output: Compilation error

Assigning a value to a variable at the time of declaration is known as initialization, while assigning a value to a variable not at the time of declaration is known as assignment.

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

Output: 25

9. If a variable or function is declared as extern globally, its visibility extends throughout the entire program, whether it consists of a single file or multiple files. For example consider a c program which has written in two files named as one.c and two.c:

(a)
//one.c
#include<conio.h>
int i=25; //By default extern variable
int j=5;  //By default extern variable
/**
Above two lines is initialization of variable i and j.
*/
void main(){
    clrscr();
    sum();
    getch();
}

//two.c
#include<stdio.h>
extern int i; //Declaration of variable i.
extern int j; //Declaration of variable j.
/**
The two lines above will search for the initialization statements of variables i and j. If the initialized variable is declared as static or extern, the search will extend to "two.c"; if it's declared as extern, the search will extend to "one.c". 
*/
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: Create two files named one.c and two.c (or any preferred names) and write the respective codes in each file. Save the files.

Step 2: 
  1. Open Turbo C++ IDE.
  2. Click on the "Project" menu.
  3. Select "Open Project" from the dropdown menu.

This will allow you to open the project where you have saved your one.c and two.c files.



Step 3: Upon selecting "Open Project," you will be presented with the following screen.

In the "Open Project" dialog, enter any project name with a .prj extension in the "File" text field. For example, I am using the project name "CProject.PRJ." After entering the name, click the OK button.


Step 4: Upon selecting "Open Project," you will be presented with the following screen.

Now, navigate to the "Project" menu and select "Add item."

Step 5: After clicking "Add item," you will see the following screen.


In the "Name" text field, enter each C source code file name one by one. For example, start by entering "one.c," then click the "Add" button. Continue this process for each file, such as "two.c," clicking "Add" after each entry. Repeat until you have added all the relevant source code files.




Step 6: Finally, click on the "Done" button. After clicking "Done," you will see the following screen.

At the lower part of the window, you will find information about the project, including its name and the list of files you have added.

Step 7: To compile the two files, press Alt+F9, and to run the program, press Ctrl+F9.

Note: To close the project click on Project -> Close project.

Output: 30

Hence, we can affirm that variables i and j, which have been initialized in two.c, are also visible in the file one.c. This example demonstrates the visibility of globally declared extern variables across the entire program.


Note: In the above example, the function sum, which was declared and defined in two.c, also has the storage class extern. Therefore, we can call this function from another file (one.c). If it were declared as static, we wouldn't be able to call the sum function because the static storage class restricts visibility to the file in which it is declared.

10. Extern variables or functions have external linkage, making them visible to all files. 
Hide
13
Why we use volatile variables in c?
Answer
Explanation:
All variables in C are, by default, not volatile. By using the volatile modifier, which is a keyword in the C language, you can designate any variable as a volatile variable.

Properties of volatile variables:

1. A volatile variable can be modified by background routines of the preprocessor. These routines may include interrupt signals by microprocessors, threads, real-time clocks, etc.

2. In simple terms, a value in a volatile variable, stored in memory, can be altered by any external sources.

3. Whenever the compiler encounters any reference to a volatile variable, it always loads the value of the variable from memory. This ensures that if any external source has modified the value in memory, the compiler will obtain its updated value.

4. The working principle of volatile variables is opposite to that of register variables in C. As a result, volatile variables take more execution time than non-volatile variables.



A volatile variable is declared with help of keyword volatile:

int volatile i;

A non-volatile variable is declared without using keyword volatile:

int i;

Question: What is meaning of following declaration in c?

const volatile float f;
register volatile char c;
Hide
14
What is the segmentation in operating system?
Answer
Explanation:
The RAM's resident memory of size 1MB is divided into 16 equal parts, each referred to as a segment. Each segment has a size of 64 KB.

16 * 64 KB = 1 MB

This division process is commonly referred to as segmentation.

Note: In Turbo C 3.0, the physical addresses of variables are stored in 20 bits. However, there are no pointers of size 20 bits available. As a result, pointers cannot access the entire resident memory address. To address this limitation, C language introduces three types of pointers:

1. Near pointer
2. Far pointer
3. Huge pointer


Hide
15
Can you write a function which returns an integer without using return keyword in turbo c?
Answer
Explanation:
#include <stdio.h>
int fun();
int main(int argc, char *argv[]){
    printf("%d",fun());
    return 0;
}
int fun(){
    _AX = 5;
}
Hide

4 comments:

AnotherBlogger said...

Hi,

Please elaborate the answer of the question 7.

Regards,
Anand

A to Z tricks said...

Very Good solving for Interviews

Unknown said...

Plaese give correct explanation of question 15 plzzz

Unknown said...

Please Correct 6th example of Question 6

given :
unsigned **(*(*ptr)[8](char const *, ...)

correct :
unsigned **( *(*ptr)[8] ) (char const *, ...)
')' is missing after [8]