Nakul Sharma
Nakul Sharma

Reputation: 534

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

Can I make a custom header file named stdio.h and can use it in my program along with <stdio.h>?

Upvotes: 1

Views: 1097

Answers (4)

Tuğberk Kaan Duman
Tuğberk Kaan Duman

Reputation: 950

I've created a file named as stdio.h and main.c afterwards added this content to main.c to test out if it works properly.

#include "stdio.h"
#include <stdio.h>

int main()
{
    testArea(); // this will use "stdio.h"
    int test = 2;
    printf("%d", test); // this will use <stdio.h>
    return 0;
}

Program output:

1002 (100 from "stdio.h" and 2 from <stdio.h>)

Content of my stdio.h:

#include <stdio.h>

void testArea()
{
    int abc = 100;
    printf("%d", abc);
}

Surprise surprise, it does! Now, you should know that using "" means searching header file via file path. Meanwhile <> basically looks at the includes folder of the compiler.

So that, you can do that.

PS: If you're going to downvote, make an explanation so that I can learn what's wrong too. It just works fine for me.

Edit: Now, you can see that program knows what to call and what to do.

Bonus: If you try to add a same name function that exists in the file compiler will give you an error.

#include <stdio.h>

void testArea()
{
    int abc = 100;
    printf("%d", abc);
}

void printf()
{

}

Will return stdio.h:9:7: error: conflicting types for ‘printf’ as an example. You just should avoid using existing functions, but it's not restricted to same name header files. You just can't do it with any filename since the names are going to conflict.

Again, the usage of this is fine. There should be no problems at all.

Edit 2: I'm using Linux Mint 18.2.

Here is my gcc --version

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

And, here is how I compile my code:

duman@duman-pc ~/Desktop/Nuclear Test Zone $ gcc main.c -o test
duman@duman-pc ~/Desktop/Nuclear Test Zone $ ./test

Nothing fancy, and I don't really know what can cause undefined behaviour since my compiler won't allow usage of same name functions.

Edit 3: Just to see if anything related to name usage throws a warning I compiled the same code with ALL of these flags: https://stackoverflow.com/a/34971392/7735711 and there were none about the names.

That's the best I can do. Hopefully it helps you.

Upvotes: 0

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.

Upvotes: 4

Stephen P
Stephen P

Reputation: 195

If you specify where in your file directory your custom "stdio.h" comes from (i.e. doing

#include "C:/ProgrammingC/stdio.h"

is probably fine, but

#include "stdio.h" //This only selects the standard include
                   //if there's no other stdio.h in the build directory

is risky, and

#include <stdio.h>

is definitely not what you want.

Upvotes: 0

chqrlie
chqrlie

Reputation: 144989

As noted by Antti Haapala, it is explicitly described as undefined behavior to name a file stdio.h and put it in any of the directories where the compiler looks for include files.

Yet, by default, the compiler does not search for standard headers in the directory of the source file, but a command line argument of -I. can easily change this behavior.

Without this option and assuming you do not put your source files in the compiler's system directories, you could use the name stdio.h for an include file and include that with #include "stdio.h" without interfering with the standard header referred to in #include <stdio.h> (which might not even be a file at all).

You could go one step further into confusion-land by naming the source file itself stdio.h...

I you truly want to confuse the reader, name the source file a.out and compile with gcc -o stdio.h -x c a.out.

Upvotes: 0

Related Questions