Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Support for Length Specifiers

In cplusplus.com reference for printf I see a specification for a "length" formatting, but it includes the note:

Yellow rows indicate specifiers and sub-specifiers introduced by C99. See <cinttypes> for the specifiers for extended types.

My question is specifically on the hh length formatting. And it's a "yellow" row. Formatting with hh behaves as expected in Visual Studio, but I'm wondering whether that's because Visual Studio is also C compiler or because hh actually supported by C++?

like image 760
Jonathan Mee Avatar asked Aug 14 '15 11:08

Jonathan Mee


2 Answers

The C++ standard used C90 as a normative reference until C++11 and so C99 features would only be supported in C++11. Although a compiler would be free to support them outside of C++11 as an extension. I would suspect this would only work with more recent versions of Visual Studio given their relatively recent drive to support C99 and cremno indicates that it is supported since 2015.

If we go to the C++11 draft standard section 1.2 Normative references [intro.refs] it says:

The following referenced documents are indispensable for the application of this document. For dated references, only the edition cited applies. For undated references, the latest edition of the referenced document (including any amendments) applies.

and includes:

  • ISO/IEC 9899:1999, Programming languages — C

and also says:

The library described in Clause 7 of ISO/IEC 9899:1999 and Clause 7 of ISO/IEC 9899:1999/Cor.1:2001 and Clause 7 of ISO/IEC 9899:1999/Cor.2:2003 is hereinafter called the C standard library.1

prior to C++11 this was:

  • ISO/IEC 9899:1990, Programming languages - C

and if we try an example in gcc using -std=c++03 -pedantic it warns:

warning: ISO C++98 does not support the 'hh' gnu_printf length modifier [-Wformat=]

like image 150
Shafik Yaghmour Avatar answered Oct 01 '22 18:10

Shafik Yaghmour


If you keep reading down the page

Those listed here are supported by the latest C and C++ standards (both published in 2011), but those in yellow were introduced in C99 (only required for C++ implementations since C++11)

So if you have Visual Studio 2013 or later, you will have access to (most) C++11 features.

like image 32
Cory Kramer Avatar answered Oct 01 '22 18:10

Cory Kramer