Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
tell me the output :D
  • hey guys just guess and tell me what's the output of this code.. Remember don't try to run before gussing :D:D:D:D

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a=1;
    printf(\"%d%d%d\",a,++a,a++);
    getch();
    }

    :D:D:D just guess and tell me :D:D
  • Deque
    Posts: 78
    The behaviour is undefined, because there is no sequence point between the increments.
  • Sh3llc0d3
    Posts: 1,910
    ...I'll be surprised to see how many people get this.

    [spoiler]void main in many compilers should NOT compile (it doesn't in code-blocks) - It isn't part of the C standard. The code-block wants to return an integer... but datatype isn't specified (int main()). int main(void) would probably be the most correct code.

    Compile the code with the change, mine returned 331 - yes this is compiler dependent. %d is a specifier (it formats the output for printf into a single character (I believe)). The answer should be... 221.

    Increment/decrement being the suffix and the prefix.
    int a, b = 5, c;

    a = b++; // a = 5, b = 6
    c = ++b; // c = 7, b = 7


    The printf statement is read into the stack from stack from left to right just so you know ;)

    Ironically - just a mention - some beginners books teach void main() :\

    A C coder will i'm sure correct me if i'm wrong about anything.[/spoiler]
  • sangf
    Posts: 203
    don't you mean 122 :P?
  • Sh3llc0d3
    Posts: 1,910
    left to right it would be 122 but if it's read into the stack from right to left then it'll be performed and output in that order won't it?

    EDIT: Just noticed I put left to right... in previous post... it should be right to left.
  • Null Set
    Posts: 112
    said:


    ...I'll be surprised to see how many people get this.

    [spoiler]void main in many compilers should NOT compile (it doesn't in code-blocks) - It isn't part of the C standard. The code-block wants to return an integer... but datatype isn't specified (int main()). int main(void) would probably be the most correct code.

    Compile the code with the change, mine returned 331 - yes this is compiler dependent. %d is a specifier (it formats the output for printf into a single character (I believe)). The answer should be... 221.

    Increment/decrement being the suffix and the prefix.

    int a, b = 5, c;

    a = b++; // a = 5, b = 6
    c = ++b; // c = 7, b = 7


    The printf statement is read into the stack from stack from left to right just so you know ;)

    Ironically - just a mention - some beginners books teach void main() :\

    A C coder will i'm sure correct me if i'm wrong about anything.[/spoiler]


    I do think the answer really is...
    [spoiler]
    331. Looking at the ASM disassembly, we have:

    0x080483c4 <+0>: push %ebp
    0x080483c5 <+1>: mov %esp,%ebp
    0x080483c7 <+3>: and $0xfffffff0,%esp
    0x080483ca <+6>: sub $0x20,%esp
    0x080483cd <+9>: movl $0x1,0x1c(%esp) # assignment of a
    0x080483d5 <+17>: mov 0x1c(%esp),%edx # moving a = 1 to edx
    0x080483d9 <+21>: addl $0x1,0x1c(%esp) # value at (0x1c)(%esp) incremented by 1 -> = 2
    0x080483de <+26>: addl $0x1,0x1c(%esp) # value at (0x1c)(%esp) incremented by 1 -> = 3
    0x080483e3 <+31>: mov $0x80484d0,%eax
    0x080483e8 <+36>: mov %edx,0xc(%esp) # value of edx copied to location 0xc(%esp) [edx = 1 at this time
    0x080483ec <+40>: mov 0x1c(%esp),%edx # copies value at 0x1c to edx [making edx = 3]
    0x080483f0 <+44>: mov %edx,0x8(%esp) # puts value of edx [3] to 0x8(%esp)
    0x080483f4 <+48>: mov 0x1c(%esp),%edx # copies value at 0x1c to edx [making edx = 3 still]
    0x080483f8 <+52>: mov %edx,0x4(%esp) # puts value of edx to 0x4(%esp)
    0x080483fc <+56>: mov %eax,(%esp)
    0x080483ff <+59>: call 0x80482f4 <printf@plt>
    0x08048404 <+64>: leave
    0x08048405 <+65>: ret


    %esp's offset would look like this before the function is called then:


    0x1c = 3
    0x18 = ~
    0x14 = ~
    0x10 = ~
    0x0c = 1 ######################################################
    0x08 = 3 # Arguments of a function are pushed with last first #
    0x04 = 3 ######################################################
    0x00 = ~


    So, yeah, I really think it's 331, not 221 :) Sorry, Sh3llc0d3

    Sangf, this should also answer why it's not 122 :)
    [/spoiler]
  • Sh3llc0d3
    Posts: 1,910
    My bad :p never could stop overthinking shit lol. Nicely demo'd Null :)
  • sangf
    Posts: 203
    yeah, compiled with msvc++ or gcc the result will be 331 (which Sh3llc0d3 did point out); so of course that will be reflected in a disasm :p it's not what i would have thought would be the answer, though. anyway, i think i like deques answer best: http://c-faq.com/expr/evalorder2.html =]
  • Deque
    Posts: 78
    Here is another very good explanation why the result is ambiguous (read through the top answer):
    http://stackoverflow.com/questions/4176 ... nce-points
  • nicely explain by shellcode.. because in c there is only two way to pass a value in variables one is from left to right and another is from right to left and it was the example of right to left :P..
    So when a++ occured then the value of a is only 1.. because it first print the value and then increment it.. so in actuallity the value of a becomes 2.. A
    nd when ++a occured it increment the value before print it and then it print the value,, so value of a ; printed is 3.. and now the current value of a is 3.. so it is printed by the a..
    Hence our output becomes 3 3 1.. :D:D[hr]
    correctly guessed by deque :P
  • sangf
    Posts: 203
    ah i was wondering what you guys were talking about with the right-to-left stack comments, you were talking about how parameters were pushed using the cdecl calling convention, i didn't even consider that; now the random value makes sense to me haha.
  • sangf lol..... :D:D:D:D:P