When use standard C function in C++, should we prefix every function with std::
?
for example (file name: std.C
):
#include <cstdio>
int main() {
std::printf("hello\n");
printf("hello\n");
}
This file can be compiled with:
g++ -Wall -Werror -std=c++11 std.C
without any error.
my questions are:
std::
before all the standard C library functions when they are used in C++?<stdio.h>
and <cstdio>
?Explanation: It is known that “std” (abbreviation for the standard) is a namespace whose members are used in the program. So the members of the “std” namespace are cout, cin, endl, etc. This namespace is present in the iostream. h header file.
All C++ standard library names, including the C library names, if you include them, are defined in the namespace std . This means that you must qualify all the library names using one of the following methods: Specify the standard namespace, for example: std::printf("example\n");
std::source_location is a class first introduced in C++20 that represents certain information about the source code, such as file names, line numbers, and function names.
What is the C programming language standard? It is the standard way defined for the compiler creators about the compilation of the code. The latest C standard was released in June 2018 which is ISO/IEC 9899:2018 also known as the C11.
The C++ library includes the same definitions as the C language library organized in the same structure of header files, with the following differences:
- Each header file has the same name as the C language version but with a "
c
" prefix and no extension. For example, the C++ equivalent for the C language header file<stdlib.h>
is<cstdlib>
.- Every element of the library is defined within the
std
namespace.Nevertheless, for compatibility with C, the traditional header names
name.h
(likestdlib.h
) are also provided with the same definitions within the global namespace although its use is deprecated in C++.
(source)
The std::
part of the std::printf()
call is the standard way to use names in the standard library, therefore, I suggest to use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With