Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C90 doesn't allow %lf use in printf, why?

Tags:

c

I'm a beginner programming student, just wanted to learn the reason behind this.

When I use this code:

#include <stdio.h>
int main()
{
 double pi = 3.1415926535897932;
 printf("%lf",pi);
 return 0;
}

Compiler gives this warning. ISO C90 does not support the ‘%lf’ gnu_printf format [-Wformat]

I use the gcc compiler in ubuntu terminal with (-o -Wall -ansi -pedantic-errors)

What's the reason behind this? I searched web and found this use is allowed in C99. Why C90 didn't allow %lf use in printf? I can use %.16lf or %.16f and both print with the same precision, so what's the matter that makes %lf bad in C90?

like image 322
epokhe Avatar asked Mar 25 '13 12:03

epokhe


People also ask

What is LLU C?

%lli or %lld. Long long. %llu. Unsigned long long.

What is a conversion format specifier?

Type conversion specifier. The type conversion specifier character specifies whether to interpret the corresponding argument as a character, a string, a pointer, an integer, or a floating-point number. The type character is the only required conversion specification field, and it appears after any optional fields.

What is PRIu64?

PRIu64 is a string (literal), for example the following: printf("%s\n", PRIu64); prints llu on my machine. Adjacent string literals are concatenated, from section 6.4.


2 Answers

According to C90 documentation:

an optional l (ell) specifying that a following d , i , o , u , x , or X conversion specifier applies to a long int or unsigned long int argument; an optional l specifying that a following n conversion specifier applies to a pointer to a long int argument; or an optional L specifying that a following e , E , f , g , or G conversion specifier applies to a long double argument. If an h , l , or L appears with any other conversion specifier, the behavior is undefined.

like image 148
Nima G Avatar answered Sep 28 '22 08:09

Nima G


C is an evolving language. New features and behaviors get added in every new release of the C standard.

C89 says that l before f leads to undefined behavior. And C90 probably says the same.

C99 on the other hand says that l before f has no effect.

like image 33
Alexey Frunze Avatar answered Sep 28 '22 10:09

Alexey Frunze