Stdio.H Not Standard in C++

stdio.h not standard in C++?

stdio.h is standard, but deprecated. Always prefer cstdio in C++.

[n3290: C.3.1/1]: For compatibility with the Standard C library, the
C++ standard library provides the 18 C headers (D.5), but their use is
deprecated in C++.

[n3290: D.5/3]: [ Example: The header <cstdlib> assuredly
provides its declarations and definitions within the namespace std. It
may also provide these names within the global namespace. The header
<stdlib.h> assuredly provides the same declarations and definitions
within the global namespace, much as in the C Standard. It may also
provide these names within the namespace std.
—end example ]

Prevent inclusion of stdio.h (or other standard header)

You can redefine printf to be some nasty value that will cause a compilation or linking error. For example:

#define printf do_not_include_stdio_h
#include <stdio.h>

int main(void) {
printf("Hello, world!\n");
return 0;
}

produces the output:

undefined reference to `do_not_include_stdio_h'

You can munge the macro if you want it to be an even more obscure name or include invalid symbols if you're worried that some poor soul will have defined do_not_include_stdio_h.

You can set the macro definition in the compiler flags so you don't have to manually edit the file(s). For example:

gcc -Dprintf=do_not_include_stdio_h my_file.c

Is stdio.h a library?

No, stdio.h is not a library, it's a header file. A common mistake when approaching C is to call every header file a library, that's just wrong.

The C standard library is a collection of functions, which are declared in header files, and stdio.h is one of them. The name stands for "Standard Input Output", so in that file you can find all the declarations of functions that deal with input, output and files. You can find a list of the header files included in the C standard library here.

A library is a compiled binary file (or in general, a collection of binary files), which can be linked when compiling a program to take advantage of the functions made available by the library (i.e. exported). Header files are then used to identify the names and the signatures of those functions, so that the compiler knows how to call them.

Commonly, for small libraries, a single header file is enough, so that's easy for beginners to confuse an header file with the library itself. The C standard library however is very complex and has a lot of functions, so they are declared in different header files.

C programs start with #include <stdio.h> because the C language does not include file manipulation functions.

Yes, that's right. The C specification only concerns the language itself (syntax, types, etc), and does not define any "standard" function.

Fatal error 'stdio.h' not found

Assuming C

<stdio.h> is one of the standard C headers. Your compiler complains that it can not find this header. This means that your standard library is broken.

Consider reinstalling your compiler.

Assuming C++

<stdio.h> is the C standard header, with C++ we use <cstdio> instead. Though <stdio.h> is still required to exist in C++, so this probably isn't the problem.


Apart from these assumptions, it seems most likely (by your coding style and tags) that you are using C. Try this as some example code. This is guaranteed (by me) to compile on a working C compiler, if it doesn't then your compiler is horribly broken and you must install another one/reinstall:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
printf("Hello World!\n");
return EXIT_SUCCESS;
}

Can I make a custom header file named stdio.h ?

C11 7.1.2p3:

If a file with the same name as one of the above < and > delimited sequences, not provided as part of the implementation, is placed in any of the standard places that are searched for included source files, the behavior is undefined.

The standard places then refers to 6.10.2p2:

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

So the only guarantee given here is that if your stdio.h is not in an implementation-defined place for a searching a header, then the behaviour wouldn't be undefined. You can then include it with

#include "stdio.h"

However if you really intended that the file would be included with

#include <stdio.h>

then for the compiler to find it you need to place it in any of the standard places, and all bets are off.


However, in a freestanding - i.e. not hosted - execution environment. stdio.h might not be a standard header name so it might as well be perfectly OK there. Not that there is anything portable about the freestanding execution environment.

Thus, unless you specify more specifically what you're up to, we can only refer to the C standard and shrug. Having a source file named stdio.h isn't strictly conforming but it very much might be conforming, so YMMV.



Related Topics



Leave a reply



Submit