Correct use of PRID64 in C + + (reprint)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/win_lin/article/details/7912693

  

Example reference high-performance streaming media server Srs:https://github.com/winlinvip/simple-rtmp-server

The PRID64 is widely used in SRS to achieve 32 and 64-bit systems common.


C + + uses PRID64, which takes two steps:

    1. Contains header files:<inttypes.h>
    2. Define macros: __stdc_format_macros, you can add-d__stdc_format_macros at compile time, or define this macro before you include a file.

The int64_t is used to represent a 64-bit integer, a long long int in a 32-bit system, and a long int in a 64-bit system, so the format method for printing int64_t is:

    printf ("%ld"//  64bit OS      printf ("%lld " // 32bit OS  

Of course there are cross-platform approaches:

#include <inttypes.h>printf ("%"PRId64"\ n", value); //equivalent to 64 bits:printf"%" "LD" "\ n", value); //or 32-bit:printf"%" "LLD" "\ n", value);

where printf ("abc" "DEF" "Ghi") writes multiple strings in such a way that there is no problem.

But, anyway compile, the error is: error:expected ') ' Before ' PRId64 '

Look for the definition of this macro,/usr/include/inttypes.h:

    /*The ISO C99 standard specifies, these macros must only be defined if explicitly requested. */      #if!defined __cplusplus | | Defined __stdc_format_macros          # if__wordsize = = -# define __pri64_prefix"L"# define __priptr_prefix"L"      # Else# define __pri64_prefix"ll"# define __priptr_prefix # endif/*Macros for printing format specifiers. */            /*Decimal notation. */# define PRID8"D"# define PRID16"D"# define PRID32"D"# define PRID64 __pri64_prefix"D"  

Originally this is defined for C use, C + + to use it, it is necessary to define a __stdc_format_macros macro display to open it.

    /*test_int64.cpp g++-d__stdc_format_macros-o test_int64-g-o0 test_int64.cpp*/#include<stdio.h>#include<inttypes.h>intMainintargcChar**argv) {int64_t value=0xFFFFFFFFFFFF; printf ("int64_t=%"PRId64", sizeof (int64_t) =%d\n", Value,sizeof(int64_t)); }  

Compile and execute:

g++-d__stdc_format_macros-o test_int64-g-o0 test_int64.cpp

./test_int64

int64_t=281474976710655, sizeof (int64_t) =8

For the new C + + standard-std=c++0x, you can also use a better way:

    /*test_int64_1.cpp g++-o test_int64_1-g-o0 test_int64_1.cpp*/#include<stdio.h>#include<cinttypes>using namespacestd; intMainintargcChar**argv) {int64_t value=0xFFFFFFFFFFFF; printf ("int64_t=%"PRId64", sizeof (int64_t) =%d\n", Value,sizeof(int64_t)); }  

Do not define the macro, compile and execute:

g++-o test_int64_1-g-o0 test_int64_1.cpp-std=c++0x

./test_int64_1

int64_t=281474976710655, sizeof (int64_t) =8

Of course, you have to specify a new parameter:-std=c++0x, otherwise it will error "#error this file requires compiler and library support for the upcoming ISO C + + standard, C + + 0x. This currently experimental, and must are enabled with the-std=c++0x or-std=gnu++0x compiler options. "

If you can compile with newer g++, you can use the latter, otherwise you can define macros directly with the former.

Correct use of PRID64 in C + + (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.