Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > predict the answer

Reply
Thread Tools

predict the answer

 
 
sophia
Guest
Posts: n/a
 
      03-07-2008
Dear all,

The following is the question which i saw in a C column contest in a
magazine

what will be the output of

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int a[] = {10,20,30,40,50};
int *p;
p = (int*)((char*)a + sizeof(int));

printf("%d",*p);
return EXIT_SUCCESS;
}

and the winning answer was :-

This question may have different answers depending on the compiler
used(16/32 bit) and the processor used

case 1:- a 32 bit compiler and a little endian machine

here sizeof int will be 4. now a points to first element in the array
i.e 10 ( 00001010 00000000 00000000 00000000) and (char*)a points to
00001010.
now (char*)a + 4 means (char*)a + 4 * sizeof(char)
i.e (char*)a + 4 points to 4 byte positions away from the current
position i.e it will point to byte position (00010100) of 20(00010100
00000000 00000000 00000000 ) so output will be 20 itself


case 2:- a 32 bit compiler and a big endian machine

it will point to byte position (00000000) of 20(00000000 00010100
00000000 00000000) so output will be 0

case 3:- a 16 bit compiler and a little endian machine


Here size of int will be 2 now a points to the first element in the
array i.e 10 now (char *)a + 2 points to 2 byte positions away from
the current position i.e it will point to lower byte position of 20
1.e( 00010100 00000000) so output will be 20 itself

case 4:- a 16 bit compiler and a big endian machine
it will point to byte position (00000000) of 20 i.e(00000000 00010100)
so output will be 0

how correct is the above answer ?
can any improvements be made in this answer ?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-07-2008
sophia said:

> Dear all,
>
> The following is the question which i saw in a C column contest in a
> magazine
>
> what will be the output of
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int a[] = {10,20,30,40,50};
> int *p;
> p = (int*)((char*)a + sizeof(int));
>
> printf("%d",*p);
> return EXIT_SUCCESS;
> }
>
> and the winning answer was :-
>
> This question may have different answers depending on the compiler
> used(16/32 bit) and the processor used
>
> case 1:- a 32 bit compiler and a little endian machine
>
> here sizeof int will be 4. now a points to first element in the array
> i.e 10 ( 00001010 00000000 00000000 00000000) and (char*)a points to
> 00001010.
> now (char*)a + 4 means (char*)a + 4 * sizeof(char)
> i.e (char*)a + 4 points to 4 byte positions away from the current
> position i.e it will point to byte position (00010100) of 20(00010100
> 00000000 00000000 00000000 ) so output will be 20 itself
>
>
> case 2:- a 32 bit compiler and a big endian machine
>
> it will point to byte position (00000000) of 20(00000000 00010100
> 00000000 00000000) so output will be 0
>
> case 3:- a 16 bit compiler and a little endian machine
>
>
> Here size of int will be 2 now a points to the first element in the
> array i.e 10 now (char *)a + 2 points to 2 byte positions away from
> the current position i.e it will point to lower byte position of 20
> 1.e( 00010100 00000000) so output will be 20 itself
>
> case 4:- a 16 bit compiler and a big endian machine
> it will point to byte position (00000000) of 20 i.e(00000000 00010100)
> so output will be 0
>
> how correct is the above answer ?
> can any improvements be made in this answer ?


Here's the code again:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int a[] = {10,20,30,40,50};
int *p;
p = (int*)((char*)a + sizeof(int));

printf("%d",*p);
return EXIT_SUCCESS;
}

int a[] = {10,20,30,40,50}; defines an array of int with five elements.
int *p; defines a pointer to int.
a yields &a[0], the address of the lowest-address byte in the a array,
which is guaranteed to be aligned correctly for int because it's an int
array.
(char *)a yields the same address, but with type char *.
Because arrays are guaranteed to be contiguous with no padding before or
after any array member, (char *)a + sizeof(int) yields the address of the
second int in the array, with type char *. Again, this is guaranteed to be
aligned correctly for int.
(int *) yields the same address as its operand, but with type int *.
p = assigns that address to p.
printf("%d", *p); will write "20" to the standard output device.
Guaranteed. (Well, printf can fail, and of course there's no newline, but
you know what I mean.)

It seems to me that the answer you saw is completely broken.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2008
sophia wrote:
> Dear all,
>
> The following is the question which i saw in a C column contest in a
> magazine
>
> what will be the output of
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int a[] = {10,20,30,40,50};
> int *p;
> p = (int*)((char*)a + sizeof(int));
>
> printf("%d",*p);
> return EXIT_SUCCESS;
> }
>
> and the winning answer was :-
>
> This question may have different answers depending on the compiler
> used(16/32 bit) and the processor used
>
> case 1:- a 32 bit compiler and a little endian machine
>
> here sizeof int will be 4. now a points to first element in the array
> i.e 10 ( 00001010 00000000 00000000 00000000) and (char*)a points to
> 00001010.
> now (char*)a + 4 means (char*)a + 4 * sizeof(char)
> i.e (char*)a + 4 points to 4 byte positions away from the current
> position i.e it will point to byte position (00010100) of 20(00010100
> 00000000 00000000 00000000 ) so output will be 20 itself
>
>
> case 2:- a 32 bit compiler and a big endian machine
>
> it will point to byte position (00000000) of 20(00000000 00010100
> 00000000 00000000) so output will be 0
>

No, it will not. p points the the second int in the array, byte order
(or sizeof int ) has no effect.

> how correct is the above answer ?


Incorrect.

--
Ian Collins.
 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      03-07-2008
sophia wrote:
> Dear all,
>
> The following is the question which i saw in a C column contest in a
> magazine
>
> what will be the output of
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int a[] = {10,20,30,40,50};
> int *p;
> p = (int*)((char*)a + sizeof(int));
>
> printf("%d",*p);
> return EXIT_SUCCESS;
> }
>
> and the winning answer was :-


0. Nothing, as you didn't emit a terminating \n in the printf
above (without it an implementation is not required to output
anything on the standard output).

I haven't read the rest very carefully, but they seems sound,
and also off topic in this newsgroup, as they go beyond the
Standard C, and into the particulars of implementation. I'm sure
this will be pointed out by others, too.
 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      03-07-2008
Vladimir Oka wrote:
> I haven't read the rest very carefully, but they seems sound, and also


Obviously so (I actually didn't go past the 1. and a bit of 2.).

Richard and Ian were of course correct.

Sophia, are you sure you copied the code correctly, as there are
ways to make it behave in a fashion similar to the given
answers? This is just not one of those.


 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      03-07-2008

"sophia" <(E-Mail Removed)> wrote:

> Dear all,
>
> The following is the question which i saw in a C column contest in a
> magazine
>
> what will be the output of
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int a[] = {10,20,30,40,50};
> int *p;
> p = (int*)((char*)a + sizeof(int));
>
> printf("%d",*p);
> return EXIT_SUCCESS;
> }
>
> and the winning answer was :-
>
> This question may have different answers depending on the compiler
> used(16/32 bit) and the processor used
>
> case 1:- a 32 bit compiler and a little endian machine
>
> here sizeof int will be 4. now a points to first element in the array
> i.e 10 ( 00001010 00000000 00000000 00000000) and (char*)a points to
> 00001010.
> now (char*)a + 4 means (char*)a + 4 * sizeof(char)
> i.e (char*)a + 4 points to 4 byte positions away from the current
> position i.e it will point to byte position (00010100) of 20(00010100
> 00000000 00000000 00000000 ) so output will be 20 itself
>
>
> case 2:- a 32 bit compiler and a big endian machine
>
> it will point to byte position (00000000) of 20(00000000 00010100
> 00000000 00000000) so output will be 0
>
> case 3:- a 16 bit compiler and a little endian machine
>
>
> Here size of int will be 2 now a points to the first element in the
> array i.e 10 now (char *)a + 2 points to 2 byte positions away from
> the current position i.e it will point to lower byte position of 20
> 1.e( 00010100 00000000) so output will be 20 itself
>
> case 4:- a 16 bit compiler and a big endian machine
> it will point to byte position (00000000) of 20 i.e(00000000 00010100)
> so output will be 0
>
> how correct is the above answer ?


Not correct at all. It will always print 20, because the
pointer always points at the 20, and it's always an int
pointer.

> can any improvements be made in this answer ?


The answer is wrong for the code. But I think your code
could be made to produce that answer, yes. Like so:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int a[] = {10,20,30,40,50};
int b = 0;

b = (int)(*((char*)a + sizeof(int)));
printf("%d\n", b);
return EXIT_SUCCESS;
}

Now I think *THAT* will produce just the answers you gave,
because the pointer is now a char pointer, not an int
pointer, so when you dereference it with the "*" I think
it gives the byte that it points to (the lowest-RAM-address
byte of the "20"), cast back to an int. So I think it will
print "20" for little-endian and "0" for big-endian,
regardless of sizeof(int).

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant


 
Reply With Quote
 
sophia
Guest
Posts: n/a
 
      03-07-2008
On Mar 7, 12:30 pm, Ian Collins <(E-Mail Removed)> wrote:
> sophia wrote:
> > Dear all,

>
> > The following is the question which i saw in a C column contest in a
> > magazine

>
> > what will be the output of

>
> > #include <stdio.h>
> > #include <stdlib.h>

>
> > int main(void)
> > {
> > int a[] = {10,20,30,40,50};
> > int *p;
> > p = (int*)((char*)a + sizeof(int));

>
> > printf("%d",*p);
> > return EXIT_SUCCESS;
> > }

>
> > and the winning answer was :-

>
> > This question may have different answers depending on the compiler
> > used(16/32 bit) and the processor used

>
> > case 1:- a 32 bit compiler and a little endian machine

>
> > here sizeof int will be 4. now a points to first element in the array
> > i.e 10 ( 00001010 00000000 00000000 00000000) and (char*)a points to
> > 00001010.
> > now (char*)a + 4 means (char*)a + 4 * sizeof(char)
> > i.e (char*)a + 4 points to 4 byte positions away from the current
> > position i.e it will point to byte position (00010100) of 20(00010100
> > 00000000 00000000 00000000 ) so output will be 20 itself

>
> > case 2:- a 32 bit compiler and a big endian machine

>
> > it will point to byte position (00000000) of 20(00000000 00010100
> > 00000000 00000000) so output will be 0

>
> No, it will not. p points the the second int in the array, byte order
> (or sizeof int ) has no effect.
>
> > how correct is the above answer ?

>
> Incorrect.
>


I have checked the answer on little endian machines using the
following
compilers gcc 4.03, Dev -C++ ,turbo C++ 3.0
and the answer seems to be correct

But I haven't checked the answer on a big endian machine

but as per the diagram given here:- http://www.df.lth.se/~pi/endian.html
the answer should be correct with the modification given
by Robbie Hatley (int)(*((char*)a + sizeof(int)));


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-07-2008
sophia said:

<snip>

> I have checked the answer on little endian machines using the
> following
> compilers gcc 4.03, Dev -C++ ,turbo C++ 3.0
> and the answer seems to be correct
>
> But I haven't checked the answer on a big endian machine


Question: What is the Universal Gravitational Constant?
Answer: If we're on Earth, it's 6.673E-11 mmm/kg/ss. On Mars, it's 42. On
Jupiter, it's 6.

I have checked the answer on Earth, and the answer seems to be correct.

But I haven't checked the answer on Mars or Jupiter.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
sophia
Guest
Posts: n/a
 
      03-07-2008
On Mar 7, 3:31 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> sophia said:
>
> <snip>
>
> > I have checked the answer on little endian machines using the
> > following
> > compilers gcc 4.03, Dev -C++ ,turbo C++ 3.0
> > and the answer seems to be correct

>
> > But I haven't checked the answer on a big endian machine

>
> Question: What is the Universal Gravitational Constant?
> Answer: If we're on Earth, it's 6.673E-11 mmm/kg/ss. On Mars, it's 42. On
> Jupiter, it's 6.
>
> I have checked the answer on Earth, and the answer seems to be correct.
>
> But I haven't checked the answer on Mars or Jupiter.


Thats really a great answer. you have a great humour sense.
Any way just to satisfy my curiosity

The value of Boltzmann constant is 1.380 6504(24) 10-23 J/K
on Earth what will be its value on other inter dimensional worlds
if they exists OR at least in Mars ?


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-07-2008
sophia wrote:

> On Mar 7, 3:31 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> sophia said:
>>
>> <snip>
>>
>> > I have checked the answer on little endian machines using the
>> > following
>> > compilers gcc 4.03, Dev -C++ ,turbo C++ 3.0
>> > and the answer seems to be correct

>>
>> > But I haven't checked the answer on a big endian machine

>>
>> Question: What is the Universal Gravitational Constant?
>> Answer: If we're on Earth, it's 6.673E-11 mmm/kg/ss. On Mars, it's
>> 42. On Jupiter, it's 6.
>>
>> I have checked the answer on Earth, and the answer seems to be
>> correct.
>>
>> But I haven't checked the answer on Mars or Jupiter.

>
> Thats really a great answer. you have a great humour sense.
> Any way just to satisfy my curiosity
>
> The value of Boltzmann constant is 1.380 6504(24) 10-23 J/K
> on Earth what will be its value on other inter dimensional worlds
> if they exists OR at least in Mars ?


Did you understand what Richard wrote? The answer is the same as for G.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Do I Predict Image Scaling With of Photofinisher Prints? kc Digital Photography 5 03-24-2007 07:34 PM
Another Stupid Hardware Questoin -- Canyou Predict Laptop Prices? Mark Adams Computer Support 5 11-17-2006 01:03 AM
Predict Java's Future in a New Google Game Amir Michail Java 1 01-14-2005 10:24 AM
Predict Java's Future in a New Google Game Amir Michail Java 0 01-14-2005 08:03 AM
predict directory write permission under windows? Qiangning Hong Python 2 12-13-2004 03:03 PM



Advertisments