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 8662831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:49:42+00:00 2026-06-12T16:49:42+00:00

I wrote a simple code on a 64 bit machine int main() { printf(%d,

  • 0

I wrote a simple code on a 64 bit machine

int main() {
    printf("%d", 2.443);
}

So, this is how the compiler will behave. It will identify the second argument to be a double hence it will push 8 bytes on the stack or possibly just use registers across calls to access the variables. %d expects a 4 byte integer value, hence it prints some garbage value.

What is interesting is that the value printed changes everytime I execute this program. So what is happening? I expected it to print the same garbage value everytime not different ones everytime.

  • 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-12T16:49:44+00:00Added an answer on June 12, 2026 at 4:49 pm

    It’s undefined behaviour, of course, to pass arguments not corresponding to the format, so the language cannot tell us why the output changes. We must look at the implementation, what code it produces, and possibly the operating system too.

    My setup is different from yours,

    Linux 3.1.10-1.16-desktop x86_64 GNU/Linux (openSuSE 12.1)

    with gcc-4.6.2. But it’s similar enough that it’s reasonable to suspect the same mechanisms.

    Looking at the generated assembly (-O3, out of habit), the relevant part (main) is

    .cfi_startproc
    subq    $8, %rsp             # adjust stack pointer
    .cfi_def_cfa_offset 16
    movl    $.LC1, %edi          # move format string to edi
    movl    $1, %eax             # move 1 to eax, seems to be the number of double arguments
    movsd   .LC0(%rip), %xmm0    # move the double to the floating point register
    call    printf
    xorl    %eax, %eax           # clear eax (return 0)
    addq    $8, %rsp             # adjust stack pointer
    .cfi_def_cfa_offset 8
    ret                          # return
    

    If instead of the double, I pass an int, not much changes, but that significantly

    movl    $47, %esi            # move int to esi
    movl    $.LC0, %edi          # format string
    xorl    %eax, %eax           # clear eax
    call    printf
    

    I have looked at the generated code for many variations of types and count of arguments passed to printf, and consistently, the first double (or promoted float) arguments are passed in xmmN, N = 0, 1, 2, and the integer (int, char, long, regardless of signedness) are passed in esi, edx, ecx, r8d, r9d and then the stack.

    So I venture the guess that printf looks for the announced int in esi, and prints whatever happens to be there.

    Whether the contents of esi are in any way predictable when nothing is moved there in main, and what they might signify, I have no idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this simple C code and compiled it using Visual Studio 2010, with
I've wrote this simple piece of code. And I have a slight problem with
I wrote this simple piece of code(it actually calculates sine, cosine or tangent values
I wrote a very simple test code of printf uint64_t: #include <inttypes.h> #include <stdio.h>
I am new to bit manipulations tricks and I wrote a simple code to
I wrote a short bit of code to simply skip num_lines lines in an
I wrote a simple code to capture the netdevice notifications and simply print their
Hello every one I have wrote a simple code in html5 in which I
I wrote a simple plugin which sets some css code using wp_options. It all
I wrote a simple example that I expected would do it. The relevant code

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.