in Programming in C
1,268 views
0 votes
0 votes
int main()
{
   int a;     
   char *x;
   x = (char *) &a;
   a = 512;
   x[0] = 1;
   x[1] = 2;
   printf("%d\n",a);   
   return 0;
}


output?
A. M/C dependent
B. 513
C. 258
D. compiler error

in Programming in C
1.3k views

4 Answers

1 vote
1 vote
Best answer

A. Machine dependent. 

  x = (char *) &a;
  a = 512;
  x[0] = 1;
  x[1] = 2;

Here, we modify the first and second bytes from the starting location of a. So, this modification depends on whether the machine is little endian (LSB stored at lowest address) or big endian (MSB stored at lowest address). 

Assuming Little endian and 4 bytes for int, we would be getting:

Initial content of $a$

0 0x02 0 0

Final content of $a$

0x01 0x02 0 0 = 2 * 256 + 1 = 513

Assuming Big endian and 4 bytes for int, we would be getting:

Initial content of $a$

0 0 0x02 0

Final content of $a$:


0x01 0x02 0x02 0

(Here MSB is on left)

= 1*256^3 + 2*256^2 + 2*256 = 16, 908, 800
selected by
by

1 comment

well explained, thanx.
1
1
10 votes
10 votes

Ans. d) Compiler Error

you are using a without declaring a

x = (char *) &a;

See here : http://ideone.com/iXIzZ4

you are trying to get the address of a identifier which have not declared and defined. Once you define a variable then only compiler will allocate memory space to it and once you will have a memory allocated to it then only you can get the address of that memory.


Once you are declaring 'a' output of program will be dependent on which machine you are running this program whether its Big Endian/Little Endian.

Lets assume that you are using a machine which stores data in Little Endian format and integer is of size 4  bytes. So once you are defining variable like below

int a;

It will allocate a 4 bytes to it. So for you have not assigned a value to it, this integer will contain a garbage value. Suppose memory is byte addressable and memory allocated to int is starts with memory location 2012 - 2015 where location 2012th byte contain least significant bit of the 4 byte integer. (Here i have assumed little endian format. Check it for more detail : http://williams.comp.ncat.edu/Endian.htm)

x = (char *) &a;

This way &a will represent the starting address of integer i.e. 2012 and once you are type casting it into char pointer still char pointer x will hold the address 2012.

a = 512

It will assign the value to the integer as 512. Bit at 2012 to 2015 will look like

  00000000   00000000   00000010  00000000
      2015           2014          2013          2012

X[0] = 1

it will put 1 at the location (x+0) i.e. 2012th byte will be changed to 00000001

x[1] = 2

it will put 2 at the location (x+1) i.e. 2013th byte will be changed to 00000010

Now the complete memory from 2012 to 2015 will be look like

  00000000   00000000   00000010  00000001
      2015           2014           2013         2012

Its decimal equivalent will be 513.

** That's what you have got. In case you would have using a machine which uses Big Endian format to store binary numbers.
Then the value of 'a' would have become (00000000   00000010    00000010     00000001)2 = (131585)10.

4 Comments

@Aditya you are right yes

@Himani Please check i have updated the answer.

0
0
got it thanku
0
0
x is not an array but still we can do

X[0]=*(x+0)

but is it always possible to do with any integer define in any program?
0
0
0 votes
0 votes
I think B)

cause char 8 bit so

X[1] = 2 = 00000010

X[0] = 1= 00000001

so 0000001000000001 =513

(i dont know it maybe compiler dependent also because size of char may vary machine to machine )

.peace

1 comment

size of char is fixed in C- it is 1 byte. It is also used for 1 byte int.

It is compiler/implementation dependent in case of big endian machine, as the output depends on sizeof int. But better answer is machine dependent.
1
1
0 votes
0 votes
Output is 513 in a little endian machine. To understand this output, let integers be stored using 16 bits. In a little endian machine, when we do x[0] = 1 and x[1] = 2, the number a is changed to 00000001 00000010 which is representation of 513 in a little endian machine.

So, answer is (a) Machine dependent.

Related questions