Function declaration without parameters

I

Ian

Consider that I have a "non-extern" function that takes no
parameters. Which of the following would be ANSI C90 compliant:


void hello(void)
{
printf("Hello\n");
}

or

void hello()
{
printf("Hello\n");
}



I always (and I mean always) use the former. I couldn't find any
mention of the correct method in the FAQ, but I remember reading
somewhere about only using 'void' as a parameter with functions
declared as 'extern' if you want to assure it accepts no parameters.
 
R

Richard Bos

Ian said:
Consider that I have a "non-extern" function that takes no
parameters. Which of the following would be ANSI C90 compliant:

void hello(void)
void hello()

Both. As a declaration, the former specifies that the function takes no
parameters; the latter that it takes an unknown number. As part of the
definition, neither gives the function access to any parameters. This
means that the former is superior for maintenance purposes, since it
allows for better error-checking of calls to this function, but both are
compliant and, if called without arguments, will do what you want.

Richard
 
F

Frederick Gotham

Ian posted:
Consider that I have a "non-extern" function that takes no
parameters.


You must define them as "static":

static int Func(void)
{
return 5;
}
 
F

Frederick Gotham

Ian posted:
Why static? I don't think this is necessary.


Functions have external linkage by default. If you wish to be explicit,
you may define it as "extern":

int extern Func(void) { ...

If you want it to have internal linkage, then you must define it as
"static":

int static Func(void) { ...


In the original post, the OP refers to a "non-extern" function.

Forgive me... but wouldn't "static" be synonymous with "non-extern"?
 
F

Flash Gordon

Ian said:
Why static? I don't think this is necessary.

You've not left sufficient context in to know why static. Searching
Google I see you previously said:
If you don't define a function as static it is externally visible. So if
you don't want it to be externally visible you have to declare it as static.
 
S

sarathy

Flash said:
You've not left sufficient context in to know why static. Searching
Google I see you previously said:

If you don't define a function as static it is externally visible. So if
you don't want it to be externally visible you have to declare it as static.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

All functions by default have external linkage;

i.e
int printf (""); and
extern int printf ("");

are equivalent.

If the function should be visible only within a particular file, and
not anywhere outside this file, make the declaration as

static int printf("");
 
B

Barry Schwarz

All functions by default have external linkage;

i.e
int printf (""); and
extern int printf ("");

are equivalent.

If the function should be visible only within a particular file, and
not anywhere outside this file, make the declaration as

static int printf("");

You meant declarations, not invocations (statements that call the
function). What you wrote are syntax errors.

int printf(const char *, ...);
extern int printf(const char *, ...);

are equivalent and declare printf to have external linkage.

static int printf(const char *, ...);

delcares printf to have internal linkage. (At this point it would
probably be good idea for the sample declarations to use a different
function name than printf unless you are really writing one of your
own.)


Remove del for email
 
S

sarathy

Barry said:
You meant declarations, not invocations (statements that call the
function). What you wrote are syntax errors.

int printf(const char *, ...);
extern int printf(const char *, ...);

are equivalent and declare printf to have external linkage.

static int printf(const char *, ...);

delcares printf to have internal linkage. (At this point it would
probably be good idea for the sample declarations to use a different
function name than printf unless you are really writing one of your
own.)


Remove del for email

Thanks for pointing that out ... I just didnt mean "". I meant ... (
something within the paranthesis)
 

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

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top