Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8653621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:35:16+00:00 2026-06-12T14:35:16+00:00

I searched for x++ vs ++x and I found a great answer here ,

  • 0

I searched for x++ vs ++x and I found a great answer here, So I decide to see the assembly output of gcc to see how x++ and ++x implemented:

main() { int s = 0; ++s; return 0; }

Compiling the example:

gcc mul.c -masm=intel -o mul.asm

Output of ++s:

    .file   "mul.c"
    .intel_syntax
    .text
    .p2align 4,,15
.globl main
    .type   main, @function
main:
    lea %ecx, [%esp+4]
    and %esp, -16
    push    DWORD PTR [%ecx-4]
    push    %ebp
    mov %ebp, %esp
    push    %ecx
    sub %esp, 16
    mov DWORD PTR [%ebp-8], 0
    add DWORD PTR [%ebp-8], 1
    mov %eax, 0
    add %esp, 16
    pop %ecx
    pop %ebp
    lea %esp, [%ecx-4]
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"      

Output for x++:

    .file   "mul.c"
    .intel_syntax
    .text
    .p2align 4,,15
.globl main
    .type   main, @function
main:
    lea %ecx, [%esp+4]
    and %esp, -16
    push    DWORD PTR [%ecx-4]
    push    %ebp
    mov %ebp, %esp
    push    %ecx
    sub %esp, 16
    mov DWORD PTR [%ebp-8], 0
    add DWORD PTR [%ebp-8], 1
    mov %eax, 0
    add %esp, 16
    pop %ecx
    pop %ebp
    lea %esp, [%ecx-4]
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"

So, I am asking if x++ and ++x have different meaning, why GCC outputs the some assembly for them, shouldn’t have different output?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:35:18+00:00Added an answer on June 12, 2026 at 2:35 pm

    This is an example of a not-well written test case. The actual result of the expression value++ or ++value is never stored, as such the compiler can handle both equivalent for basic types.

    Use this examples instead:

    main() { int s = 0, x; x = ++s; return 0; }
    
    main() { int s = 0, x; x = s++; return 0; }
    

    Post-increment:

    (gdb) disas /m main
    Dump of assembler code for function main():
    1       int main(){
       0x0040138c :     push   %ebp
       0x0040138d :     mov    %esp,%ebp
       0x0040138f :     and    $0xfffffff0,%esp
       0x00401392 :     sub    $0x10,%esp
       0x00401395 :     call   0x4018d4 
    
    2               int s = 0;
       0x0040139a :    movl   $0x0,0xc(%esp)
    
    3               int x;
    4               x = s++;
       0x004013a2 :    mov    0xc(%esp),%eax
       0x004013a6 :    mov    %eax,0x8(%esp)
       0x004013aa :    incl   0xc(%esp)
    
    5               return 0;
       0x004013ae :    mov    $0x0,%eax
    
    6       }   0x004013b3 :   leave
       0x004013b4 :    ret
    
    End of assembler dump.
    (gdb)

    Pre-increment:

    (gdb) disas /m main
    Dump of assembler code for function main():
    1       int main(){
       0x0040138c :     push   %ebp
       0x0040138d :     mov    %esp,%ebp
       0x0040138f :     and    $0xfffffff0,%esp
       0x00401392 :     sub    $0x10,%esp
       0x00401395 :     call   0x4018d4 
    
    2               int s = 0;
       0x0040139a :    movl   $0x0,0xc(%esp)
    
    3               int x;
    4               x = ++s;
       0x004013a2 :    incl   0xc(%esp)
       0x004013a6 :    mov    0xc(%esp),%eax
       0x004013aa :    mov    %eax,0x8(%esp)
    
    5               return 0;
       0x004013ae :    mov    $0x0,%eax
    
    6       }   0x004013b3 :   leave
       0x004013b4 :    ret
    
    End of assembler dump.
    (gdb)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've searched and found a good discussion here on SO , but it is
I've searched and I found some things about compiling to JARs in Eclipse, but
Searched a lot about this problem but never found a answer, that solved it.
I have searched the internet in great detail and found many samples of PHP
I've searched here for the various MSDN subscription questions, but the answers I've found
I have looked and searched for an answer for that question and I found
I have searched and searched on here, the other day there was a great
I searched and found three fairly good posts but all of them were animating
I have searched and found that in cordova.plist we need to add a row
I have searched and found a lot of different things but none that actually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.