5

I compiled the following c program with gcc -ansi -pedantic -Wall test.c:

#include <stdio.h>
#include <stdint.h>
#define  BUFFER 21
int main(int argc, char* argv[]) {
  uint64_t num = 0x1337C0DE;
  char str[BUFFER]; /* Safely Holds UINT64_MAX */
  if(argc > 1)
    sscanf(argv[1],"%llu",&num);
  sprintf(str,"%llu",num);
  return 0;
}

And I receive the following warnings:

test.c:8:5: warning: ISO C90 does not support the ‘ll’ gnu_scanf length modifier
test.c:9:3: warning: ISO C90 does not support the ‘ll’ gnu_printf length modifier

What is the correct, C90 standards compliant, way to a parse/print 64 bit integer from/to a string,
which doesn't generate these warnings?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78

2 Answers2

7

There is none. The largest integer type in C 90 is long, which is only guaranteed to be at least 32 bits. Since there's no integer type guaranteed to be at least 64 bits, there's also no way to read a 64-bit integer in C90 either. Of course, long could be 64 bits (it has been in at least one implementation, but there's no certainty that it is.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    Then, from my understanding, I should make the code at minimum `C99` standards compliant if I need to use 64 bit values? – recursion.ninja Jul 04 '13 at 04:42
  • 1
    @awashburn: Yes -- C99 was the first to provide a type that was guaranteed to be at least 64 bits. – Jerry Coffin Jul 04 '13 at 04:43
  • 2
    @awashburn and with C99 you get the correct macros to handle `uint64_t` in `printf` and `scanf` for free. Since you tag your question with gcc, there is really no point in not using C99 these days, unless you have badly written hereditary code. – Jens Gustedt Jul 04 '13 at 07:54
-1

you can use macros SCNu64 and PRIu64 in <inttypes.h>, for your example, they can be

sscanf(argv[1],"%" SCNu64,&num);
sprintf(str,"%" PRIu64,num);
ersteLicht
  • 44
  • 1
  • 3