functions and main

M

Merrill & Michele

The following progs differ only in the location of the prototype
(<--adjective) definition with respect to the main call. Both build and
behave for me. Is there a difference that amounts to something once the
subject matters gets stickier? MPJ
#include <stdio.h>
int main(void){
int i = 5;
void increment(int *);
increment(&i);
printf("i = %d\n",i);
return 0;
}
void increment(int *px){
int tmp = *px;
++ tmp;
*px = tmp;
}


#include <stdio.h>
void increment(int *);
int main(void){
int i = 5;
increment(&i);
printf("i = %d\n",i);
return 0;
}
void increment(int *px){
int tmp = *px;
++ tmp;
*px = tmp;
}
 
L

Lawrence Kirby

The following progs differ only in the location of the prototype
(<--adjective) definition with respect to the main call. Both build and
behave for me. Is there a difference that amounts to something once the
subject matters gets stickier? MPJ

Yes, this is the difference between putting a function declaration in
a function (i.e. at block scope) or outside a function (i.e. at file
scope). Don't put function declarations in functions, if you do the
compiler is never required to validate the type of the declaration against
the function definition (it might anyway but it isn't required to). It is
also not where C programmers expect to find them. You won't go far wrong
if you stick to the rules

1. A static function declaration should go at or near the top of the file
where the function is defined. Some people prefer to put definition before
use to avoid the need for a declaration. That's fine too, although I like
having the declarations as a summary of the functions in the source file.

2. A non-static function declaration should be in an appropriate header
file that is included by all source files that use the function.

Lawrence
 
M

Merrill & Michele

Lawrence Kirby said:
Yes, this is the difference between putting a function declaration in
a function (i.e. at block scope) or outside a function (i.e. at file
scope). Don't put function declarations in functions, if you do the
compiler is never required to validate the type of the declaration against
the function definition (it might anyway but it isn't required to). It is
also not where C programmers expect to find them. You won't go far wrong
if you stick to the rules

1. A static function declaration should go at or near the top of the file
where the function is defined. Some people prefer to put definition before
use to avoid the need for a declaration. That's fine too, although I like
having the declarations as a summary of the functions in the source file.

2. A non-static function declaration should be in an appropriate header
file that is included by all source files that use the function.
Thanks. I just violated Mr. Pop's injunction to go through K&R sequentially
and was leafing through C Unleashed. I had apologized to Mr. Summit for
printing off and binding his FAQs without giving him a cent while unaware
that I indeed had. There is a question I've been dying to ask though: what
the hell does 'foo' stand for? MPJ
 
D

dandelion

Merrill & Michele said:
There is a question I've been dying to ask though: what
the hell does 'foo' stand for? MPJ

It's the name of a popular cafe frequented by programmers: The Foo Bar,
owned by Mr Foo.
 
M

Merrill & Michele

dandelion said:
It's the name of a popular cafe frequented by programmers: The Foo Bar,
owned by Mr Foo.
And people stumble home from this bar from all continents? In America, we
have a homonym for foobar, and I doubt that this meaning is one a programmer
would like to have associated with his code. MPJ
 
M

Michael Mair

Don't pull the newbies' legs -- they might fall off... ;-)

And people stumble home from this bar from all continents? In America, we
have a homonym for foobar, and I doubt that this meaning is one a programmer
would like to have associated with his code. MPJ

Have a look at the jargon file; the glossary provides quite a lot on
foo:
http://www.catb.org/~esr/jargon/

Apart from that, bookmark it: It will often help to understand the
gibberish in comp.*

BTW: Asking google in a nice way also would have helped you.


Cheers
Michael
 

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,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top