Tuesday, January 15, 2008

C Debugging

Hi Friends,
Here we are going to discuss about debugging on three different OS and compiler. The output is based on how compiler written. It’s not based on operating system. So if one could able to predict the output of a compiler, he is good in that compiler. But consider the situation, you are going to attend written test (1st round interview). Most of the companies they won’t specify which compiler’s output they are expecting they simply distribute the question paper they wont specify anything on that question paper you have to ask if you have doubt otherwise they wont tell. I am not saying all programs output will be different some logic the way compiler generates code for that, that’s different.


So here we will take windows, Solaris and Linux.

Windows --- Turbo compiler
Solaris --- GCC compiler
Linux --- GCC compiler



Now we will start understanding programs.

/* Example 1*/

main()
{
int i = 10;
i = ++i + ++i + ++i + ++i;
printf("%d",i);
}

Windows turbo compiler
56

Solaris GCC compiler
50

Linux GCC compiler
51


Explanation:

Turbo c compiler evaluates the above expression as

i = i + 1;
i = i + 1;
i = i + 1;
i = i + 1;


i = i + i + i + i; \\ 14 + 14 + 14 + 14

Solaris GCC compiler evaluates the above expression as


mov ax, 0
inc i;
add ax, i
inc i
add ax, i
inc i
add ax, i
inc i
add ax, i
mov i, ax;


solaris compiler generates code according to the human behaviour thats one by one execution.

Linux GCC compiler evaluates the above expression as


inc i
inc i
mov ax, i
add ax, i
inc i
add ax, i
inc i
add ax, i

No comments: