Thread: Floating point in printf()

  1. #1
    Registered User
    Join Date
    Apr 2020
    Greater Philadelphia

    Floating point in printf()

    Maybe I'm missing something, but in the format field for printf() etc., I don't see a distinction between float variables and double variables. So how does the function know the size of the argument actually submitted? Does the compiler automatically promote float values to double where this function is concerned? Or is the programmer expected always to give it a double?
    Last edited by Alogon; 04-14-2020 at 12:06 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I believe these are the relevant parts of the C standard:
    Quote Originally Posted by C11 Clause Paragraphs 6a, 7
    If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions.

    If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type. The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.
    Quote Originally Posted by C11 Clause Paragraph 1
    #include <stdio.h>
    int fprintf(FILE * restrict stream,
        const char * restrict format, ...);
    So, we see that the arguments provided to correspond to the conversion specifiers are trailing arguments, so they have the default argument promotions, i.e., integer promotions and promotion from float to double, applied to them. Hence, if you pass a float where a double is expected, that's perfectly fine as the float will be converted to a double.

    Now, you might point out that this paragraph then creates problems for chars and shorts:
    Quote Originally Posted by C11 Clause Paragraph 9b
    If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
    However, if you check the relevant paragraphs for the relevant modifiers, you will see wording such as "the argument will have been promoted according to the integer promotions, but its value shall be converted to signed char or unsigned char before printing" that indicates that this nominal difference in type after promotion is not considered a violation of the rule against an incorrect type.
    Last edited by laserlight; 04-14-2020 at 12:30 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot write multiple printf 's after a floating point
    By arfanarf in forum C Programming
    Replies: 3
    Last Post: 02-25-2014, 11:49 AM
  2. floating point number printf/string formating
    By stanlvw in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2009, 04:41 PM
  3. Floating point Hex.
    By cstubbs50 in forum C Programming
    Replies: 4
    Last Post: 11-16-2005, 04:29 PM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

Tags for this Thread