Like Tree1Likes

Program with two source files

This is a discussion on Program with two source files within the C Programming forums, part of the General Programming Boards category; Wow - this is so helpful. for print.c (where we define mul to print twice the value of parameter i) ...

  1. #16
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Wow - this is so helpful.

    for print.c (where we define mul to print twice the value of parameter i) you say we do not need to include "print.h" - so by naming convention alone the compiler knows that print.h and print.c are ??related??
    so main.c would need print.h to understand how mul is used? In main I do not need stdio.h as well?

    I thought I had to include stdio.h in print.c to use the printf() function. Is this not the case?

  2. #17
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by sjmp View Post
    wo dont I need print.h in print.c but I do in main.c?
    You don't need to #include print.h in print.c just because print.c is so simple and contains only one function and therefore is not calling any other function in print.c.
    If print.c would use other functions that would also be in print.c then it is best practice to also #include print.h in print.c so that you are free to define the functions in any order.
    It doesn't hurt to have a declaration and a definition of a function in one compilation unit.
    Kurt

  3. #18
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by sjmp View Post
    for print.c (where we define mul to print twice the value of parameter i) you say we do not need to include "print.h" - so by naming convention alone the compiler knows that print.h and print.c are ??related??
    No the names of the files don't matter for the compiler. You don't need to #include "print.h" because print.c is not depending on anything else in print.c. But it doesn't hurt to #include "print.h" ( see my prev reply ).
    so main.c would need print.h to understand how mul is used?
    Yes
    In main I do not need stdio.h as well?
    No, you don't use anything ftom stdio.h in main.c

    I thought I had to include stdio.h in print.c to use the printf() function. Is this not the case?
    Yes you need to #include <stdio.h> because you use printf() in print.c

    Kurt

  4. #19
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Kurt - thanks for taking the time to explain. very helpful.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 10:37 PM
  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, 10:46 AM
  3. Mutiple source files for one program
    By earth_angel in forum C Programming
    Replies: 7
    Last Post: 06-08-2005, 09:47 AM
  4. Multiple source files for one program
    By gflores in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2004, 02:32 AM
  5. Makefiles, Object files, Source files, OH MY!
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-29-2003, 10:36 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21