Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long double datatype problem in C

Tags:

c

In the code below, printf prints -0.00000. What is the problem? If it is double instead of long double, then it works fine.

#include<stdio.h>
long double abs1(long double x) {
    if (x<0.0)
        return -1.0*x;
    else
        return x;
}

main() {
    long double z=abs1(4.1);
    printf("%llf\n",z);
}
like image 982
avd Avatar asked Nov 25 '25 23:11

avd


1 Answers

The correct print format for a long double is %Lf. Turning on your compiler's warnings would have pointed out the mistake immediately:

$ gcc -Wall b.c -o b
b.c:9: warning: return type defaults to `int'
b.c: In function `main':
b.c:11: warning: use of `ll' length modifier with `f' type character
b.c:11: warning: use of `ll' length modifier with `f' type character
b.c:12: warning: control reaches end of non-void function
like image 151
Mark Rushakoff Avatar answered Nov 28 '25 16:11

Mark Rushakoff