?
Solved

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

Posted on 2005-04-07
6
Medium Priority
?
238 Views
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.

Thank

Epsilontek
0
Comment
Question by:epsilontek
  • 2
  • 2
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 160 total points
ID: 13733166
> 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 ------

0
 

Author Comment

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

Thank,

Epsilontek
0
 
LVL 10

Expert Comment

by:Eduard Ghergu
ID: 13734105
The .h file has the scope of the module file (.c) where it is included.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 13742280
Actully, The .h file has the scope of the module file (.c) *AFTER* where it is included.
0
 

Author Comment

by:epsilontek
ID: 14877385
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

Thank

Epsilontek
0
 
LVL 10

Expert Comment

by:Eduard Ghergu
ID: 14884553
Hello!

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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Simple Linear Regression
Loops Section Overview

807 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