elementary construction

M

Merrill & Michele

Today is when I sing in the choir, and I was thinking about rehearsal
tonight when I realized that in order to be in the C choir, I had to buy K&R
to be reading the same "music."

I am familiar with http://www.eskimo.com/~scs/C-faq/top.html , but I found
no resolution to my first question, which crops up on page 6. I have always
called main with an integer that says how many pointers are being passed to
it. Is this just a matter of style?

Would int main(int a, int b, int c){ return (51);} be, while close to
trivial, ANSI compliant? MPJ
 
J

Joona I Palaste

Merrill & Michele said:
Today is when I sing in the choir, and I was thinking about rehearsal
tonight when I realized that in order to be in the C choir, I had to buy K&R
to be reading the same "music."
I am familiar with http://www.eskimo.com/~scs/C-faq/top.html , but I found
no resolution to my first question, which crops up on page 6. I have always
called main with an integer that says how many pointers are being passed to
it. Is this just a matter of style?

The first argument to main should be an integer saying the number of
arguments, the second should be a pointer to the arguments themselves.
It's not a matter of style, it's the standard requirement.
Would int main(int a, int b, int c){ return (51);} be, while close to
trivial, ANSI compliant? MPJ

No it would not.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Parthenogenetic procreation in humans will result in the founding of a new
religion."
- John Nordberg
 
J

Jens.Toerring

Merrill & Michele said:
I am familiar with http://www.eskimo.com/~scs/C-faq/top.html , but I found
no resolution to my first question, which crops up on page 6. I have always
called main with an integer that says how many pointers are being passed to
it. Is this just a matter of style?
Would int main(int a, int b, int c){ return (51);} be, while close to
trivial, ANSI compliant? MPJ

No. The only versions of main() blessed by the standard are either
passing it nothing i.e.

int main( void )

or passing it an int and an array of char pointers

int main( int argc, char *argv[ ] )

which you also sometimes find written as

int main( int argc, char **argv )

with argv having argc+1 elements, argv[0] to argv[argc-1] pointing
to strings and argv[argc] being a null pointer. Of course, if you
want to confuse people reading your program you can give argc and
argv different names - for maximum effect exchange their names;-)

Regards, Jens
 
E

Eric Sosman

Merrill said:
Today is when I sing in the choir, and I was thinking about rehearsal
tonight when I realized that in order to be in the C choir, I had to buy K&R
to be reading the same "music."

I am familiar with http://www.eskimo.com/~scs/C-faq/top.html , but I found
no resolution to my first question, which crops up on page 6. I have always
called main with an integer that says how many pointers are being passed to
it. Is this just a matter of style?

Would int main(int a, int b, int c){ return (51);} be, while close to
trivial, ANSI compliant? MPJ

Not in a "hosted environment," which covers the vast
majority of general-purpose C implementations. Such an
implementation launches a program by taking care of some
necessary housekeeping (for example, initializing all
your `static' variables, setting up stdin/out/err, and
so forth) and then calling the main() function that you
supply. The implementation uses Implementation Magic(tm)
to accomplish all this, but isn't Magical enough to read
your mind about what sort of arguments your main() wants,
so it always provides an `int' and a `char**'. If your
main() expects something else, unpredictable confusion
will arise.

Actually, there is one further bit of Magic: You can
write your main() to expect no arguments at all, and the
implementation is required to call it anyhow. Thus, there
are two valid "signatures" for main():

int main(int, char**);
int main(void);

(There are, of course, other ways to express these two
possibilities.)

Now, a particular C implementation may also support
other main() signatures in addition to the two required
by the language -- a common extension is

int main(int, char**, char**);

where the third (non-standard) argument points to an
array of "environment variables." The Standard does not
forbid such alternate forms for main(), but also does not
require them. You can use other forms if your platform
supports them, but all bets are off when you move your
code to a different platform.

All this pertains to "hosted environments." The C
Standard also considers "free-standing environments," the
sort of C implementation that might operate your microwave
oven or your talking toilet. (I'm not kidding; use Google.)
These environments make their own provisions for invoking
and terminating programs, and main() may have a completely
different form -- it may not even have any special meaning
at all. Indeed, there may be no such concept as "program
start" or "program finish;" the program may simply run as
long as the electricity keeps flowing.

Finally, a word about all that Implementation Magic(tm).
I've said it "calls main()," but in fact the actual means by
which main() begins to execute need not be a bona-fide C
language function call. The implementation must make it
appear "as if" main() has been called from a higher-order
function, but the means by which this happens could be
mysterious in the extreme. You really needn't worry about
this because once your main() receives control it seems to
have been called in ordinary fashion; I'm only mentioning
this to keep my fellow pedants off my case.

To return to your music analogy, when you're "hosted"
by a choir with an organ and/or piano and/or orchestra,
tuning to A = 440Hz is all but mandatory; you use the
standard forms for main(). Some groups with an interest in
early music may deliberately use older tunings like A=435Hz;
some platforms support alternate main() forms. And when
you're singing in the shower, "Nessun dorma" sounds a lot
better if you slide that A down to 370Hz or so; there are
no standards for main() in a free-standing shower stall.
 
M

Merrill & Michele

And what characters are legal in the main call? I know I can use argc and
banana. What about % or a number? Common sense says that you don't try to
confuse somebody with a main call, but I'm wondering about ANSI compliance
or feasibility. MPJ
 
D

Dave Vandervies

And what characters are legal in the main call?

Anything that's legal (that is, well-formed and not reserved) as an
identifier for any other function argument.
I know I can use argc and
banana. What about %

Nope, neither in main's arguments nor in any other variable name.
Only if you want the integer modulus operation, a printf format modifier,
or the digraphs for '{', '}', or '#'.
or a number?

Not a decimal representation of a number on its own, but something like
'fortytwo' or 'not_42' is OK.
Common sense says that you don't try to
confuse somebody with a main call, but I'm wondering about ANSI compliance
or feasibility. MPJ

No different from any other function interface: The number and type of
arguments are defined, but you can call them anything you like, within
the restrictions on which identifiers are legal.


dave
 
M

Merrill & Michele

No different from any other function interface: The number and type of
arguments are defined, but you can call them anything you like, within
the restrictions on which identifiers are legal.

I am going to beat this horse until I think it's dead. The following
compiles for me. Is it ANSI compliant?

#include <stdio.h>

int main(int argc, char* [])
{

return 0;
}

So am I to think of the main call as being like any other C function call?
I've always thought of it differently. MPJ
 
J

Jack Klein

No different from any other function interface: The number and type of
arguments are defined, but you can call them anything you like, within
the restrictions on which identifiers are legal.

I am going to beat this horse until I think it's dead. The following
compiles for me. Is it ANSI compliant?

#include <stdio.h>

int main(int argc, char* [])

This is an error requiring a diagnostic. In a function definition all
arguments must have names. If is only in a function prototype that
the names may be omitted.
{

return 0;
}

So am I to think of the main call as being like any other C function call?
I've always thought of it differently. MPJ

In C, main() is _almost_ like any other function. Prior to C99 it had
only one difference, in that it could be called either with no
arguments (int main(void)) or two arguments (int main(int argc, char
**argv).

C99, the 1999 major update to the C standard, added one other special
property to main, that is if main() is properly defined with a return
type of int and one of the two standard sets of arguments, the effect
of hitting the closing brace without executing a return statement is
the same as executing "return 0;".

This applies _only_ to the initial call to main() at program start up,
and is not guaranteed to work equivalently if main() is called
recursively from within a program.
 
M

Michael Wojcik

All this pertains to "hosted environments." The C
Standard also considers "free-standing environments," the
sort of C implementation that might operate your microwave
oven or your talking toilet.

Don't forget the popular freestanding implementation for Microsoft
Windows programs called "GUI mode". I'm willing to be that there are
quite a few C programmers who've had to develop for that freestanding
environment at one time or another. I have (and the memory is seared
into my brain).

There the program's initial function is called "WinMain", and it has
parameters that are quite different from main's.

--
Michael Wojcik (e-mail address removed)

The lark is exclusively a Soviet bird. The lark does not like the
other countries, and lets its harmonious song be heard only over the
fields made fertile by the collective labor of the citizens of the
happy land of the Soviets. -- D. Bleiman
 
D

Dan Pop

In said:
I am going to beat this horse until I think it's dead. The following
compiles for me. Is it ANSI compliant?

Only if you use a broken compiler.
#include <stdio.h>

int main(int argc, char* [])
{

return 0;
}

fangorn:~/tmp 218> gcc test.c
test.c: In function `main':
test.c:3: error: parameter name omitted
So am I to think of the main call as being like any other C function call?

The *initial* call of main(), that comes from outside your C code is
special. It must work no matter which of the two supported definitions
of main is used and, if main() returns, the program termination sequence
must be initiated. If the program calls main() itself, these calls are
perfectly ordinary: they must match the actual definition of main() and
returning from main() has no special semantics.

Dan
 
K

Keith Thompson

Only if you use a broken compiler.
[snip]

<NITPICK>
Dan, I don't think that's what you meant. The brokenness of any
compiler has no bearing on whether a particular piece of code is ANSI
compliant.
</NITPICK>
 
M

Merrill & Michele

I think it's important to declare when a question has been adequately
discussed, as far as the questioner is concerned. Tomorrow I will post a
new question with the subject: elementary construction +1 . MPJ
 
K

Keith Thompson

Merrill & Michele said:
I think it's important to declare when a question has been adequately
discussed, as far as the questioner is concerned.
[...]

That's fine, but if you're going to do that you should post a response
on the thread in question, without changing the subject header. It's
hard to tell which thread you're talking about.
 
D

Derrick Coetzee

Of course, if you
want to confuse people reading your program you can give argc and
argv different names - for maximum effect exchange their names;-)

For a different sort of evil, you can use typedefs to pretend to change
their types:

main.h:
typedef int f1oat;
typedef char *doub1e[];

main.c:
#include "main.h"
f1oat main(f1oat argc, doub1e argv) { }

Perfectly valid ANSI C!
 
D

Derrick Coetzee

Derrick said:
main.c:
#include "main.h"
f1oat main(f1oat argc, doub1e argv) { }

Or, just to add a bit to the illusion:
f1oat main(f1oat argc, doub1e argv) {
return 0.0f;
}
 
M

Mark F. Haigh

Merrill & Michele said:
I think it's important to declare when a question has been adequately
discussed, as far as the questioner is concerned. Tomorrow I will post a
new question with the subject: elementary construction +1 . MPJ

You don't seem to be the one donating your time to answer people's
questions, do you? You seem to be one of the people asking them.

So how about you worry about you, and we'll worry about ourselves.
We'll discuss it until we feel like stopping. When everybody stops,
the thread will die. You haven't been around Usenet very long, have
you?


Mark F. Haigh
(e-mail address removed)
 
M

Merrill & Michele

snip

My point was that any further discussion was going to occur without the
asker's attention. You are correct to think I haven't been around Usenet
long. In this short time I have serially identified threads that would have
been best chopped off and started new with a different emphasis. MPJ
 
D

Dan Pop

In said:
Of course, if you
want to confuse people reading your program you can give argc and
argv different names - for maximum effect exchange their names;-)

For a different sort of evil, you can use typedefs to pretend to change
their types:

main.h:
typedef int f1oat;
typedef char *doub1e[];

main.c:
#include "main.h"
f1oat main(f1oat argc, doub1e argv) { }

Perfectly valid ANSI C!

It's a lot worse than that. The following is also a perfectly valid
standard C program:

main.h:
#define float int
#define double char **

main.c
#include "main.h"

float main(float argc, double argv)
{
return 0.0;
}

Because of this, the function-like macros from the standard headers cannot
use the normal C keywords. The C standard only requires the C keywords
not to be redefined as macros when the standard headers are included.
After that, C keywords are no longer taboo as names for preprocessor
macros. Sheer insanity, IMHO.

Dan
 
M

Mark McIntyre

My point was that any further discussion was going to occur without the
asker's attention.

Commonplace - usenet is a discussion forum, not a one-to-one chatroom, and
inevitably threads take on a life independent of their originator.
You are correct to think I haven't been around Usenet long.

Something else you may want to learn about is that its generally considered
useful to leave some context in your posts. Otherwise they're meaningless.
DO NOT assume your readers have read the entire thread, or even have access
to it.
In this short time I have serially identified threads that would have
been best chopped off and started new with a different emphasis.

Its called topic-drift, it happens, no point worrying.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top