Re: Clunky C cleanup code
From: Merrill & Michele (beckjensen_at_comcast.net)
Date: 12/01/04
- Next message: Merrill & Michele: "Re: read url"
- Previous message: Dik T. Winter: "Re: read url"
- In reply to: Keith Thompson: "Re: Clunky C cleanup code"
- Next in thread: dandelion: "Re: Clunky C cleanup code"
- Reply: dandelion: "Re: Clunky C cleanup code"
- Reply: Lawrence Kirby: "Re: Clunky C cleanup code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 30 Nov 2004 21:01:55 -0600
"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnbrdedf5a.fsf@nuthaus.mib.org...
> "Merrill & Michele" <beckjensen@comcast.net> writes:
> >> "Mike Wahler"
> >> >"Merrill & Michele"
> >> > > "fatted"
> >> > > I've written a function (clean_string) to remove characters from a
> >> string,
> >> > > but it looks clunky to me, and I'm sure there's a more 'C' like way
of
> >> > > doing it (still learning), comments and advice welcome...
> >> > >
> >> > > --
> >> > > #include <stdio.h>
> >> > > #include <stdlib.h>
> >> > > #include <string.h>
> >> > >
> >> > > int main(void)
> >> > > {
> >> > > char * clean_string(const char *, const
> >> > [single point to make, rest of post snipped]
> >> >
> >> > I get routinely chastised for defining functions inside of main().
> >>
> >> OP's code did not contain any function definitions inside of main
> >> or any other function.
> >
> > Let me go through my premises. There are two return statements. This
> > implies .... What if I #defined 'definition' 'prototype declaration' in
my
> > last post? MPJ
>
> If you "#defined 'definition' 'prototype declaration'", your last post
> would have been unreadable. But yes, I know what you meant.
>
> Yes, there are two return statements, one inside the definition of
> main(), and one inside the definition of clean_string(). No problem
> there.
>
> A function definition includes the stuff in curly braces; that can't
> be nested inside another function definition.
>
> The original post has a function *declaration* inside the definition
> of main(). This is legal, but not generally a good idea.
>
> You need to have (or, in C90, should have) a visible declaration in
> order to call the function from within main(). One way to do this
> would be to put the entire definition of clean_string() before the
> definition of main(), but there are good reasons to want to put main()
> at the top -- and this method doesn't work if you have mutually
> recursive functions.
>
> The usual method is to put declarations (prototypes) for functions
> other than main near the top of the source file, outside any function
> definition. (You don't usually need a separate prototype for main
> because main isn't usually called explicitly.) If the functions
> aren't to be called from any other source file, it's best to declare
> them static. If they are, the declarations should be in a separate
> header file.
Thanks all for correcting my correction, in particular Mr. Thompson with
generous detail. OP does one other thing that caught my interest. He
returns EXIT_SUCCESS from main(). My stdlib has the following:
/* Definition of the argument values for the exit() function */
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
I know that one possible answer to my following question is
READ_THE_STANDARD, but that's like telling me to climb Everest with only a
windbreaker. Q) Is there any virtue to using EXIT_SUCCESS as opposed to 0
? MPJ
- Next message: Merrill & Michele: "Re: read url"
- Previous message: Dik T. Winter: "Re: read url"
- In reply to: Keith Thompson: "Re: Clunky C cleanup code"
- Next in thread: dandelion: "Re: Clunky C cleanup code"
- Reply: dandelion: "Re: Clunky C cleanup code"
- Reply: Lawrence Kirby: "Re: Clunky C cleanup code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|