12 Replies - 6338 Views - Last Post: 07 February 2011 - 07:57 AM Rate Topic: -----

#1 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

calling functions without including header files

Posted 06 February 2011 - 07:41 AM

Hey everyone,

So I am not really a C person but I have been playing with it lately and something got me curious. I forgot to include ctype.h and i used some of its functions, the compiler didn't issue any warnings or errors and gave correct output, but then later i checked out the stdio.h which i had included and found that it includes getopt.h which includes ctype.h so i guess that explained it. But since i am pretty nosy I decided to remove all header file includes from the code and recompiled. The compiler didn't issue any warnings or errors unless you raise the warning level and then it issues an "implicit declaration of function" warning, but it still spits out the right output. Now that is what's puzzling me, I didn't include any header files and i can still use some functions. I have been searching around and got to know that gcc has some built-in functions from the standard library for optimization and other purposes. Now my guess is that the program computes the wanted output (disregarding the warning messages) because the compiler looks for those functions in its built-in functions and finds definitions for them. I just need someone to confirm this for me or provide me with the correct answer. Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: calling functions without including header files

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5459
  • View blog
  • Posts: 16,986
  • Joined: 25-December 09

Re: calling functions without including header files

Posted 06 February 2011 - 07:45 AM

The correct answer is to include the header files.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: calling functions without including header files

Posted 06 February 2011 - 07:47 AM

C is very, very generous in assuming you know what you're doing, as you just discovered :)
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: calling functions without including header files

Posted 06 February 2011 - 07:48 AM

I know you are supposed to include the header files and i know for what reasons. I am just trying to understand the rather strange behavior. In Java for example if i use a certain method without importing the class first, a compilation error is issued but this not the case in C.
Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: calling functions without including header files

Posted 06 February 2011 - 07:54 AM

View PostJackOfAllTrades, on 06 February 2011 - 05:47 PM, said:

C is very, very generous in assuming you know what you're doing, as you just discovered :)


That seems very true. I've been playing around with it these days and I can say that its pretty crazy in a lot of situations, a lot of times it takes me a while to be able to understand why a certain behavior is happening. Eventhough its strange, I am actually loving it. I find it a lot more interesting than Java which is what I do most of my work in.
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2144
  • View blog
  • Posts: 4,208
  • Joined: 30-May 10

Re: calling functions without including header files

Posted 06 February 2011 - 09:12 AM

In the absence of a proper prototype, the C compiler will make up one the first time you use the function.
This is an historic feature of C from before ANSI/ISO came along.

You should also note that such "implicit declarations" are deprecated (don't rely on them being there for much longer), and some compilers can warn you if you have any left lying around in old code.

#include <stdio.h>

int main ( ) {
  foo("hello");
  // extern int foo(); is now assumed
  int x = foo(3);
  return 0;
}

void foo ( double d ) {
}


$ gcc bar.c
bar.c:10: warning: conflicting types for ‘foo’
bar.c:4: note: previous implicit declaration of ‘foo’ was here



Now this function will take implicit parameters and return an int. Meaning you can pass any parameters you like and it will "compile".
If the parameters you actually pass happen (through default promotion rules) to align with what the prototyped function was expecting, then the code will "work".
Was This Post Helpful? 2
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: calling functions without including header files

Posted 06 February 2011 - 09:41 AM

It's also important to note that sometimes such behavior is flagged as your code base grows. I do find that particularly large projects C++ can magically make you stick to fairly strict convention... without telling you precisely what is wrong. :whistling:
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: calling functions without including header files

Posted 06 February 2011 - 09:42 AM

View PostSalem_c, on 06 February 2011 - 07:12 PM, said:

In the absence of a proper prototype, the C compiler will make up one the first time you use the function.
This is an historic feature of C from before ANSI/ISO came along.

You should also note that such "implicit declarations" are deprecated (don't rely on them being there for much longer), and some compilers can warn you if you have any left lying around in old code.

#include <stdio.h>

int main ( ) {
  foo("hello");
  // extern int foo(); is now assumed
  int x = foo(3);
  return 0;
}

void foo ( double d ) {
}


$ gcc bar.c
bar.c:10: warning: conflicting types for ‘foo’
bar.c:4: note: previous implicit declaration of ‘foo’ was here



Now this function will take implicit parameters and return an int. Meaning you can pass any parameters you like and it will "compile".
If the parameters you actually pass happen (through default promotion rules) to align with what the prototyped function was expecting, then the code will "work".


Thanks for the reply but I think I figured it out. it just took me some time to connect the dots. including the header files just includes the function declarations, constants etc. The standard functions actual definitions are in glibc. so when I call a standard function without including its prototypes, that call is an implicit declaration and things will work out for the reasons you specified. If I said something incorrect please correct me though.

EDIT: I was just confused because I forgot that the actual function definitions are not in the header files, just the declarations.

This post has been edited by mostyfriedman: 06 February 2011 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7154
  • View blog
  • Posts: 14,901
  • Joined: 16-October 07

Re: calling functions without including header files

Posted 06 February 2011 - 10:38 AM

It's more acurate to say that C does a whole lot of magic "implicitly".

e.g.
// #include <stdio.h>

void a() { printf("hello from a\n"); }

int main() {
	a();
	return 0;
}



Results:
[email protected]:~/Dev/C$ gcc t.c
t.c: In function 'a':
t.c:3: warning: incompatible implicit declaration of built-in function 'printf'



The only real reason it's bitching is because it can't imply the defintion we want in the object file it's implicitly including.

If I change a to void a() { puts("hello from a\n"); } it compiles fine; it's a simple inference. If I still want to use printf without the header, I can do:
e.g.
int printf (const char *fmt, ...);

void a() { printf("hello from a\n"); }

int main() {
	a();
	return 0;
}



The reason this works is because "extern" is also an implicit flag.

Test two, throw a in another file. Now compile: gcc t.c a.c. This works.

Let's throw an explicit declaration in a.c:
// was: void a() { puts("hello from a\n"); }
// same as: extern void a() { puts("hello from a\n"); }
// static is a very contextual word in C
// in this context, it says the function is local to the object file
static void a() { puts("hello from a\n"); }


[email protected]:~/Dev/C$ gcc t.c a.c
/tmp/cciGLicj.o: In function `main':
t.c:(.text+0x7): undefined reference to `a'
collect2: ld returned 1 exit status



Header files preform a number of roles. They are documentation and also validate the functions signatures in the source file when included. You can write a complex C program with many source files and no header files. However, you really wouldn't want to.
Was This Post Helpful? 1
  • +
  • -

#10 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: calling functions without including header files

Posted 06 February 2011 - 11:07 AM

Make your own functions you want to use or include the header.

You won't know the words without a dictionary, right? :)
Was This Post Helpful? 0
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: calling functions without including header files

Posted 06 February 2011 - 11:30 AM

View Postbaavgai, on 06 February 2011 - 05:38 PM, said:

It's more acurate to say that C does a whole lot of magic "implicitly".

And that's the fault of the programmer rather than the language in my view. C also permits you to define everything, so to allow it to attempt to assume anything implicitly is poor code design. :)
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7154
  • View blog
  • Posts: 14,901
  • Joined: 16-October 07

Re: calling functions without including header files

Posted 06 February 2011 - 03:24 PM

I don't know about fault so much as choice. Every language is going to have a collection of default behaviors. Scope, reference, protection level, etc. A major language design decision is how much to require from the programmer to define behavior.

C is as loose as possible, allowing the programmer to manipulate memory at will. It's what C programmers enjoy and what gives C++ programmers aneurysms. C++ gets more type conscious.

Java is so anal that you have to declare are all expected exceptions in the method definition. Early C# was near identical to Java, but chose to eliminate this requirement. The "duct typed" languages take an entire different path.

Generally purpose programming languages all have the same goal. Every one believes they know the best way to get there.
Was This Post Helpful? 0
  • +
  • -

#13 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: calling functions without including header files

Posted 07 February 2011 - 07:57 AM

View Postbaavgai, on 06 February 2011 - 10:24 PM, said:

...
C is as loose as possible, allowing the programmer to manipulate memory at will. It's what C programmers enjoy and what gives C++ programmers aneurysms. C++ gets more type conscious.
...

So true... never quite thought of it that way! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1