Thread: Program with two source files

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    55

    Program with two source files

    Hello

    I have to put a very basic program into two source files.
    Main.c contains Main() function and give a variable a value
    Print.c multiplies that variable by a number and prints it.

    From what I am reading I should create a third file - header file?

    Do I need to do this or just add "main.c" to the pre-processor directive, in print.c?

    Thanks,
    SP

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Never #include source files ( .c files ).
    Only header files ( .h files ) should be #included and they should contain only declarations.

    Kurt

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Thanks Kurt - but do I need a header file for this?
    From what i am reading I can just create a main.c file that gives variable 'i' a value:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int i;
    
    
    	i = 5;
    
    
    	return 0;
    }
    Add second source file (print.c)to print output - How I I get this file to reference the variable 'i' in main.c?
    Code:
    #include <stdio.h>
    
    
    
    
    int i(int)
    {
    	printf("i * 2 = %d\n", (i * 2));
    }

    Thanks for your help

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, you should have a header file for print.c. The header file is a list of functions, types, enums, etc you want available to other modules (.c files). Since main.c uses a funciton in print.c, you need to #include "print.h" in main.c, not the other way around.

    Also, make sure you always use include guards with all of your header files: Include guard - Wikipedia, the free encyclopedia.

    A few other things:

    1. Your function name 'i' is horrible. Not only is it non-descriptive, i is a common variable name and the compiler will be confused since you use it as a variable in two places and as a function name. Call the function something descriptive like mult_and_print.
    2. You need to actually name the parameter to your function. i would acceptable: int mult_and_print(int i).
    3. You technically don't need to #include <stdio.h> in main.c since you don't use any functions from stdio in that .c file.
    4. It's not required, but I prefer the more explicit version int main(void).

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    This is what I have - (using 'i' is part of the assignment. "need to assign variable 'i' a value")

    other.h
    Code:
    #ifndef _OTHER_H#define _OTHER_H
    
    
    int i;
    
    
    #endif
    main.c
    Code:
    #include "other.h"
    
    
    int main()
    {
    	int i;
    
    
    	i = 5;
    
    
    	return 0;
    }
    print.c
    Code:
    #include <stdio.h>
    #include "other.h"
    
    
    
    
    void print(int i)
    {
    	printf("i * 2 = %d\n", (i * 2));
    }

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    At first you should realize that in the first code i is a variable.In the second part i is a function and it is wrong.What you wanted to write i think is
    Code:
    int multiply(int i)
    {
        printf("i * 2 = %d\n", (i * 2));
    }
    So what you want to do is having the file main.c and from function main call another function that is located at another file.So what you should do is having the file main.c .Then you have to create for example the file function.c and the function.h .At function.h only declarations must exist.Then you have to include the .h file at main in order for the main to know the existance of the function multiply.In code this is somehow like this

    main.c
    Code:
    /*Now main can see what file functions.h contains.
    *This is equivalent to have all the code of functions.h at this point 
    *of file instead of this line(#include "functions.h)*/
    #include "functions.h"
    
    int main(void)
    {
        int i=8;
        mul(i);
        return 0;
    }
    functions.h
    Code:
    /*DECLARATIION only*/
    int mul(int i);
    functions.c
    Code:
    #include <stdio.h>
    
    int mul(int i)
    {
        printf("%d\n", i*2);
    }
    Happy to hear your questions

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Better:

    1. It's okay to have a variable named i, it was just a bad function name, and would not work if you also had variables named i.
    2. Your header file is "backwards". It should contain the function prototype for the print() function in print.c, not the variable in main.c.
    3. You should name the header file the same as the .c file, just change the extension. That way, you can always know which header files go with which .c files. In your case, use print.h
    4. Don't use leading underscores in your identifiers, they are reserved for the implementation (i.e. compiler). _OTHER_H_ should just be OTHER_H_.
    5. The include guard conventionally is the name of the header file, so I would suggest changing the include guard to PRINT_H_ after you change the file name.
    6. You don't really need to include print.h in print.c in your case.
    7. You need to actually call the print function from main.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I think you should study up on variable scope. Each of your variables that are named i are actually different variables, because of scope.

    Jim

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Thank you for breaking this down. I do have a lot of questions, if you dont mind:

    when writing a header file you need only include function declarations and a function declaration is the same as a function prototype?
    Are include guards best practice, yet not technically required for it to work?
    why are we initializing mul (int i) in function.c?

    Is this correct?
    function.h - declare function mul to contain variable 'i' as an integer data type - what is the difference between a definition and a declaration?
    main.c - initialize variable 'i' to the value of '8'. Assign variable 'i' to function 'mul' - so this file is really just about calling main and assigning a value to the variable 'i'. Exactly what are you doing in mul(i)?
    does main(void) mean return nothing? not sure why we also need to have return 0 at the end of the function body.
    functions.c - print i * 2

    for function.c - why are we initializing 'mul' again?

    Thanks SP

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Thanks for the tips/suggestions anduril462 -

    The assignment specifically requires us to give the variable 'i' a value in main.c and to print & mult in print.c -

    wo dont I need print.h in print.c but I do in main.c?

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    std10093
    When I rebuild this project w the code you supplied - should i put extern in the declaration?
    1>c:\users\main.c(4): warning C4013: 'mul' undefined; assuming extern returning int
    1> Generating Code...
    1>c:\users\print.c(6): warning C4716: 'mul' : must return a value

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by sjmp View Post
    Thank you for breaking this down. I do have a lot of questions, if you dont mind:

    when writing a header file you need only include function declarations and a function declaration is the same as a function prototype?
    Yes.

    Quote Originally Posted by sjmp View Post
    why are we initializing mul (int i) in function.c?
    What do you mean?
    Quote Originally Posted by sjmp View Post
    Is this correct?
    function.h - declare function mul to contain variable 'i' as an integer data type - what is the difference between a definition and a declaration?

    Declaration is equivalent to the prototype.Definition is the implementation of the function.This means the prototype and the body of it.
    Quote Originally Posted by sjmp View Post
    Quote Originally Posted by sjmp View Post
    main.c - initialize variable 'i' to the value of '8'. Assign variable 'i' to function 'mul' - so this file is really just about calling main and assigning a value to the variable 'i'. Exactly what are you doing in mul(i)?
    Variable 'i' is passed as an argument to function mul.In order to see what i am doing in mul,you should search for the body of mul,which lies in file functions.c
    Quote Originally Posted by sjmp View Post
    does main(void) mean return nothing? not sure why we also need to have return 0 at the end of the function body.
    int main(void) means that the return type of function main is an int and void means that no arguments are received by her.The return 0 means that all went well and the program executed successfully.
    functions.c - print i * 2

    for function.c - why are we initializing 'mul' again?

    Thanks SP[/QUOTE]

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by sjmp View Post
    std10093
    When I rebuild this project w the code you supplied - should i put extern in the declaration?
    This is it because we have the prototype as if
    Code:
    int mul(int i)
    so the compiler expects a return command in the body of it.However we do not wish to return anything so change the prototype of the fucntion mul to void(instead of int) in files functions.c and fucntons.h

  14. #14
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Part of the assignment also states the Print.c contains function print() which can be called from main() - I dont get this at all. what should in called from main?

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by sjmp View Post
    Thank you for breaking this down. I do have a lot of questions, if you dont mind:
    Not at all, that's why I'm here
    when writing a header file you need only include function declarations and a function declaration is the same as a function prototype?
    Basically, yes, a function declaration and function prototype are the same thing.
    Are include guards best practice, yet not technically required for it to work?
    Best practice is a subjective thing, but I think it is best to always use them. They are not required in your example, since it is so simple. In more complex situations, they are necessary so that you avoid accidentally including something twice, which can cause problems
    why are we initializing mul (int i) in function.c?
    I'm not sure "initializing" is the right term. You do need to provide a definition for the function (a function body -- the code for the function) in the .c file

    Is this correct?
    function.h - declare function mul to contain variable 'i' as an integer data type - what is the difference between a definition and a declaration?
    main.c - initialize variable 'i' to the value of '8'. Assign variable 'i' to function 'mul' - so this file is really just about calling main and assigning a value to the variable 'i'. Exactly what are you doing in mul(i)?
    does main(void) mean return nothing? not sure why we also need to have return 0 at the end of the function body.
    functions.c - print i * 2
    Close:
    function.h Declare function mul to take an integer i as a parameter, and return nothing. Use the 'extern' keyword. Also use include guards
    function.c Define the function mul to print out twice the value of the parameter i. Only include header files that declare functions you actually call in that .c file.
    main.c Contains your main function, which the compiler sets up to be the first function that is called when you compile and run your program. In main, iitialize the variable i to the value of 8. Call the function mul, passing it the variable i.

    A definition actually creates or sets aside space for a thing. In the case of a variable, it sets aside enough space in memory for that variable type. For a function, it sets aside enough space for all the instructions that comprise that funciton. The definition, if "visible" to the compiler, also serves as a declaration.

    A declaration describes a thing, but does not reserve any space. They are used to inform the compiler what something looks like so it can check your program for errors. For functions, a declaration includes the function name, it's return type, and the type and number of all of it's parameters.

    for function.c - why are we initializing 'mul' again?
    Again, I think the term you want is defining, but you must provide a definition for a function. You must tell it what set of instructions to run when you call the function.

    Quote Originally Posted by sjmp View Post
    The assignment specifically requires us to give the variable 'i' a value in main.c and to print & mult in print.c -

    wo dont I need print.h in print.c but I do in main.c?
    Header files like print.h are used to share information between different .c files. print.h contains information about things in print.c that other .c files need to know, if they want to use those things from print.c For example, in your main.c file, you will call the mul function. The compiler can only see one .c file at a time (and it can only see the parts of it that it has read so far -- from the top of the file down). Thus, the compiler needs to know what the mul function looks like, so it knows you're using it correctly in main.c That is why you provide the declaration of mul in print.h. print.c doesn't need print.h because it can see itself* (remember, print.h is only there for other .c files).

    You should put the 'extern' keyword in front of function and variable declarations in header files.

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, 11: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, 11: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