Why a long double variable shows minus zero?

Debojit Acharjee 455 Reputation points
2023-05-29T05:53:23.2433333+00:00

When I initialize a long double variable with '1' in the following program, I get the output as -0.000000. Why it showing minus sign before zero? because that's mathematically not correct.

#include <stdio.h>

int main()
{
    long double a;
    
    a = 1;
    
    printf("%Lf", a);

    return 0;
}

Output:

-0.000000

Developer technologies C++
Developer technologies Visual Studio Other
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2023-05-29T07:11:34.9633333+00:00

    Hi Debojit Acharjee,

    Normally this doesn't happen, please tell me what compiler you are using? What is an operating system? Here's what I get in VS2022:

    User's image

    Assigns the integer constant 1 to the long double variable a and prints it using the %Lf format string. According to the C language standard, the integer constant 1 is implicitly converted to type long double in this case. Regarding the issue with the output being -0.000000 , this may be due to the default printing precision for long double types.

    You can specify a higher printing precision. I suggest you use %.0Lf to print an integer value and make sure no fractional part is displayed.

    printf("%.0Lf", a);

    User's image

    If you want the decimal part to appear in the output, I suggest you use the %.6Lf format string, where .6 means set the printing precision to 6 decimal places.

    You could also try printf("%Le", a); to output it using scientific notation.

    Best regards,

    Elya Yao


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-06-02T17:14:45.3966667+00:00

    you must have a compiler issue

    on a mac with xcode, I get 1.000000

    with the online c compiler : https://www.onlinegdb.com/online_c_compiler

    I get the proper 1.000000


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.