Thread: Calling function in another file

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Calling function in another file

    I am writing a program that takes integers from a user and sorts them in ascending order. I understand the process of most of the steps but our teacher has said that the actual function that determines which of two array elements is larger must be done from another .c file.

    I'm not quite sure how the calling of another file works. Here is the code I have so far.

    p1.c:

    Code:
    /* This program in its entirety takes a list of integers from a user and arranges them in ascending
    order */
    
    
    #include<stdio.h>
    #include "temp.h"
    
    #define PROMPT "Enter up to 10 integers, ending with -1:"
    #define TOO_MANY "Exceeded maximum array size."
    
    main(void)
    {
            char input[MAX_NUM + 1] = {'0'};
            int x, i, j, temp1, temp2, temp;
    
            puts(PROMPT);
            gets(input);
    
    
    
            for(i = 0; i < MAX_NUM; i++)
            {
                    for(j = MAX_NUM; j < i; j--)
                    {
                            larger(input[i], input[j]);
    
                            if(temp = input[j])
                            {
                                    temp1 = input[i];
                                    input[i] = input[j];
                                    input[j] = temp1;
                            }
    
                    }
    
            }
    
            return 0;
    }
    temp.h

    Code:
    #define MAX_NUM 10
    int larger(int a, int b);

    temp.c

    Code:
    #include<stdio.h>
    #include "temp.h"
    
    int larger(int a, int b)
    {
            if(a > b)
            {
                    temp = a;
    
            } else temp = b;
    
            return temp;
    
    }
    The teacher provided us with the temp.h and says that the larger() function must be defined in temp.c. So now that I have that, how do I access the function from p1.c?

    And will the 'temp' variable returned in the larger() function be accessible from p1.c in the manner I've written?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include "temp.h"
    This line indirectly imports the declaration of the function so when you compile it, it knows that there should be a function somewhere with that name and signature. That means you can simply call the function. Use it in every .c file that needs to use that function (or in the case of temp.c where it needs to define the function).

    When you compile, you must specify all of the .c files to compile, including the one that has the definition of the function(s) declared in temp.h. The compiler (or perhaps more properly, the linker) is tasked with figuring out how to put it all together.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Ah, thank you MacGyver, it didn't occur to me that I had to compile both .c files. Thanks man

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by cashmerelc View Post
    And will the 'temp' variable returned in the larger() function be accessible from p1.c in the manner I've written?
    You don't use the return value at all within the P1.C source file. You'd have to assign the return value from the function call to one of your variables to use it in some way.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    I see, hk, that makes sense.


    I've made a few changes to my code and I'm getting a new compiler error.

    passing arg 1 of `fgets' makes pointer from integer without a cast

    The user is going to enter integers, one at a time with each followed by a return, so I'm trying to use fgets to put each integer into the same array in respective elements and here is what I have.

    Code:
     for(i = 0; i < MAX_NUM; i++)
            {
                    fgets(input[i], MAX_NUM, stdin);
                    nl = strrchr(input, '\n');
                    if (nl) *nl = '\0';
            }
    I don't understand the error.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    input[i] is a char (integer type) but the fgets function accepts a pointer for the first argument, the error is saying you are trying to convert/use the integer value stored in input[i] as a pointer when it isn't and is prompting you that perhaps you might want to use a cast (which you don't in this case) which would suppress that warning/error. fgets is perhaps not the way you want to go here for data input since you'd still have to convert the text entered by the user into a numeric/integer value perhaps using sscanf before storing it in the input array. It occurs to me that your array probably should be an array of ints and not char given what is happening.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    main(void) is lacking return type int!
    gets is very, very bad! fgets is a better choice.
    Read the FAQ for more info or check my signature for links.
    Last edited by Elysia; 12-05-2007 at 12:43 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > main(void) is NON-standard!

    The void as an argument is okay, it's only bad as a return type. The int return type should be explicit, though.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or maybe I should just mention it's bad practice and it's lacking return type int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Elysia View Post
    Or maybe I should just mention it's bad practice and it's lacking return type int.
    Making the argument(s) of main() explicit is _not_ bad practice. In fact, the C99 Standard explicitly mentions "int main(void)" as an allowed prototype, but not "int main()".

    http://faq.cprogramming.com/cgi-bin/...wer=1044841143

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You misundestand
    main(void) = bad practice
    int main(void) = good practice
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM