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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:02:50+00:00 2026-05-23T17:02:50+00:00

I came across this code: #include<stdio.h> void main() { int x; float t; scanf(%f,&t);

  • 0

I came across this code:

#include<stdio.h>
void main()
{
    int x;
    float t;
    scanf("%f",&t);
    printf("%d\n",t);
    x=90;
    printf("%f\n",x);
    {
        x=1;
        printf("%f\n",x);
        {
            x=30;
            printf("%f\n",x);
        }
        printf("%f\n",x);
    }
    printf("%f\n",x);
}  

Glancing at it I thought of it as some undefined output as quoted in standards:

A warning: printf uses its first argument to decide how many arguments
follow and what their type is. It will get confused, and you will get
wrong answers, if there are not enough arguments of if they are the
wrong type.

But the output didn’t let me leave this question without giving it second thought.
(input given is 23).

23 
0
23.000000
23.000000
23.000000
23.000000
23.000000

Why always 23.00000? What is the compiler actually trying to do here? Instead of messing around with the value stored at x, why does it print the value of t? Does it have any explanation, because there seems something defined about this undefined output (pun intended).

Edit:

I am using gcc compiler on 32 bit machine.

  • 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-23T17:02:51+00:00Added an answer on May 23, 2026 at 5:02 pm

    The program behavior is undefined, the compiler is permitted to do anything at all.

    That said, I am able to reproduce this behavior on my system and a glance at the assembly output shows what happens:

    printf("%d\n",t); begins by loading the floating point value from t into the CPU register %xmm0 which is used on my platform to pass floating-point arguments to functions. That register is not accessed by this call to printf(), since it is looking for an integer input instead.

    None of the subsequent calls to printf() load any values into %xmm0, since you’re not passing any floating-point values into printf or into any other function. But when each printf encounters %f in its format string, it reads from %xmm0, which still contains 23.0

    Assembly output, from CLang, using a simpler program with float t = 23.0;

    .LCPI0_0:
        .quad   4627167142146473984     # double 2.300000e+01
    ...
        movl    $.L.str, %edi          # .L.str is "%d\n"
        movsd   .LCPI0_0(%rip), %xmm0  # 23.0 stored in xmm0 here
        movb    $1, %al
        callq   printf                 # this printf will print %esi
    
        movl    $90, %esi              # 90 stored in %esi here
        movl    $.L.str1, %edi         # .L.str1 is "%f\n"
        xorb    %al, %al
        callq   printf                 # but this printf will print %xmm0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this piece of code on a website. main(i) { gets(&i); puts();
I came across this in the Python interpreter source code. void PyThread_delete_key_value(int key) {
Looking at an open source code base i came across this code: #include StableHeaders.h
While writing some code i came across this issue: #include <iostream> class random {
I came across this code showing format string exploitation while reading this article. #include
I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with
I just came across this code and a few Google searches turn up no
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;
I recently came across this in some code - basically someone trying to create
I came across this line in some code and can't find the syntax defined

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.