1

I'm using gcc 4.8.1 with std=c99 on Windows 7. What's the correct place holder for long double when using scanf and fscanf? I tried %lf and %Lf and neither is working. Compile command is gcc -std=c99 main.c -o main.exe. When I omit std=c99 it works fine.

#include <stdlib.h>
#include <stdio.h>
int main()
{
    long double x = 0;
    scanf("%Lf", &x); 
    printf("x = %Lf\n", x); 
    return 0; 
}
Nathan Schmidt
  • 374
  • 1
  • 4
  • 16
  • 1
    **Edit your question** to show the exact source code and the compilation command. Remember to test the return -number of scanned items- from [scanf](http://pubs.opengroup.org/onlinepubs/009695399/functions/fscanf.html) why may fail – Basile Starynkevitch Jun 20 '15 at 06:03
  • 1
    Did you compile with `gcc -std=c99 -Wall -g` ? How did you run your program? Why don't you test the result of `scanf` (e.g. `if (scanf("%Lf", &x)<=0)` ....) – Basile Starynkevitch Jun 20 '15 at 06:11
  • compilation command: `gcc -std=c99 main.c -o main.exe`. When I don't use `std=c99` it works fine. – Nathan Schmidt Jun 20 '15 at 06:14
  • But you should test the result of `scanf` – Basile Starynkevitch Jun 20 '15 at 06:14
  • 1
    What do you mean it isn't working? What output are you getting? – Nathan Wilson Jun 20 '15 at 06:22
  • If you're using MinGW you are probably running into an incompatibility between how GCC and msvcrt.dll represent `long double`. See: http://stackoverflow.com/questions/7822928/scanf-not-taking-in-long-double and http://stackoverflow.com/a/7136886/12711 Some newer distributions of MinGW may have fixed formatted I/O incompatibilities with `long double` and `long long` (for example, by using custom version of the I/O functions implemented in `libmingwex.a`). An upgrade might be in order (maybe also consider mingw_w64). Or just use `double`. – Michael Burr Jun 20 '15 at 06:28
  • I'm getting whatever value I assign to `x` in the code. – Nathan Schmidt Jun 20 '15 at 06:30

1 Answers1

1

It works fine for me, on Centos 6.6, with or without -std=c99:

$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11)
Copyright (C) 2010 Free Software Foundation, Inc.

$ gcc x.c -o x -std=c99 -Wall -pedantic
$ ./x
123.456
x = 123.456000

$ gcc x.c -o x  -Wall -pedantic
$ ./x
123.456
x = 123.456000

I'm assuming you're on Linux (or Mac OSX), and I assume you're using the libc that matches the installed gcc.

HOWEVER

There are issues if you're using GCC with MinGW on Windows:

scanf not taking in long double

Dev-c++ uses MinGW, which uses the gcc compiler and the Microsoft runtime library. Unfortunately, those components disagree on the underlying type to be used for long double (64 vs. 80 or 96 bits, I think). Windows assumes long double is the same size as double; gcc makes long double bigger.

Either choice is valid, but the combination results in a broken C and C++ implementation.

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I'm on windows. I'll edit the question. – Nathan Schmidt Jun 20 '15 at 06:23
  • 1
    OK, then. Here are [possible solutions](http://mingw-users.1079350.n2.nabble.com/How-to-printf-long-doubles-td1650580.html): "mingwrt-3.15 onwards[*] provides a fully ANSI conformant printf(). However, it isn't used by default, unless you compile with `-ansi`, `-posix` or, as JonY suggests, `-D__USE_MINGW_ANSI_STDIO`. – paulsm4 Jun 20 '15 at 06:29
  • just tired all of those. None works :( – Nathan Schmidt Jun 20 '15 at 06:34
  • Please: 1) Verify you have mingwrt or higher (remember: mingw sits on top of GCC; they're distinct); 2) Consider trying `-D__USE_MINGW_ANSI_STDIO=1`, 3) Consider adding `#define printf __mingw_printf` – paulsm4 Jun 20 '15 at 06:46