Thread: When to use header files?

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28

    When to use header files?

    The #include "plot.h" in file1.c does'nt seem to make a difference.
    If I remove the line with #include plot.h it still compiles.

    I have a structure as below:
    ├── build
    ├── include
    └── plot.h
    └── src
    ├── file1.c
    └── plot.c
    file1.c
    Code:
      #include <stdio.h>
      #include <stdlib.h>
      #include "plot.h"
      
      int main(void) {
      
          plotXY(3,4);
      
          return 0;
      }
    plot.c
    Code:
      #include <stdio.h>
      #include <stdlib.h>
      
      void plotXY(int x,int y) {
      
          printf("Res: %d\n",x*y);
      }
    plot.h
    Code:
    void plotXY(int x,int y);
    I'm using the following to compile the code:
    gcc src/plot.c -o build/plot.o -c
    gcc src/file1.c -o build/file1.o -c -I ./include
    gcc -o pgm1 build/file1.o build/plot.o

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is this (deprecated) language feature in which by default, parameters and return types are presumed to be of the type of int unless specified. In this case it worked in your favour, but you should not rely on this, i.e., having the header inclusion is correct and not redundant since you should declare your functions (with function prototypes or with the definition itself) before calling them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    I would like to add that I suggest that you always compile with at least the flags "-W" and "-Wall", then you would see that your compiler probably issued a warning.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    OK, I see.

    Regarding the "-W"and "-Wall" flags you are correct. I got a warning telling me "impilicit declaration ..."

    Thanks for the clarification.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Header Files and Source files
    By yukapuka in forum C Programming
    Replies: 3
    Last Post: 10-10-2012, 01:57 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  4. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  5. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 09:35 AM