Muralidhar Yaragalla
Muralidhar Yaragalla

Reputation: 427

Is there any default header file in GNU

When I am going through a code snippet I have seen some functions like

#include <stdio.h>
int main() {

printf( "Upper case of a is %c\n", toupper('a'));
printf( "Upper case of 9 is %c\n", toupper('9'));
printf( "Upper case of g is %c\n", toupper('g'));
return 0;
}

being used in the source file without any header file being included. So is there any default header file that gets added to source when compiling. I am using GNU C.

Please don't mind if the syntax of the function is wrong as that is not the important point.

Upvotes: 1

Views: 356

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263667

No, there are no implicit #include directives.

What you're probably running into is that, prior to the 1999 ISO C standard, C permitted functions to be called with no visible declaration. The compiler would assume that the called function returns int and takes arguments compatible with the (promoted) arguments passed in the call.

gcc by default supports the 1990 ISO C standard plus GNU-specific extensions.

If you compile with something like gcc -std=c99 -pedantic, you'll get warnings about calls to functions with no visible declarations. (Use -std=gnu99 if you need GNU-specific extensions as well.)

Calling undeclared functions was a bad idea even before the 1999 standard. You should correct your code so there's a visible declaration (probably via a #include for the appropriate header) for each function you call or otherwise refer to.

Your original question asked about toUppercase, which is not a standard function; it may or may not be defined somewhere else.

Your revised question uses toupper, which is a standard C function declared in <ctype.h> and defined in the standard C library.

It's not surprising that you can get away with calling toupper with no visible declaration -- but you should still add

#include <ctype.h>

to the top of your source file.

Before you do that, try compiling with gcc -std=c99; you should get a warning.

One more thing: It's important to keep in mind that headers and libraries are two different things.

Headers, like <stdio.h> and <stdlib.h> are generally text files containing just declarations of functions and other entities specified by the C standard library.

Libraries, which have system-specific names like, for example, libc.so, contain the actual executable code that implements those functions.

Headers are handled by the compiler; libraries are handled by the linker.

There are generally no default headers; every header you use has to be explicitly #included, either directly or indirectly. libc.so (or whatever it's called) is typically linked by default; you don't have to specify it. (Though for the functions declared in <math.h>, you often have to specify -lm to link the corresponding library.)

Upvotes: 4

Muralidhar Yaragalla
Muralidhar Yaragalla

Reputation: 427

As forum people are asking not to discuss in comments so i have no other option than replying to " Keith Thompson" last post. I am not sure what you meant by c standard library. look here C standard library

It clearly says 27 header files are part of standard library and stdlib.h is one of them. See my point is not to argue with you. I am trying to have clarity in mind. You are saying something like libc.so as standard library but the wikipedia clearly states something else and now i am totally confused.

Upvotes: 0

Related Questions