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

  • Home
  • SEARCH
  • 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 1060507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:20:15+00:00 2026-05-16T18:20:15+00:00

#include <stdio.h> int main(void) { double resd = 0.000116; long long resi = 0;

  • 0
#include <stdio.h>

int main(void)
{
        double resd = 0.000116;
        long long resi = 0;

        printf("%lld %f %lld %f\n", resd, resd, resi, resi);
        return 0;
}

gives (Linux, gcc, x64)

0 0.000116 0 0.000116
             ^^^^^^^^ odd, since the memory for resi is zeroed

Actually, compiled with g++ it gives random results instead of the second 0.

I understand I gave invalid specifiers to printf and that it triggers unspecified undefined behavior, but I wonder why this specific corruption occurs, since long long and double have the same size.

  • 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-05-16T18:20:15+00:00Added an answer on May 16, 2026 at 6:20 pm

    I get the same results as you do on my machine (Mac OS X, so AMD/Linux ABI). The floating point parameters are passed in XMM registers and the integer parameters in integer registers. When printf grabs them using va_arg, it pulls from XMM when it sees the %f format, and from the other registers when it sees %lld. Here’s the disassembly for your program as compiled (-O0) on my machine:

     1 _main:
     2   pushq   %rbp
     3   movq    %rsp,%rbp
     4   subq    $0x20,%rsp
     5   movq    $0x3f1e68a0d349be90,%rax
     6   move    %rax,0xf8(%rbp)
     7   movq    $0x00000000,0xf0(%rbp)
     8   movq    0xf0(%rbp),%rdx
     9   movq    0xf0(%rbp),%rsi
    10   movsd   0xf8(%rbp),%xmm0
    11   movq    0xf8(%rbp),%rax
    12   movapd  %xmm0,%xmm1
    13   movq    %rax,0xe8(%rbp)
    14   movsd   0xe8(%rbp),%xmm0
    15   lea     0x0000001d(%rip),%rdi
    16   movl    $0x00000002,%eax
    17   callq   0x100000f22    ; symbol stub for: _printf
    18   movl    $0x00000000,%eax
    19   leave
    20   ret
    

    There you can see what’s going on – the format string is passed in %rdi, then your parameters are passed (in order) in: %xmm0, %xmm1, %rsi, and %rdx. When printf
    gets them, it pops them off in a different order (the order specified in your format string). That means it pops them: %rsi, %xmm0, %rdx, %xmm1, giving the results you see. The 2 in %eax is to indicate the number of floating point arguments passed.

    Edit:

    Here’s an optimized version – in this case the shorter code might be easier to understand. The explanation is the same as above, but with a little less boilerplate noise. The floating point value is loaded by the movsd on line 4.

     1 _main:
     2    pushq   %rbp
     3    movq    %rsp,%rbp
     4    movsd   0x00000038(%rip),%xmm0
     5    xorl    %edx,%edx
     6    xorl    %esi,%esi
     7    movaps  %xmm0,%xmm1
     8    leaq    0x00000018(%rip),%rdi
     9    movb    $0x02,%al
    10    callq   0x100000f18   ; symbol stub for: _printf
    11    xorl    %eax,%eax
    12    leave
    13    ret
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> int main (void) { double f; printf (What is the
#include<stdio.h> #include<conio.h> int main (void) { int a,b,c,d; clrscr(); a=3; b=5; c=a,b; d=(a,b); printf(c=%d,c);
Consider the following program: #include <stdio.h> int main(void) { return 0; } When i
#include<stdio.h> void main(){ int x,y,z; x=y=z=1; z=++x||++y&&++z; printf(%d %d %d \n,x,y,z); getch(); } the
#include <stdio.h> int main(void){ float a = 1.1; double b = 1.1; if(a ==
I have the following code: #include <stdio.h> #include <math.h> int main(void) { printf(%f\n, fmax(1.2,
#include <stdio.h> #include <limits.h> int main(void){ printf("Type Size Min Max\n----------------------------------------------------------------------\n"); printf("%-20s%-10d%-20ld%-20ld\n", "long", sizeof(long), LONG_MIN,
Possible Duplicate: Why does sizeof(x++) not increment x? #include<stdio.h> int main(void) { double num=5.2;
#include <stdio.h> #include <stdlib.h> int main(void) { int x; int *in, *begin; in =
consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same

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.