468,457 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Function declaration without parameters

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.
Jul 17 '06 #1
8 4584
Ian <ia*@127.0.0.1wrote:
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
Jul 17 '06 #2
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;
}
--

Frederick Gotham
Jul 17 '06 #3
Ian
Frederick Gotham wrote:
You must define them as "static":

static int Func(void)
{
return 5;
}
Why static? I don't think this is necessary.
Jul 17 '06 #4
Ian posted:
Frederick Gotham wrote:
>You must define them as "static":

static int Func(void)
{
return 5;
}

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"?

--

Frederick Gotham
Jul 17 '06 #5
Ian wrote:
Frederick Gotham wrote:
>You must define them as "static":

static int Func(void)
{
return 5;
}

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:
>>Consider that I have a "non-extern" function that takes no
parameters.
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
Jul 17 '06 #6

Flash Gordon wrote:
Ian wrote:
Frederick Gotham wrote:
You must define them as "static":

static int Func(void)
{
return 5;
}
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:
>>Consider that I have a "non-extern" function that takes no
>>parameters.

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("");

Jul 17 '06 #7
On 17 Jul 2006 11:06:09 -0700, "sarathy" <sp*********@gmail.com>
wrote:

>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
Jul 17 '06 #8

Barry Schwarz wrote:
On 17 Jul 2006 11:06:09 -0700, "sarathy" <sp*********@gmail.com>
wrote:

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
Thanks for pointing that out ... I just didnt mean "". I meant ... (
something within the paranthesis)

Jul 18 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by johny smith | last post: by
28 posts views Thread by Michael B. | last post: by
18 posts views Thread by Razvan | last post: by
20 posts views Thread by Christian Christmann | last post: by
28 posts views Thread by Bill | last post: by
29 posts views Thread by Ravishankar S | last post: by
8 posts views Thread by vaib | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.