Thread: Simple c program-please explain output.

  1. #1
    Registered User hitesh.incept's Avatar
    Join Date
    Sep 2012
    Posts
    2

    Exclamation Simple c program-please explain output.

    Code:
    int main()
    {
         int a=7, t=0;
         t=--a+--a+a+++a;
         printf("%d",t);
    }

    output: 20


    I could not understand why it prints '20' ? please explain it !

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read this FAQ series on expressions in C. Basically, you are looking at the results of undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Code:
            .file   "cpb19.c"
            .section        .rodata
    .LC0:
            .string "%d"
            .text
    .globl main
            .type   main, @function
    main:
            leal    4(%esp), %ecx
            andl    $-16, %esp
            pushl   -4(%ecx)
            pushl   %ebp
            movl    %esp, %ebp
            pushl   %ecx
            subl    $36, %esp
            movl    $7, -8(%ebp)
            movl    $0, -12(%ebp)
            subl    $1, -8(%ebp)        <<<<---  '--a' now 'a' is 6
            subl    $1, -8(%ebp)        <<<<---  '--a' now 'a' is 5
            movl    -8(%ebp), %eax
            addl    -8(%ebp), %eax    <<<<--- adding  '--a+--a' 5+5=10 
            addl    -8(%ebp), %eax    <<<<--- adding   10+5
            addl    -8(%ebp), %eax    <<<<--- adding    15+5
            movl    %eax, -12(%ebp)
            addl    $1, -8(%ebp)
            movl    -12(%ebp), %eax
            movl    %eax, 4(%esp)
            movl    $.LC0, (%esp)
            call    printf
            addl    $36, %esp
            popl    %ecx
            popl    %ebp
            leal    -4(%ecx), %esp
            ret
            .size   main, .-main
            .ident  "GCC: (GNU) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)"
            .section        .note.GNU-stack,"",@progbits
    This is the assembly instruction flow for the given program. You can see the execution flow.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The same answers as you got over here -> Simple c program-please explain output. - Dev Shed

    If you'd bothered to read my post, and realised that different compilers give different answers, you would soon realise that the code is broken and the question is meaningless.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, but please don't get fascinated with this kind of useless code. It is not just non-compliant C, but violates the principle that code should be understandable, as well.

    (Exception: obfuscation contests, which are mad fun, and never meant to be understood).

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > This is the assembly instruction flow for the given program. You can see the execution flow.
    That's the assembly output for your compiler. Undefined Behavior can produce different results depending on what compiler you use.

  7. #7
    Registered User hitesh.incept's Avatar
    Join Date
    Sep 2012
    Posts
    2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output Problem, Very simple program
    By MrAlt in forum C Programming
    Replies: 6
    Last Post: 01-15-2012, 08:34 PM
  2. getting wrong output from simple program
    By d387420489 in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 06:21 PM
  3. what is the program output please explain
    By sunil17 in forum C Programming
    Replies: 2
    Last Post: 09-02-2010, 08:34 AM
  4. plz explain the output of the program.
    By raj_ksrt in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 03:43 AM
  5. Explain the output of the C program
    By abacus00 in forum C Programming
    Replies: 0
    Last Post: 03-24-2003, 06:24 PM