How to distribute a C program into many C files ?

D

Darius

How to distribute a C program into many C files ?

this is the first question a C programmer asks after learning C basics,
Questions like
--what is a header file
--where should i put global variables
--where should i put declaration and definitions
--where should i put macros
--when to use static functions and variables

Is there an existing C draft for this, i mean a draft that specifies
some conventions , following which you can save yourself from double
defination error, undefined variables errors etc ?

I am planning to make such a draft rather "We" can develop such a
draft. Right now i have written some trivial conventions and i'll post
its link to the community in 1-2 days, as soon as i finalize it.
My idea is that, everybody(who is interested) will read the draft and
see if there is something missing or if something is wrong. And i will
make the necessary changes and mail the new link (new version) in this
thread. In this way the draft will adaptively improve. In this way we
can develop a good convention to make a proper C project, which will be
least vulnerable to bugs.

Here are the rules to do this :-

1) Mail the changed file or suggestions to me at (e-mail address removed)
with subject "C-project-draft"
2) If you are making some changes then comment the places where you
have changed it.
3) I will see the changes and if i find some ambiguities or
contradictions , i'll discuss it here.
4) After making necessary changes, i'll release the newer version with
your name appended to the file along with newer version.
ranveer kunal<email>(0.1), eric sosman<email>(0.2),...
5) You can suggest any rule.

And if such a draft exsists, kindly give its link here.
 
A

Allan Bruce

Darius said:
How to distribute a C program into many C files ?

this is the first question a C programmer asks after learning C basics,
Questions like
--what is a header file
--where should i put global variables
--where should i put declaration and definitions
--where should i put macros
--when to use static functions and variables

Basically you put the function prototypes in the header file, and include
the files that are necessary only for the prototypes - you can add some
definitions here if they are required externally to your functions. All
function definitions in the c file. I put my struct definitions in the
header file but this depends on your style.
Basically the header should be as minimal as possible but you dont want to
repeat any code anywhere. As for macros, generally in your source file is
best. If you want macros available across many files, then make a separate
..c file for them with a single header, eg. myMacros.h (which would be
empty).
Global variables should be avoided as much as possible but if needed I put
them in the c file which has my entry point (main). To access these from
other modules, you must declare them like:
extern int myGlobalInt;
(they are declared normally in the main c file).

Allan
 
D

Darius

Allan said:
Basically you put the function prototypes in the header file, and include
the files that are necessary only for the prototypes - you can add some
definitions here if they are required externally to your functions. All
function definitions in the c file. I put my struct definitions in the
header file but this depends on your style.
Basically the header should be as minimal as possible but you dont want to
repeat any code anywhere. As for macros, generally in your source file is
best. If you want macros available across many files, then make a separate
.c file for them with a single header, eg. myMacros.h (which would be
empty).
Global variables should be avoided as much as possible but if needed I put
them in the c file which has my entry point (main). To access these from
other modules, you must declare them like:
extern int myGlobalInt;
(they are declared normally in the main c file).

Allan

read the mail carefully, i am not asking anything, i am proposing
something.

please read the previous mail carefully.
 
C

Chris Hills

Search on "C coding standards" "coding standards" "C style guides"
"MISRA-C"

then there is NASA's coding standard onthe web and Jack Ganssle's web
site and thousands of others




How to distribute a C program into many C files ?

this is the first question a C programmer asks after learning C basics,
Questions like
--what is a header file
--where should i put global variables
--where should i put declaration and definitions
--where should i put macros
--when to use static functions and variables

Is there an existing C draft for this, i mean a draft that specifies
some conventions , following which you can save yourself from double
defination error, undefined variables errors etc ?

I am planning to make such a draft rather "We" can develop such a
draft. Right now i have written some trivial conventions and i'll post
its link to the community in 1-2 days, as soon as i finalize it.
My idea is that, everybody(who is interested) will read the draft and
see if there is something missing or if something is wrong. And i will
make the necessary changes and mail the new link (new version) in this
thread. In this way the draft will adaptively improve. In this way we
can develop a good convention to make a proper C project, which will be
least vulnerable to bugs.

Here are the rules to do this :-

1) Mail the changed file or suggestions to me at (e-mail address removed)
with subject "C-project-draft"
2) If you are making some changes then comment the places where you
have changed it.
3) I will see the changes and if i find some ambiguities or
contradictions , i'll discuss it here.
4) After making necessary changes, i'll release the newer version with
your name appended to the file along with newer version.
ranveer kunal<email>(0.1), eric sosman<email>(0.2),...
5) You can suggest any rule.

And if such a draft exsists, kindly give its link here.

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/\
/\/\/ (e-mail address removed) www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
 
M

Mark McIntyre

How to distribute a C program into many C files ?

this is the first question a C programmer asks after learning C basics,
Questions like
--what is a header file

a file that is textually included in your C file by a #include
directive.
--where should i put global variables

Ideally you should not have any. If you do, declare and initialise
them in one C file and have extern declarations in a header.
--where should i put declaration and definitions

public declarations belong in headers, static ones in the relevant
source file. Definitions can only go in source files.
--where should i put macros
headers

--when to use static functions and variables

whenever you need private functions or data. Steer clear of static
data in threaded apps.
Is there an existing C draft for this, i mean a draft that specifies
some conventions ,

The rules vary from company to company, and you need to find out what
is house style where you currently work. Typically tho, its as above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top