Is the C part of the C++ library automatically C99? - c++

Are all the functions in a conformant C++98/03/0x implementation completely C99 conformant?
I thought C++0x added some C99 (language) features, but never heard or read anything definitive about the C library functions.
Just to avoid any confusion, I'm talking about a C++ program using functions declared in the <c*> header set.
Thanks.

Most of the C99 standard library has been imported in C++0X but not all. From memory, in what wasn't imported there are
<ctgmath> simply includes <ccomplex> and <cmath>,
<ccomplex> behaves as if it included <complex>
<cmath> has quite a few adjustment (providing overload and template functions completing the C99 provided one)
Some other headers (<cstdbool>, <iso646.h>, ...) have adjustments to take differences between language into account (bool is primitive in C++, a macro provided by <stdbool.h> in C for instance), but nothing of the scope of the math part.
The headers <xxx.h> whose <cxx> form doesn't behaves as the C99 version simply declares the content of <cxxx> in the global namespace, they aren't nearer of the C99 <xxx.h> content.
A related thing: C++0X provides some headers in both cxxx and xxx.h forms which aren't defined in C99 (<cstdalign> and <cuchar>, the second one is defined in a C TR)
(I remembered that a bunch of mathematical functions from C99 had been put in TR1 but not kept in C++0X, I was mistaken, that bunch of mathematical functions weren't part of C99 in the first place).

No. C++03 is aligned with ANSI C89/ISO C90, not C99.
The upcoming C++0x standard is expected to be aligned to some degree with C99. See paragraph 17.6.1.2 in the current draft which lists ccomplex, cinttypes, cstdint etc. Note that, as AProgrammer mentions, some headers aren't exactly the same; further, that the header cuchar is aligned with the C Technical Report 19769 rather than C99.

Related

Is memcpy(dest, src, 0) defined in the C++ standard?

This question and many more are among the numerous questions on this site that ask if a call to memcpy() is valid with a length\size specified with a zero value.
When answering, everyone quotes the current C standard (in this case C17 ISO/IEC9899:2017 page 283 ),
Where an argument declared as size_t n specifies the length of the array for a function, n can have
the value zero on a call to that function. Unless explicitly stated otherwise in the description of a
particular function in this subclause, pointer arguments on such a call shall still have valid values, as
described in 7.1.4. On such a call, a function that locates a character finds no occurrence, a function
that compares two character sequences returns zero, and a function that copies characters copies
zero characters.
However, this is from C standard, not quoted from the C++ standard.
Where in the current C++ standard (i.e. C++17 ISO/IEC 14882) is this same definition listed? C and C++ have two different standards (and languages) and from my understanding, you cannot quote one and expect that same rule/behavior to be present in the other standard.
If this quotation from the C standard is valid in C++ without explicitly stating it in the standard, can someone then provide a source that backs up this connection between C and C++?
The C++17 standards says this about the C standard library:
1
The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.
2
The descriptions of many library functions rely on the C standard library for the semantics of those functions. In some cases, the signatures specified in this International Standard may be different from the signatures in the C standard library, and additional overloads may be declared in this International Standard, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict qualifier) are the same unless otherwise stated.
As to your question,
If this quotation from the C standard is valid in C++ without explicitly stating it in the standard
The answer is "yes".
[library.c]/2, emphasis mine:
The descriptions of many library functions rely on the C standard library for the semantics of those functions.
In some cases, the signatures specified in this document may be different from the signatures in the C standard library, and additional overloads may be declared in this document, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict qualifier) are the same unless otherwise stated.
[cstring.syn]/1:
The contents and meaning of the header <cstring> are the same as the C standard library header <string.h>.
The C library is sort of baked into C++. As said in [intro.scope] from the C++17 standard (emphasis mine):
C++ is a general purpose programming language based on the C programming language as described in
ISO/IEC 9899:2011 Programming languages — C (hereinafter referred to as the C standard). In addition to
the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces,
operator overloading, function name overloading, references, free store management operators, and additional
library facilities.
And again, in [intro.refs]:
The library described in Clause 7 of ISO/IEC 9899:2011 is hereinafter called the C standard library.1
1) With the qualifications noted in Clauses 21 through 33 and in C.5, the C standard library is a subset of the C++ standard
library.
So everything that is in the C standard library is in C++.

Why does the C++ standard not mention __STDC_IEC_559__?

According to C++11 standard [c.math], the <cmath> header is same as Standard C library header <math.h>.
(Of course, there are several differences, --- namespace, overloads etc. --- but these can be ignored here.)
And according to C99 standard annex F, "An implementation that defines __STDC_IEC_559__ shall conform to the specifications in" the annex F.
Ex. The atan2 may cause a domain error if both arguments are zero, but It must not if __STDC_IEC_559__ is defined.
In C99, many behavior is also dependent on whether __STDC_IEC_559__ is defined or not.
However, it seems that __STDC_IEC_559__ is not mentioned anywhere in C++11 standard.
If so, shall a C++ implementation conform to the specifications in the annex F?
I think that std::numeric_limits<T>::is_iec559() is a substitute, but it seems to mention about only type.
The C++ standard (n3797) includes the C standard library by reference, see s1.2/2.
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.
With the qualifications noted in Clauses 18 through 30 and in C.4, the C standard library is a subset of the C++ standard
library.
The standard contains no mention of that symbol, and I would not expect it be defined, since it appears to be specific to Standard C. By not defining that symbol, C++ is not bound by the contents of Annex F.
Instead the C++ standard contains multiple mentions of IEC 559 in a rather more C++-like form. For example,
Shall be true for all specializations in which is_iec559 != false
There is a specific mention in 18.3.2.4/56.
static constexpr bool is_iec559;
True if and only if the type adheres to IEC 559 standard.218
Meaningful for all floating point types.
I think it would be fair to say that C++ includes all the same capabilities (or lack of them), but adapted to the C++ world.

What header defines NULL in C++?

According to C++ primer, <cstdlib> header defines NULL. cpluspplus says it is defined in <cstddef>.
Ultimately, if the right header is not included, I thought NULL can't be referenced.
From what i can see, however it can be referenced and produce programs and that compile and run without warnings or errors, after including only <iostream>
Please help me understand this.
The C standard requires that NULL be defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.
The C++ standard requires that NULL be defined in the c* header corresponding to each of those.
The C standard is very strict about the names a standard can define--each standard header must define precisely the names the standard requires that header to define. The only other names it can define are those that are reserved for the implementation, such as those starting with an underscore followed by another underscore or a capital letter.
The C++ standard is much more permissive in this respect--including any one standard header can have the same effect as including any or all other standard headers.
From a practical viewpoint, C++ implementations used to take quite a bit of advantage of this permissiveness--that is, including one standard header frequently defined the names from a number of other standard headers. More recent implementations tend to work more like the C standard requires, staying much closer to each header defining only the names required by to be defined by that header. They're still probably not as strict about it as the C standard requires, but much closer than they used to be (as a rule).
C++03 section 18.1.2 says that NULL is defined in cstddef.
On some implementations, iostream may include cstddef, so including iostream would also give you NULL.
It is defined in <cstddef>
The C++11 standard section 18.2, table 30 explains what's in <cstddef> It says:
Table 30 — Header <cstddef> synopsis
Macros: NULL [...]
[...] The macro NULL is an implementation-defined C++ null pointer constant in this International Standard
The C++11 Standard says NULL must be defined in multiple files. They are:
<clocale>
<cstddef>
<cstdlib>
<cstring>
<ctime>
<cwchar>
This is mentioned in Table 149, Section C.3 C standard library/3 of the standard.
Here's an image of the table and some surrounding text.
In the latest version of MinGW (MinGW 5.3.0) NULL was defined in the wchar.h header as :
#define NULL 0

putc implemented as Macro ic C++?

I know Macro implementation of putc() in C, but is it same in C++?
It will depend on your implementation of cstdio. In most cases this is really just a wrapper around stdio.h, with wrappers declared inside the std namespace, and the C and C++ compilers share the same standard library for C functions. For example, VS2010 uses stdio.h for C++, in which putc is implemented as both a macro and a function, depending on environment and other compile-time definitions.
Which version of C++? C++83 (1983)? C++98 (1998)? C++11 (2011)?
The C++98 and C++11 Specifications rely on the ISO C specifications for C Library functions, and do not put additional implementation constraints on them, other than trivial ones like renaming stdio.h to cstdio.h and allowing inclusion without the dot-h suffix.
See: C++98 Specification
See: C++11 Specification
Look in cstdio.h if you are interested in your particular compiler.
However, if we dig deeper and take a look at the ISO C standard: "ISO/IEC 9899:1990" (C89/C90), well, we find that it is unavailable for free viewing on the web (not even the final draft standard), so moving on to C99 (NOT ISO C), you find...
...that C99 (Not "ISO C") says putc() MAY be implemented as a macro,
See: C99 Specification
So if you are really developing in Obj-C++ (which uses C99), then C99 is the relevant specification to consider, not ISO C (C90). Also, since C99 lets the compiler writer decide whether to make putc() a macro or not, you should consider it an open possibility, and decide whether you really care to know about the C90 (ISO C) spec which is becoming obsolete (now that even C11 (2011) is out.)
Yes it is. Both C and C++ use <stdio.h> which has the same scheme in all implementations that I know of.

Is a C++ preprocessor identical to a C preprocessor?

I am wondering how different the preprocessors for C++ and C are.
The reason for the question is this question on a preprocessor-specific question where the paragraph of the standard that addresses the question has a different wording (and a different paragraph number) and also are difference concerning the true and false keywords in C++.
So, are there more differences or is this the only difference.
An extension of the question would be when is a source file emitted differently by a C++ preprocessor and a C preprocessor.
The C++03 preprocessor is (at least intended to be) similar to the C preprocessor before C99. Although the wording and paragraph numbers are slightly different, the only technical differences I'm aware of between the two are that the C++ preprocessor handles digraphs (two-letter alternative tokens) and universal character names, which are not present in C.
As of C99, the C preprocessor added some new capabilities (e.g., variadic macros) that do not exist in the current version of C++. I don't remember for sure, but don't believe that digraphs were added.
I believe C++0x will bring the two in line again (at least that's the intent). Again, the paragraph numbers and wording won't be identical, but I believe the intent is that they should work the same (other than retaining the differences mentioned above).
They are supposed to be the same: C++98 and C++03 should match C90, and C++0x should match C99. There may be bugs in the wording, though.
Predefined macros differ between the preprocessors, mostly for obvious language feature differences. E.g. compare:
C99 N1256 draft 6.10.8 "Predefined macro names"
C++11 N3337 draft 16.8 "Predefined macro names"
In particular:
C requires you not to define __cplusplus, C++ uses it to represent the version
C uses __STDC__ to represent the version, C++ says is implementation defined and uses __cplusplus instead
C has __STDC_IEC_559__ and __STDC_IEC_559_COMPLEX__ to indicate floating point characteristics, C++ does not and seems replace that with the per type std::numeric_limits<float>::is_iec559 constants
C does not have the macros prefixed with __STDCPP: _STDCPP_STRICT_POINTER_SAFETY__ and __STDCPP_THREADS__
As mentioned by DevSolar, C11 added many more defines which are not part of C++11.