Formatting output with printf

This is a discussion on Formatting output with printf within the C Programming forums, part of the General Programming Boards category; Hi all, If I create a variable as a float type and then print this number out to the screen ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    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, 04:17 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Printf Output Confusion
    By simpleid in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2006, 10: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: 03-31-2003, 11:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21