Thread: Formatting output with printf

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    Formatting output with printf

    Hi all,

    If I create a variable as a float type and then print this number out to the screen with anything other than a %f format, such as %d, I get a *seemingly* random number.

    Is C doing some calculation against my initial float type to try and turn it into a %d output?

    Example:

    Code:
    int     main()
    {
        float       data;
        printf("Number: ",stdout);
        scanf("%f", &data);
        printf("Output in decimal format: %d", data);
        return EX_OK;
    }
    The code above results in an input of 10 displaying as "1076101120". I understand that I need to use %f, but would like to know how the printf comes to output the long string of "1076101120"

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    printf merely grabs the bits of whatever you might have passed in and interprets the data in whatever format you have specified. The bits in the float just happens to be the number you see when interpreted as an integer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Printf Output Confusion
    By simpleid in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2006, 11:36 AM
  4. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  5. Replies: 4
    Last Post: 04-01-2003, 12:49 AM