Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C standard library
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== C++ === The [[C++]] language incorporates the majority of the C standard library’s constructs into its own, excluding C-specific machinery. C standard library functions are exported from the C++ standard library in two ways. For backwards-/cross-compatibility to C and pre-Standard C++, functions can be accessed in the global [[namespace (programming)|namespace]] ({{mono|::}}), after {{code|#include|lang=cpp}}ing the C standard header name as in C.<ref>{{citation |url=https://en.cppreference.com/w/cpp/header#C_compatibility_headers |access-date=9 October 2024 |title=C++ Standard Library Headers—C compatibility headers}}</ref>{{^|This should probably be interwiki}} Thus, the C++98 program <syntaxhighlight lang="cpp">#include <stdio.h> int main() { return ::puts("Hello, world!") == EOF; }</syntaxhighlight> should exhibit (apparently-)identical behavior to [[ANSI_C#C95|C95]] program <syntaxhighlight lang="c">#include <stdio.h> int main(void) { return puts("Hello, world!") == EOF; }</syntaxhighlight> From [[C++98]] on, C functions are also made available in namespace {{mono|::std}} (e.g., C {{mono|printf}} as C++ {{mono|::std::printf}}, {{mono|atoi}} as {{mono|::std::atoi}}, {{mono|feof}} as {{mono|::std::feof}}), by including header <code lang="cpp"><c{{varserif|hdrname}}></code> instead of corresponding C header <code lang="cpp"><{{varserif|hdrname}}.h></code>. E.g., {{mono|<cstdio>}} substitutes for {{mono|<stdio.h>}} and {{mono|<cmath>}} for {{mono|<math.h>}}; note lack of ''{{mono|.h}}'' extension on C++ header names. Thus, an equivalent (generally preferable) C++≥98 program to the above two is: <syntaxhighlight lang="cpp" >#include <cstdio> int main() { return std::puts("Hello, world") == EOF; }</syntaxhighlight> A {{code|using namespace ::std|lang=cpp}} declaration above or within {{mono|main}} can be issued to apply the {{mono|::std::}} prefix automatically, although it’s generally considered poor practice to use it globally in headers because it pollutes the global namespace.<ref>{{cite web |url=https://websites.umich.edu/~eecs381/handouts/NamespaceGuide.pdf#page=2 |first=David |last=Kieras |title=Using "using": How to use the std namespace |publisher=EECS Department, University of Michigan |website=EECS381 Handouts |access-date=9 October 2024 |date=15 February 2015 |format=PDF |url-status=live |archive-url=https://web.archive.org/web/20221224070046/https://websites.umich.edu/~eecs381/handouts/NamespaceGuide.pdf#page=2 |archive-date=24 December 2022 |quote=A single <code>using namespace std;</code> statement in a single header file in a complex project can make a mess out of the namespace management for the whole project. <em>So, no top level [<nowiki /><code>using namespace</code><nowiki />] statements in a header file!</em>}}</ref> A few of the C++≥98 versions of C’s headers are missing; e.g., C≥11 {{mono|<stdnoreturn.h>}} and {{mono|<threads.h>}} have no C++ counterparts.<ref>{{cite web |url=https://en.cppreference.com/w/cpp/header#Unsupported_C_headers |title=C++ Standard Library headers—Unsupported C headers |access-date=9 October 2024}}</ref> Others are reduced to placeholders, such as (until [[C++20]]) {{mono|<ciso646>}} for C95 {{mono|<iso646.h>}}, all of whose requisite macros are rendered as keywords in C++98. C-specific syntactic constructs aren’t generally supported, even if their header is.<ref>{{cite web |url=https://en.cppreference.com/w/cpp/header#Meaningless_C_headers |title=C++ Standard Library headers—Meaningless C headers |access-date=9 October 2024}}</ref> Several C headers exist primarily for C++ compatibility, and these tend to be near-empty in C++. For example, [[C99]]–[[C17 (C standard revision)|17]] {{mono|<stdbool.h>}} require only <syntaxhighlight lang="c" highlight="4" class="nowrap" >#define bool _Bool #define false 0 #define true 1 #define __bool_true_false_are_defined 1</syntaxhighlight> in order to feign support for the C++98 {{mono|bool}}, {{mono|false}}, and {{mono|true}} keywords in C. [[C++11]] requires {{mono|<stdbool.h>}} and {{mono|<cstdbool>}} for compatibility, but all they need to define is {{mono|__bool_true_false_are_defined}}. [[C23 (C standard revision)|C23]] obsoletes older {{mono|_Bool}} keyword in favor of new, C++98-equivalent {{mono|bool}}, {{mono|false}}, and {{mono|true}} keywords, so the C≥23 and C++≥11 {{mono|<stdbool.h>}}/{{mono|<cstdbool>}} headers are fully equivalent. (In particular, C23 doesn’t require any {{mono|__STDC_VERSION_BOOL_H__}} macro for {{mono|<stdbool.h>}}.) Access to C library functions via namespace {{mono|::std}} and the C++≥98 header names is preferred where possible. To encourage adoption, [[C++98]] obsoletes the C (''{{mono|*.h}}'') header names, so it’s possible that use of C compatibility headers will cause an especially strict C++98–[[C++20|20]] preprocessor to raise a diagnostic of some sort. However, [[C++23]] (unusually) <em>de-</em>obsoletes these headers, so newer C++ implementations/modes shouldn’t complain without being asked to specifically.<ref>{{cite web |url=https://en.cppreference.com/w/cpp/header#C_compatibility_headers |title=C++ Standard Library headers—C compatibility headers |access-date=9 October 2024}}</ref> {{^|These↓↓ should probably be expanded and given their own subsections alongside C++ and Python. At the very least, citation needed!}} Other languages take a similar approach, placing C compatibility functions/routines under a common namespace; these include [[D (programming language)|D]], [[Perl]], and [[Ruby (programming language)|Ruby]]. {{^|PHP is another prominent example, and some basics like `printf`, `puts`, `toupper`, and `sqrt` show up in other Unixy languages such as POSIX shell script and Awk. Probably a trailing §(#Other languages) would be best.}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)