Thread: a question about long double format

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    2

    a question about long double format

    hello all
    im wondering, when i use this code in windows over codeblocks:

    long double ld=6;

    scanf("%Lf",&ld);

    printf("%f",ld); //// see, %f

    the entered value returns correctly. when i use incorrect format, i must use %Lf in printf.

    when i do not use the scanf function, i must use %Lf format.
    any one knows what is happening?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For scanf to read a long double or for printf to print a long double, %Lf is correct.

    The standard states for printf that "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined." That is, you must always use %Lf (or equivalents) to print long double with printf. If you use %f and it works as expected, then you're just lucky (or unlucky, as the case may be).
    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. Replies: 6
    Last Post: 03-05-2018, 09:59 AM
  2. Replies: 16
    Last Post: 06-07-2015, 03:28 PM
  3. number bigger than long long double
    By suryak in forum C Programming
    Replies: 9
    Last Post: 08-18-2011, 02:02 PM
  4. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  5. format specifier for long long int
    By rakeshkool27 in forum C Programming
    Replies: 9
    Last Post: 08-18-2010, 10:27 PM

Tags for this Thread