c langauge how to decare a methods and enum to be able to use in multiple file in C

Posted on 2005-04-07
Last Modified: 2010-08-05
Hi, Experts:
     Currently, i am working on c. I just wonder how can i declare a function/ macro/ enum variable that allow me to use in multiple file. I will have multiple .c file. Please let me know and i would be really helpful if you can provide me with an examples.


Question by:epsilontek
    LVL 23

    Accepted Solution

    > I just wonder how can i declare a function/ macro/ enum variable that allow me to use in multiple file.

    You generally put these in a .h file (header file) that gets included in all the .c files that need the definitions:

    ----- the mydefs.h header file -----
    /* mydefs.h
     * Common function protoypes, macros, enums, structures, etc used by my .c files.

    /* Macro definitions */
    #define MY_BUFFER_SIZE 4096
    #define min(x,y)   ((x) < (y) ? (x) : (y))

    /* Some enums */
    enum days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

    /* Function prototypes */
    char * funcInA(void);
    void funcInB(const char *, char *, int);

    ----- end of mydefs.h -----

    -----  The file a.c follows -----

    #include <memory.h>

    /* include common definitions */
    #include "mydefs.h"

    char * funcInA(void)
        /* allocate a chunk of memory */
        char * buf = malloc(MY_BUFFER_SIZE);

        /* fill the buffer with repeated copies of the string "hello world" */
        if (buf)
            funcInB("hello world", buf, MY_BUFFER_SIZE);

        /* return buffer full of "hello world" to caller */
        return buf;

    ----- end of a.c -----

    ----- The file b.c follows -----

    #include <string.h>
    #include "mydefs.h"

    /* fill the supplied buffer with the pattern string */
    void funcInB(const char * fillStr, char *buf, int bufSize)
         int fillLen = strlen( fillStr );
         while (bufSize > 0) {
             strncpy(buf, fillStr, min(bufSize, fillLen));
             buf += fillLen;
             bufSize -= fillLen;

    ----- end of b.c ------


    Author Comment

    Hi, Brettmjohnson
        Thank you very muc....What are those scope of the  enums, structures in the .h file? Is it now limite.


    LVL 10

    Expert Comment

    by:Eduard Ghergu
    The .h file has the scope of the module file (.c) where it is included.
    LVL 23

    Expert Comment

    Actully, The .h file has the scope of the module file (.c) *AFTER* where it is included.

    Author Comment

    Hi. all Experst :
        Does any of you can explain to me in detail what is the different between extern function and variable
    How ans what it scoop..please include the example too


    LVL 10

    Expert Comment

    by:Eduard Ghergu

    A variable is a name given to a storage memory area where some data will be stored. A variable has a name, a type and a value. As I said, the name is used for reference, the type gave the compiler an information about how much memory is necessary to be rezerved and about the organization of this memory area. The value is what shoul be stored in that memory area.
    A function is a code sequence that receives some parameters, make some calculations or whatever, and returns a result. Tipically, a function has a prototype (signature, declaration) in a .h file and an implementation (definition) in a .c file. A function, in order to be used in a .c file, it should be made "visible" in that file. If the function implementation is made in another .c file than the one where you want to use it, in order to avoid a duplicate implementation, you can use extern keyword that instruct the compiler to search for the function definition in another .c file than the current one.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    Adobe Customization Wizard XI issues 26 134
    wefewf 2 28
    array11 challenge 16 40
    wordlen challenge 3 28
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now