Jaspreet wrote on 16/09/05 :
> I was recently asked this question in an interview. Unfortunately I was
> not able to answer it and the interviewer made a decision on my C
> strengths (or weekness) based on this single question and that was a
> sad end to my interview. Here is the program:
>
> #include <stdio.h>
>
> int main()
> {
> char *c ="abc";
> int *i=(int*)c;
Undefined behaviour (UB). There is no guarantee that the address of a
string literal is correctly aligned for a int.
> printf("%x", *i);
Undefined behaviour. "%x" expects an unsigned int.
A '\n' is missing (unterminated line). The result may appear or not.
> return 0;
> }
>
> The output is:
> 636261.
Once the UB's fixed, the behaviour is implementation-dependent (size of
an int, endianness, charset). The result is target-dependent. It can't
be explainded without knowing the details of the implementation.
> I know that hex 61 is decimal 97 which is the ASCII code for a. hex 62
> is code for b and so on. My query is why is it printing the ascii codes
> in the reverse order.
Try on a Mac...
> I apologise if this questios is a very basic one.
This question is idiotic. The interviewer is a idiot. The company that
hires such an idiotic is simply going to die. Try another place.
--
Emmanuel
The C-FAQ:
http://www.eskimo.com/~scs/C-faq/faq.html
The C-library:
http://www.dinkumware.com/refxc.html
"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++