Thread: ahhh, noob question.

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    ahhh, noob question.

    I'm using DevC++ and im trying to call a function that is written in another '.c' file that is included in the project

    umm heres an example of what im trying to do.



    ~MAIN.C~

    Code:
    #include <stdio.h>
    #include "func.c"
    
    int main()
    {
    
        callprint("hello world!");
        
    getchar();  
        return 0;
        
    }//end of main
    ~FUNC.C~

    Code:
    #include <stdio.h>
    
    void callprint(char text[])
    {
    printf(text);               
    }

    it keeps comming up with multi definition of callprint(char text[])

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You never include .c files, you include .h files.
    func.h:
    Code:
    #ifndef __FUNC_H__
    #define __FUNC_H__
    
    void callprint(char text[]);
    
    #endif
    func.c:
    Code:
    #include <stdio.h>
    
    void callprint(char text[])
    {
        printf(text);               
    }
    main.c:
    Code:
    #include <stdio.h>
    #include "func.h"
    
    int main()
    {
        callprint("hello world!");   
        getchar();  
        return 0;
    }//end of main

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > it keeps comming up with multi definition of callprint(char text[])
    That's because you #include the source code (that's one copy), and you also compile it as a separate file (that's two).

    Do as rags_to_riches says and create a header file.

    Oh, and
    printf(text);
    is extremely dangerous.
    If you make the mistake of passing a string with formats (like &#37;d), then the call to printf becomes undefined (no extra parameters you see).

    Use fputs() in this instance, or perhaps
    printf("%s",text);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. noob question
    By unclebob in forum C++ Programming
    Replies: 9
    Last Post: 09-24-2006, 08:48 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM