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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:38:15+00:00 2026-06-13T17:38:15+00:00

Possible Duplicate: print the float value in integer in C language I am trying

  • 0

Possible Duplicate:
print the float value in integer in C language

I am trying out a rather simple code like this:

float a = 1.5;
printf("%d",a);

It prints out 0. However, for other values, like 1.4,1.21, etc, it is printing out a garbage value. Not only for 1.5, for 1.25, 1.5, 1.75, 1.3125 (in other words, decimal numbers which can be perfectly converted into binary form), it is printing 0. What is the reason behind this? I found a similar post here, and the first answer looks like an awesome answer, but I couldn’t discern it. Can any body explain why is this happening? What has endian-ness got to do with t?

  • 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-13T17:38:16+00:00Added an answer on June 13, 2026 at 5:38 pm

    you’re not casting the float, printf is just interpreting it as an integer which is why you’re getting seemingly garbage values.

    Edit:

    Check this example C code, which shows how a double is stored in memory:

    int main()
    {
        double a = 1.5;
        unsigned char *p = &a;
        int i;
    
        for (i=0; i<sizeof(double); i++) {
            printf("%.2x", *(p+i));
        }
        printf("\n");
        return 0;
    }
    

    If you run that with 1.5 it prints

    000000000000f83f
    

    If you try it with 1.41 it prints

    b81e85eb51b8f63f
    

    So when printf interprets 1.5 as an int, it prints zero because the 4 LSBs are zeros and some other value when trying with 1.41.

    That being said, it is an undefined behaviour and you should avoid it plus you won’t always get the same result it depends on the machine and how the arguments are passed.

    Note: the bytes are reversed because this is compiled on a little indian machine which means the least significant byte comes first.

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

Sidebar

Related Questions

Possible Duplicate: Float to binary in C++ I want to print out the binary
Possible Duplicate: nul terminating a int array I'm trying to print out all elements
Possible Duplicate: C++: Print out enum value as text Say I have an enumeration:
Possible Duplicate: C++: What is the printf() format spec for “float”? I am new
Possible Duplicate: How to print out the method name and line number and conditionally
Possible Duplicate: Why does printf print wrong values? #include<stdio.h> int main(int argc, const char
Possible Duplicate: TSQL How do you output PRINT in a user defined function? This
Possible Duplicate: How do I compare strings in Java? I'm sorry for this rather
Possible Duplicate: How do I fix this Perl code so that 1.1 + 2.2
Possible Duplicate: Dealing with accuracy problems in floating-point numbers Code: float flt = 78983636;

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.