0% found this document useful (0 votes)
51 views29 pages

C Questions and Answers

Uploaded by

Punit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views29 pages

C Questions and Answers

Uploaded by

Punit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Tricky c questions and answers

Tricky c programs question for interview and answers with explanation. These questions are
for experienced persons.

C advanced interview questions and answers

(1) What will be output if you will compile and execute the following c code?

struct marks{
int p:3;
int c:3;
int m:2;
};
void main(){
struct marks s={2,-6,5};
printf("%d %d %d",s.p,s.c,s.m);
}

(a) 2 -6 5
(b) 2 -6 1
(c) 2 2 1
(d) Compiler error
(e) None of these

Answer: (c)
Explanation:
Binary value of 2: 00000010 (Select three two bit)
Binary value of 6: 00000110
Binary value of -6: 11111001+1=11111010
(Select last three bit)
Binary value of 5: 00000101 (Select last two bit)

Complete memory representation:

(2) What will be output if you will compile and execute the following c code?

void main(){
int huge*p=(int huge*)0XC0563331;
int huge*q=(int huge*)0xC2551341;
*p=200;
printf("%d",*q);
}

(a)0
(b)Garbage value
(c)null
(d) 200
(e)Compiler error

Answer: (d)
Explanation:
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 same physical address so content of q will also same as
content of q.

(3) Write c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)?

Answer:
#include”dos.h”
#include”stdio.h”
void main()
{
union REGS i,o;
int x,y,k;
//show mouse pointer
i.x.ax=1;
int86(0x33,&i,&o);
while(!kbhit()) //its value will false when we hit key in the key board
{
i.x.ax=3; //get mouse position
x=o.x.cx;
y=o.x.dx;
clrscr();
printf("(%d , %d)",x,y);
delay(250);
int86(0x33,&i,&o);
}
getch();
}

(4) Write a c program to create dos command: dir.

Answer:

Step 1: Write following code.

#include “stdio.h”
#include “dos.h”
void 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");
}
}

Step 2: Save the as list.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\list.c (Path where you have saved)
Step 8: Open command prompt and write list and press enter.
Command line argument tutorial.

(6) What will be output if you will compile and execute the following c code?

void main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
}

(a) Equal
(b) Greater than
(c) Less than
(d) Compiler error
(e) None of above

Answer: (d)
Explanation:
static variables are load time entity while auto variables are run time entity. We can not initialize any load
time variable by the run time variable.
In this example i is run time variable while x is load time variable.
Properties of static variables.
Properties of auto variables.

(7) What will be output if you will compile and execute the following c code?

void main(){
int i;
float a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=3;i++)
printf("%d ",*ptr++);
}

(a)0 0 0 0
(b)Garbage Garbage Garbage Garbage
(c)102 56 -80 32
(d)102 102 -90 64
(e)Compiler error

Answer: (d)
Explanation:
In c float data type is four byte data type while char pointer ptr can point one byte of memory at a time.
Memory representation of float a=5.2

ptr pointer will point first fourth byte then third byte then second byte then first byte.
Content of fourth byte:
Binary value=01100110
Decimal value= 64+32+4+2=102
Content of third byte:
Binary value=01100110
Decimal value=64+32+4+2=102
Content of second byte:
Binary value=10100110
Decimal value=-128+32+4+2=-90
Content of first byte:
Binary value=01000000
Decimal value=64
Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit.

(8) What will be output if you will compile and execute the following c code?

void main(){
int i;
double a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=7;i++)
printf("%d ",*ptr++);
}
(a) -51 -52 -52 -52 -52 -52 20 64
(b) 51 52 52 52 52 52 20 64
(c) Eight garbage values.
(d) Compiler error
(e) None of these

Answer: (a)
Explanation:
In c double data type is eight byte data type while char pointer ptr can point one byte of memory at a
time.
Memory representation of double a=5.2

ptr pointer will point first eighth byte then seventh byte then sixth byte then fifth byte then fourth byte
then third byte then second byte then first byte as shown in above figure.
Content of eighth byte:
Binary value=11001101
Decimal value= -128+64+8+4+1=-51
Content of seventh byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of sixth byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of fifth byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of fourth byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of third byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of second byte:
Binary value=000010100
Decimal value=16+4=20
Content of first byte:
Binary value=01000000
Decimal value=64
Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit.

(9) What will be output if you will compile and execute the following c code?

void main(){
printf("%s","c" "question" "bank");
}

(a) c question bank


(b) c
(c) bank
(d) cquestionbank
(e) Compiler error

Answer: (d)
Explanation:
In c string constant “xy” is same as “x” “y”
String tutorial.

(10) What will be output if you will compile and execute the following c code?

void main(){
printf("%s",__DATE__);
}

(a) Current system date


(b) Current system date with time
(c) null
(d) Compiler error
(e) None of these

Answer: (a)
Explanation:
__DATE__ is global identifier which returns current system date.

(11) What will be output if you will compile and execute the following c code?

void main(){
char *str="c-pointer";
printf("%*.*s",10,7,str);
}

(a) c-pointer
(b) c-pointer
(c) c-point
(d) cpointer null null
(e) c-point

Answer: (e)
Explanation:
Meaning of %*.*s in the printf function:
First * indicates the width i.e. how many spaces will take to print the string and second * indicates how
many characters will print of any string.
Following figure illustrates output of above code:

Properties of printf function.

(12) What will be output if you will compile and execute the following c code?

void start();
void end();
#pragma startup start
#pragma exit end
int static i;
void main(){
printf("\nmain function: %d",++i);
}
void start(){
clrscr();
printf("\nstart function: %d",++i);
}
void end(){
printf("\nend function: %d",++i);
getch();
}

(a)
main function: 2
start function: 1
end function:3
(b)
start function: 1
main function: 2
end function:3
(c)
main function: 2
end function:3
start function: 1
(d) Compiler error
(e) None of these

Answer: (b)
Explanation:
Every c program start with main function and terminate with null statement. But #pragma startup can call
function just before main function and #pragma exit
What is pragma directive?
Preprocessor tutorial.

(13) What will be output if you will compile and execute the following c code?
void main(){
int a=-12;
a=a>>3;
printf("%d",a);
}

(a) -4
(b) -3
(c) -2
(d) -96
(e) Compiler error

Answer :( c)
Explanation:
Binary value of 12 is: 00000000 00001100
Binary value of -12 wills 2’s complement of 12 i.e.

So binary value of -12 is: 11111111 11110100

Right shifting rule:


Rule 1: If number is positive the fill vacant spaces in the left side by 0.
Rule 2: If number is negative the fill vacant spaces in the left side by 1.
In this case number is negative. So right shift all the binary digits by three space and fill vacant space by
1 as shown following figure:

Since it is negative number so output will also a negative number but its 2’s complement.
Hence final out put will be:

And its decimal value is: 2


Hence output will be:-2

(14) What will be output if you will compile and execute the following c code?

#include "string.h"
void main(){
clrscr();
printf("%d%d",sizeof("string"),strlen("string"));
getch();
}
(a) 6 6
(b) 7 7
(c) 6 7
(d) 7 6
(e) None of these

Answer: (d)
Explanation:
Sizeof operator returns the size of string including null character while strlen function returns length of a
string excluding null character.

(15) What will be output if you will compile and execute the following c code?

void main(){
static main;
int x;
x=call(main);
clrscr();
printf("%d ",x);
getch();
}
int call(int address){
address++;
return address;
}
(a) 0
(b) 1
(c) Garbage value
(d) Compiler error
(e) None of these
Answer: (b)
Explanation:
As we know main is not keyword of c but is special type of function. Word main can be name variable in
the main and other functions.
What is main function in c?

(16) What will be output if you will compile and execute the following c code?

void main(){
int a,b;
a=1,3,15;
b=(2,4,6);
clrscr();
printf("%d ",a+b);
getch();
}
(a) 3
(b) 21
(c) 17
(d) 7
(e) Compiler error

Answer: (d)
Explanation:
In c comma behaves as separator as well as operator.
a=1, 3, 15;
b= (2, 4, 6);
In the above two statements comma is working as operator. Comma enjoys least precedence and
associative is left to right.
Assigning the priority of each operator in the first statement:

Hence 1 will assign to a.


Assigning the priority of each operator in the second statement:

(17) What will be output if you will compile and execute the following c code?

int extern x;
void main()
printf("%d",x);
x=2;
getch();
}
int x=23;

(a) 0
(b) 2
(c) 23
(d) Compiler error
(e) None of these

Answer: (c)
Explanation:
extern variables can search the declaration of variable any where in the program.

(18) What will be output if you will compile and execute the following c code?

void main(){
int i=0;
if(i==0){
i=((5,(i=3)),i=1);
printf("%d",i);
}
else
printf("equal");
}

(a) 5
(b) 3
(c) 1
(d) equal
(e) None of above

Answer: (c)
Explanation:

(19) What will be output if you will compile and execute the following c code?

void main(){
int a=25;
clrscr();
printf("%o %x",a,a);
getch();
}

(a) 25 25
(b) 025 0x25
(c) 12 42
(d) 31 19
(e) None of these

Answer: (d)
Explanation:
%o is used to print the number in octal number format.
%x is used to print the number in hexadecimal number format.
Note: In c octal number starts with 0 and hexadecimal number starts with 0x.

(20) What will be output if you will compile and execute the following c code?

#define message "union is\


power of c"
void main(){
clrscr();
printf("%s",message);
getch();
}

(a) union is power of c


(b) union ispower of c
(c) union is
Power of c
(d) Compiler error
(e) None of these

Answer: (b)
Explanation:
If you want to write macro constant in new line the end with the character \.

(21) What will be output if you will compile and execute the following c code?

#define call(x) #x
void main(){
printf("%s",call(c/c++));
}

(a)c
(b)c++
(c)#c/c++
(d)c/c++
(e)Compiler error

Answer: (d)
Explanation:
# is string operator. It converts the macro function call argument in the string. First see the intermediate
file:
test.c 1:
test.c 2: void main(){
test.c 3: printf("%s","c/c++");
test.c 4: }
test.c 5:
It is clear macro call is replaced by its argument in the string format.

(22) What will be output if you will compile and execute the following c code?

void main(){
if(printf("cquestionbank"))
printf("I know c");
else
printf("I know c++");
}

(a) I know c
(b) I know c++
(c) cquestionbankI know c
(d) cquestionbankI know c++
(e) Compiler error

Answer: (c)
Explanation:
Return type of printf function is integer which returns number of character it prints including blank spaces.
So printf function inside if condition will return 13. In if condition any non- zero number means true so
else part will not execute.

C pointers interview questions and answers

Frequently asked technical objective types multiple choice pointer questions with explanation
of placement in c programming language
Note: Linux GCC compilers and Visual C++ compiler doesn't support far and huge pointers.

1.
What will be output of following program?

#include<stdio.h>
int main(){
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
return 0;
}
(A) 2

(B) 320

(C) 64

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: 64

Turbo C ++4.5: 64

Linux GCC: 64

Visual C++: 64

As we know int is two byte data byte while char is one byte data byte. char pointer can keep the address
one byte at time.

Binary value of 320 is 00000001 01000000 (In 16 bit)

Memory representation of int a = 320 is:


So ptr is pointing only first 8 bit which color is green and Decimal value is 64.

2.
What will be output of following program?

#include<stdio.h>
#include<conio.h>
int main(){
void (*p)();
int (*q)();
int (*r)();
p = clrscr;
q = getch;
r = puts;
(*p)();
(*r)("cquestionbank.blogspot.com");
(*q)();
return 0;
}

(A) NULL

(B) cquestionbank.blogspot.com

(C) c

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: cquestionbank.blogspot.com

Turbo C ++4.5: cquestionbank.blogspot.com

Linux GCC: Compilation error

Visual C++: Compilation error

p is pointer to function whose parameter is void and return type is also void. r and q is pointer to function
whose parameter is void and return type is int . So they can hold the address of such function.

3.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j=&i;
k=&j;
printf("%u %u %d ",k,*k,**k);
return 0;
}
(A) Address, Address, 3

(B) Address, 3, 3

(C) 3, 3, 3

(D) Compilation error

(E) None of above

Output

Explanation:

Turbo C++ 3.0: Address, Address, 3

Turbo C ++4.5: Address, Address, Address

Linux GCC: Address, Address, 3

Visual C++: Address, Address, 3

Memory representation

Here 6024, 8085, 9091 is any arbitrary address, it may be different.

Value of k is content of k in memory which is 8085

Value of *k means content of memory location which address k keeps.

k keeps address 8085 .

Content of at memory location 8085 is 6024

In the same way **k will equal to 3.

Short cut way to calculate:


Rule: * and & always cancel to each other

i.e. *&a = a

So *k = *(&j) since k = &j

*&j = j = 6024

And
**k = **(&j) = *(*&j) = *j = *(&i) = *&i = i = 3

4.
What will be output of following program?

#include<stdio.h>
int main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 80;
(*p)++;
printf("%d",*q);
return 0;
}
(A) 80

(B) 81

(C) 82

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: 81

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error

Far address of p and q are representing same physical address.

Physical address of 0x55550005 = (0x5555) * (0x10) + (0x0005) = 0x55555

Physical address of 0x53332225 = (0x5333 * 0x10) + (0x2225) = 0x55555

*p = 80, means content at memory location 0x55555 is assigning value 25

(*p)++ means increase the content by one at memory location 0x5555 so now content at memory
location 0x55555 is 81

*q also means content at memory location 0x55555 which is 26


5.
What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
return 0;
}
(A) c questions

(B) c (null)

(C) (null) (null)

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: (null) (null)

Turbo C ++4.5: Run time error

Linux GCC: Run time error

Visual C++: Run time error

We cannot assign any string constant in null pointer by strcpy function.

6.
What will be output of following program?

#include<stdio.h>
int main(){
int huge *a =(int huge *)0x59990005;
int huge *b =(int huge *)0x59980015;
if(a == b)
printf("power of pointer");
else
printf("power of c");
return 0;
}
(A) power of pointer

(B) power of c

(C) power of cpower of c


(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: power of pointer

Turbo C ++4.5: power of c

Linux GCC: Compilation error

Visual C++: Compilation error

Here we are performing relational operation between two huge addresses. So first of all both a and b will
normalize as:

a= (0x5999)* (0x10) + (0x0005) =0x9990+0x0005=0x9995

b= (0x5998)* (0x10) + (0x0015) =0x9980+0x0015=0x9995

Here both huge addresses are representing same physical address. So a==b is true.

7.
What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
register a = 25;
int far *p;
p=&a;
printf("%d ",*p);
return 0;
}
(A) 25

(B) 4

(C) Address

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: Compilation error

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error


Visual C++: Compilation error

Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

8.
What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
char far *p,*q;
printf("%d %d",sizeof(p),sizeof(q));
return 0;
}
(A) 2 2

(B) 4 4

(C) 4 2

(D) 2 4

(E) None of above

Output

E x p l a n a t i o n :
Turbo C++ 3.0: 4 4

Turbo C ++4.5: 4 4

Linux GCC: Compilation error

Visual C++: Compilation error

p is far pointer which size is 4 byte.

By default q is near pointer which size is 2 byte.

9.
What will be output of following program?

#include<stdio.h>
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
(A) 10

(B) Address

(C) 2
(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: 10

Turbo C ++4.5: 10

Linux GCC: 10

Visual C++: 10

Void pointer can hold address of any data type without type casting. Any pointer can hold void pointer
without type casting.

10.
What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
int register a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
//if a=25

(A) 25

(B) Address

(C) 0

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: Compilation error

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error


Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.
11.
What will be output of following program?

#include<stdio.h>
int main(){
char arr[10];
arr = "world";
printf("%s",arr);
return 0;
}
(A) world

(B) w

(C) Null

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: Compilation error

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error

Compilation error Lvalue required

Array name is constant pointer and we cannot assign any value in constant data type after declaration.

12.
What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
int a,b,c,d;
char *p = ( char *)0;
int *q = ( int *q)0;
float *r = ( float *)0;
double *s = 0;
a = (int)(p+1);
b = (int)(q+1);
c = (int)(r+1);
d = (int)(s+1);
printf("%d %d %d %d",a,b,c,d);
return 0;
}
(A) 2 2 2 2

(B) 1 2 4 8

(C) 1 2 2 4

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: 1 2 4 8

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error

Address + 1 = next address

Since initial address of all data type is zero. So its

next address will be size of data type.

13.
What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}
(A) 1

(B) 5

(C) -5

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: 1


Turbo C ++4.5: 1

Linux GCC: 1

Visual C++: 2

Difference of two same type of pointer is always one.

14.
What will be output of following program?

#include<stdio.h>
unsigned long int (* avg())[3]{
static unsigned long int arr[3] = {1,2,3};
return &arr;
}
int main(){
unsigned long int (*ptr)[3];
ptr = avg();
printf("%d" , *(*ptr+2));
return 0;
}
(A) 1

(B) 2

(C) 3

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :
Turbo C++ 3.0: 3

Turbo C ++4.5: 3

Linux GCC: 3

Visual C++: 3

15.
What will be output of following program?

#include<stdio.h>
int main(){
int * p , b;
b = sizeof(p);
printf("%d" , b);
return 0;
}
(A) 2

(B) 4
(C) 8

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: 2 or 4

Turbo C ++4.5: 2 or 4

Linux GCC: 4

Visual C++: 4

since in this question it has not written p is which type of pointer. So its output will depend upon which
memory model has selected. Default memory model is small.

16.
What will be output of following program?

#include<stdio.h>
int main(){
int i = 5 , j;
int *p , *q;
p = &i;
q = &j;
j = 5;
printf("%d %d",*p,*q);
return 0;
}
(A) 5 5

(B) Address Address

(C) 5 Address

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: 5 5

Turbo C ++4.5: 5 5

Linux GCC: 5 5

Visual C++: 5 5

17.
What will be output of following program?

#include<stdio.h>
int main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
return 0;
}
(A) 5 Address

(B) Address Address

(C) Address 5

(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: Address Address

Turbo C ++4.5: Address Address

Linux GCC: Address Address

Visual C++: Address Address

Since * and & always cancel to each other.

i.e. *&a = a

so *&p = p which store address of integer i

&*p = &*(&i) //since p = &i

= &(*&i)

= &i

So second output is also address of i

18.
What will be output of following program?

#include<stdio.h>
int main(){
int i = 100;
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
return 0;
}
(A)
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
(B)
value of i : 100 addresss of i : Address
value of i : 100 addresss of i : Address
(C)
value of i : 101 addresss of i : Address
value of i : 101 addresss of i : Address
(D) Compilation error

(E) None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0:

value of i : 100 addresss of i : Address

value of i : 101 addresss of i : Address

Turbo C ++4.5:

value of i : 100 addresss of i : Address

value of i : 101 addresss of i : Address

Linux GCC:

value of i : 100 addresss of i : Address

value of i : 101 addresss of i : Address

Visual C++:

value of i : 100 addresss of i : Address


value of i : 101 addresss of i : Address

Within the scope of any variable, value of variable may change but its address will never change in any
modification of variable.

19.
What will be output of following program?

#include<stdio.h>
int main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 25;
(*p)++;
printf("%d",*q);
return 0;
}
(A) 25
(B) Address

(C) Garbage

(D) Compilation error

(E)None of above

Output

E x p l a n a t i o n :

Turbo C++ 3.0: 26

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error

Far address of p and q are representing same physical address. Physical address of

0x55550005 = 0x5555 * ox10 + ox0005 = 0x55555

Physical address of

0x53332225 = 0x5333 * 0x10 + ox2225 = 0x55555

*p = 25, means content at memory location 0x55555 is assigning value 25

(*p)++ means to increase the content by one at memory the location 0x5555 so now content of memory
location at 0x55555 is 26

*q also means content at memory location 0x55555 which is 26

20.
What will be output of following program?

#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u",i,j,k);
return 0;
}
(A) 3 Address 3

(B) 3 Address Address

(C) 3 3 3

(D) Compilation error

(E) None of above


Output

E x p l a n a t i o n :

Turbo C++ 3.0: 3 Address Address

Turbo C ++4.5: 3 Address Address

Linux GCC: 3 Address Address

Visual C++: 3 Address Address

Here 6024, 8085, 9091 is any arbitrary address, it may be different.

You might also like