Prototypes of static functions 
Author Message
 Prototypes of static functions

Hi all!

Usually I divide my code into program files (.c) and header
files (.h). In header files I put all macros, typedefs,
data structure definitions and function prototypes. In
program files I put all functions with necessary #includes
depending what functions and data structures are used.

Functions declared static need prototypes too, right?
Static functions are visible only to the source file in
which the function is defined and hence there is no need
to put it in a header file that may be included by other
source files, right?

Should I define the prototypes for static function is the
same program file?

--
Dado Colussi                         Xenetic Ltd.


direct +358-9-61268126               fax +358-9-61268110
gsm    +358-40-5005501
--



Sat, 10 Nov 2001 03:00:00 GMT  
 Prototypes of static functions

Quote:

> Usually I divide my code into program files (.c) and header
> files (.h). In header files I put all macros, typedefs,
> data structure definitions and function prototypes. In
> program files I put all functions with necessary #includes
> depending what functions and data structures are used.

> Functions declared static need prototypes too, right?

Maybe.  If you define the functions before they are called then you
won't need explicit prototypes.  That isn't always possible (e.g.
mutually recursive functions), and may not be good style (but that's
a whole debate unto itself).

Quote:
> Static functions are visible only to the source file in
> which the function is defined and hence there is no need
> to put it in a header file that may be included by other
> source files, right?

> Should I define the prototypes for static function is the
> same program file?

In my opinion:

Same program file.  Also put any macros, typedefs, and structure
definitions that are for internal use only in the program file.
The header file should contain the minimum needed for other program
files to call your file.  That way, people who are going to call
your code can read the header file and grok the interface without
getting bogged down in implementation details.

If you're writing an abstraction layer, you may want to put some
of your includes into the header file; for instance, if you're
writing a windowing toolkit that works on Windows and X11, you'd
want something like:

#ifdef WIN32
#include<windows.h>
#else if defined(X11)
#include<X11/Xlib.h>
#endif /* WIN32 */

because without that you rely on the users of your library to
write abstract code.  That defeats the original point of the
abstraction.  Obviously the above macros and headers are
implementation-specific.

--Sumner
--



Sat, 10 Nov 2001 03:00:00 GMT  
 Prototypes of static functions

BTW static only matters on the function decl. , not on the
prototype, correct? (eg. if you leave it off the proto, you've
done no wrong (according to the standard, at least?))

------------------------------------------------

--



Sun, 11 Nov 2001 03:00:00 GMT  
 Prototypes of static functions

Quote:

>Usually I divide my code into program files (.c) and header
>files (.h). In header files I put all macros, typedefs,
>data structure definitions and function prototypes. In
>program files I put all functions with necessary #includes
>depending what functions and data structures are used.

>Functions declared static need prototypes too, right?
>Static functions are visible only to the source file in
>which the function is defined and hence there is no need
>to put it in a header file that may be included by other
>source files, right?

>Should I define the prototypes for static function is the
>same program file?

One solution is to use a "private" and "public" header file. The
private header has static prototypes and typdefs/defines/structs
private to that file, whereas the public header is the "exposed"
interface.

MyCode.h
int foo(int);  // public interface

MyCodep.h   // private implementation; 'p' ==> 'private'
#include "MyCode.h"
static int foo1(int);
static int foo2(int);

MyCode.c
#include "MyCodep.h"

int foo1(int a, int b)
{
  return foo1(a) * 6 + (foo2(b) - 2); // or whatever

Quote:
}

static int foo1(int a)
{
...

Quote:
}

static int foo2(int a);
{
...

Quote:
}

----
Thurman Gillespy
Seattle, Washington, USA
--



Sun, 11 Nov 2001 03:00:00 GMT  
 Prototypes of static functions
Quote:

> One solution is to use a "private" and "public" header file. The
> private header has static prototypes and typdefs/defines/structs
> private to that file, whereas the public header is the "exposed"
> interface.

Good point. Such approach allows for later (as the module grows up)
split of the module to many "program files (.c)) to make
the edition easier, for example...

Krzysiek Kobus

--



Fri, 23 Nov 2001 03:00:00 GMT  
 Prototypes of static functions

Quote:

> Should I define the prototypes for static function is the
> same program file?

Basically, static functions are private to the file, so no one should
(or needs to) see them. Put them in the same file, or keep them in
a separate header that the C file will include.

--


--



Fri, 23 Nov 2001 03:00:00 GMT  
 Prototypes of static functions


Quote:
>BTW static only matters on the function decl. , not on the
>prototype, correct?

Not correct. static specifies internal linkage at file scope. Linkage
is applicable to all function and object declarations, not just
definitions. In this I'm including "no linkage" as a form of linkage.

Quote:
>(eg. if you leave it off the proto, you've
>done no wrong (according to the standard, at least?))

void foo(void);

static void foo(void)
{

Quote:
}

is very much an error in C since you're trying to define foo with both
internal and external linkage in the same translation unit.

--
-----------------------------------------


-----------------------------------------
--



Sun, 02 Dec 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. static functions and prototypes

2. Curious: static functions and their prototypes

3. static functions and prototypes

4. Static functions prototype

5. One Problem in Calling Static function from Non static function

6. Accessing non-static functions from a static function

7. Function returning a function-pointer - how to prototype ?

8. prototype (?) of function passed to function

9. Function prototypes for pointers to functions.

10. HELP: function prototype with pointer to function as parameter

11. static/extern in prototype?

12. Pointers to non-static vs static functions

 

 
Powered by phpBB® Forum Software