Thread: PRId64 test code is it correct and proper?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4,183

    PRId64 test code is it correct and proper?

    I am having PRId64 warnings while building MinGW64 GCC under MSys2 for MinGW 64 bit. I just wish to verify my test code is correct in order to start determining the cause of the many warnings.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <inttypes.h>
    
    #define MAX_OUTPUT 24
    
    int main()
    {
        char test_output[MAX_OUTPUT+1];
        int64_t test_signed_64_bit=0;
    
    #ifdef __USE_MINGW_ANSI_STDIO
        printf("__USE_MINGW_ANSI_STDIO is as %d\n", __USE_MINGW_ANSI_STDIO);
    #else
        printf("%s\n", "__USE_MINGW_ANSI_STDIO is not defined");
    #endif
        printf("PRId64 is %s\n", PRId64);
    
        sscanf("1234567891011121314", "%" PRId64, &test_signed_64_bit);
        snprintf(test_output, MAX_OUTPUT, "%" PRId64, test_signed_64_bit);
        printf("%s\n", test_output);
        return 0;
    }
    build log
    Code:
    x86_64-w64-mingw32-gcc.exe -pedantic -Wextra -Wall -std=c99 -Wno-pedantic-ms-format  -c C:\Users\stahta01\devel\test\test_format\main.c -o obj\no_USE_MINGW_ANSI_STDIO\main.o
    x86_64-w64-mingw32-gcc.exe  -o bin\no_USE_MINGW_ANSI_STDIO\test_format.exe obj\no_USE_MINGW_ANSI_STDIO\main.o
    output
    Code:
    __USE_MINGW_ANSI_STDIO is as 1
    PRId64 is lld
    1234567891011121314
    I was slightly surprised that __USE_MINGW_ANSI_STDIO is defined as 1 by default.

    Tim S.
    Last edited by stahta01; 01-20-2021 at 02:29 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    Do you understand what is going on?

    There are at least two standard IO implementations on your system.
    When the MINNGW library is used, your macro is defined. It also happens to be defined to one.

    The printf() format string is very old, and has no way to specify 64 bit numbers directly.
    It can specify integers ("%d"), longs, "("%ld") or long longs ("%lld"). Any of these could
    be your 64 bit type - int is unlikely to be over 32 bits, but it is allowed.

    So the macro PRId64 specifies the format string. Because C strings can concatentate, you
    just insert the macro in its place, but outside the quotes.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Malcolm McLean View Post
    Do you understand what is going on?

    There are at least two standard IO implementations on your system.
    When the MINNGW library is used, your macro is defined. It also happens to be defined to one.

    The printf() format string is very old, and has no way to specify 64 bit numbers directly.
    It can specify integers ("%d"), longs, "("%ld") or long longs ("%lld"). Any of these could
    be your 64 bit type - int is unlikely to be over 32 bits, but it is allowed.

    So the macro PRId64 specifies the format string. Because C strings can concatentate, you
    just insert the macro in its place, but outside the quotes.
    Sounds like I know all of what you stated; and the macro appears to be always defined!
    Under C I can set it to zero; but, under C++ it requires it to be one for C++ libraries to work.

    There are at least two standard IO implementations on your system.
    Not really sure that this is true in all ways; but, it could be true.
    I think it could have just one IO implementation that supports both sets format strings.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-15-2019, 09:47 AM
  2. Replies: 30
    Last Post: 12-11-2017, 12:26 PM
  3. help to correct the code
    By Java in forum C Programming
    Replies: 3
    Last Post: 02-22-2015, 09:23 AM
  4. Can anybody help me correct this C++ code?
    By Amanda2 in forum C++ Programming
    Replies: 9
    Last Post: 10-04-2007, 04:46 AM
  5. Help writing code (Can't think of a proper title)
    By markcls in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2007, 09:51 AM

Tags for this Thread