Connecting Tech Pros Worldwide Forums | Help | Site Map

Is main a registered word

small TUX
Guest
 
Posts: n/a
#1: Dec 8 '06
Hello programmers;
I joinded today itself. Can anyone say
about the word main, is it a registered word, justify your question
also. I would like to point about one thing that we can make variables
with name main.
Thanks.


Eric Sosman
Guest
 
Posts: n/a
#2: Dec 8 '06

re: Is main a registered word


small TUX wrote:
Quote:
Hello programmers;
I joinded today itself. Can anyone say
about the word main, is it a registered word, justify your question
also. I would like to point about one thing that we can make variables
with name main.
`main' is not a keyword, and `main' is not a reserved
identifier. It is possible (although perverse) to use `main'
as the name of a macro, or of a variable, or of a struct or
union tag, or ...

#include <stdio.h>
int main(void) {
const char *main = "world";
printf ("Hello, %s!\n", main);
#define main 0
return main;
}

(Not recommended.)

--
Eric Sosman
esosman@acm-dot-org.invalid
Simias
Guest
 
Posts: n/a
#3: Dec 8 '06

re: Is main a registered word


Eric Sosman <esosman@acm-dot-org.invalidwrites:
Quote:
small TUX wrote:
Quote:
>Hello programmers;
> I joinded today itself. Can anyone say
>about the word main, is it a registered word, justify your question
>also. I would like to point about one thing that we can make variables
>with name main.
>
`main' is not a keyword, and `main' is not a reserved
identifier. It is possible (although perverse) to use `main'
as the name of a macro, or of a variable, or of a struct or
union tag, or ...
>
#include <stdio.h>
int main(void) {
const char *main = "world";
printf ("Hello, %s!\n", main);
#define main 0
return main;
}
>
(Not recommended.)
And what about calling main as a regular function?
e.g.:

#include <stdio.h>

int main(void)
{
printf("hello");
main();
return (0);
}

It makes an infinite recursion with gcc, but i think i've heard that you
can't call main, so is this an undefined behaviour?

--
Simias
email rot13-ified
Richard Tobin
Guest
 
Posts: n/a
#4: Dec 8 '06

re: Is main a registered word


In article <86odqecxsp.fsf@simias.hd.free.fr>,
Simias <fvzvnf.a@tznvy.pbzwrote:
Quote:
>It makes an infinite recursion with gcc, but i think i've heard that you
>can't call main, so is this an undefined behaviour?
No, it's perfectly legal to call main().

I can't think of any cases where it wouldn't be clearer to instead
call another function though. main()'s arguments are intended to be
convenient for accessing command line arguments, and that's unlikely
to be a useful internal interface.

-- Richard




--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Eric Sosman
Guest
 
Posts: n/a
#5: Dec 8 '06

re: Is main a registered word


Simias wrote:
Quote:
[...]
And what about calling main as a regular function?
e.g.:
>
#include <stdio.h>
>
int main(void)
{
printf("hello");
main();
return (0);
}
>
It makes an infinite recursion with gcc, but i think i've heard that you
can't call main, so is this an undefined behaviour?
The sample you show is valid C, but it expresses a program
that does not terminate in any normal fashion. It is likely to
exceed an implementation limit (two limits, actually, but the
problems of generating an infinitely long line of text aren't
immediately relevant to your question) and terminate abnormally.

It is possible to call main recursively, if (as with any
other recursive function) you arrange for the recursion to
"bottom out" eventually. Here is a silly program that prints
its command-line arguments in reverse order:

#include <stdio.h>
int main(int argc, char **argv) {
if (argc 0) {
main (argc-1, argv+1);
puts (*argv);
}
return 0;
}

--
Eric Sosman
esosman@acm-dot-org.invalid
mark_bluemel@pobox.com
Guest
 
Posts: n/a
#6: Dec 8 '06

re: Is main a registered word



small TUX wrote:
Quote:
Hello programmers;
I joinded today itself.
(UK readers, can you hear Bluebottle saying that as well?)
Quote:
Can anyone say
about the word main, is it a registered word,
Yes of course someone can and the answer is no it isn't.

http://c-faq.com/ansi/avail.html led me to the draft ANSI C89 standard
document which explains that on a "hosted environment" the function
called at program startup is called "main", it returns an int value and
either takes no parameters or 2 parameters (it also defines what these
parameters are).

So "main" is just a function name which has a specific use in hosted
environments. Nothing more, nothing less.
Quote:
justify your question also.
I don't have a question, so why do I need to justify it?

Thad Smith
Guest
 
Posts: n/a
#7: Dec 8 '06

re: Is main a registered word


mark_bluemel@pobox.com wrote:
Quote:
http://c-faq.com/ansi/avail.html led me to the draft ANSI C89 standard
document which explains that on a "hosted environment" the function
called at program startup is called "main", it returns an int value and
either takes no parameters or 2 parameters (it also defines what these
parameters are).
>
So "main" is just a function name which has a specific use in hosted
environments. Nothing more, nothing less.
In C99, executing to the closing brace of main is the equivalent of
return 0.

--
Thad
Simias
Guest
 
Posts: n/a
#8: Dec 8 '06

re: Is main a registered word


Thad Smith <ThadSmith@acm.orgwrites:
Quote:
mark_bluemel@pobox.com wrote:
In C99, executing to the closing brace of main is the equivalent of
return 0.
Even if it's a recursive call to main?

--
Simias
email rot13-ified
Keith Thompson
Guest
 
Posts: n/a
#9: Dec 8 '06

re: Is main a registered word


Simias <fvzvnf.a@tznvy.pbzwrites:
Quote:
Thad Smith <ThadSmith@acm.orgwrites:
Quote:
>mark_bluemel@pobox.com wrote:
>In C99, executing to the closing brace of main is the equivalent of
>return 0.
>
Even if it's a recursive call to main?
I think so.

C99 5.1.2.2.3 says:

If the return type of the main function is a type compatible with
int, a return from the initial call to the main function is
equivalent to calling the exit function with the value returned by
the main function as its argument; reaching the } that terminates
the main function returns a value of 0.

The way it's phrased, I think the last clause refers to any call to
main, not just the initial call.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
santosh
Guest
 
Posts: n/a
#10: Dec 8 '06

re: Is main a registered word


Keith Thompson wrote:
Quote:
Simias <fvzvnf.a@tznvy.pbzwrites:
Quote:
Thad Smith <ThadSmith@acm.orgwrites:
Quote:
mark_bluemel@pobox.com wrote:
In C99, executing to the closing brace of main is the equivalent of
return 0.
Even if it's a recursive call to main?
>
I think so.
>
C99 5.1.2.2.3 says:
>
If the return type of the main function is a type compatible with
int, a return from the initial call to the main function is
equivalent to calling the exit function with the value returned by
the main function as its argument; reaching the } that terminates
the main function returns a value of 0.
>
The way it's phrased, I think the last clause refers to any call to
main, not just the initial call.
It seems to contradict the preceding statement. Anyhow, what if the
code returns a value other than zero, say EXIT_FAILURE?

I suppose the last statement is meant for the situation where main() is
prototyped as returning an int value, but the code itself fails to
return an explicit value, terminating by just a return.

Harald van Dijk
Guest
 
Posts: n/a
#11: Dec 8 '06

re: Is main a registered word


Keith Thompson wrote:
Quote:
Simias <fvzvnf.a@tznvy.pbzwrites:
Quote:
Thad Smith <ThadSmith@acm.orgwrites:
Quote:
mark_bluemel@pobox.com wrote:
In C99, executing to the closing brace of main is the equivalent of
return 0.
Even if it's a recursive call to main?
>
I think so.
>
C99 5.1.2.2.3 says:
>
If the return type of the main function is a type compatible with
int, a return from the initial call to the main function is
equivalent to calling the exit function with the value returned by
the main function as its argument; reaching the } that terminates
the main function returns a value of 0.
>
The way it's phrased, I think the last clause refers to any call to
main, not just the initial call.
On the other hand, the way it's phrased (without the word "and" before
"reaching"), it also seems to refer to any call to main() regardless of
the return type. That is, if an implementation documents void(void) as
one of the implementation-defined allowed types for main(), the
standard seems to state

void main(void) {
}

is equivalent to

void main(void) {
return 0;
}

which then of course requires a diagnostic. I doubt this is intended;
it makes far more sense for the last clause to refer to less than
suggested by the current wording. How much less is anyone's guess.

Richard Tobin
Guest
 
Posts: n/a
#12: Dec 8 '06

re: Is main a registered word


In article <1165594037.776960.120100@n67g2000cwd.googlegroups .com>,
santosh <santosh.k83@gmail.comwrote:
Quote:
Quote:
> If the return type of the main function is a type compatible with
> int, a return from the initial call to the main function is
> equivalent to calling the exit function with the value returned by
> the main function as its argument; reaching the } that terminates
> the main function returns a value of 0.
>>
>The way it's phrased, I think the last clause refers to any call to
>main, not just the initial call.
Quote:
>It seems to contradict the preceding statement. Anyhow, what if the
>code returns a value other than zero, say EXIT_FAILURE?
Then it doesn't reach the }.
Quote:
>I suppose the last statement is meant for the situation where main() is
>prototyped as returning an int value, but the code itself fails to
>return an explicit value, terminating by just a return.
It's meant for the case where it doesn't reach a return statement at
all, but reaches the } at the end of the function. On the other hand
this presumably does something undefined:

int main(void)
{
return;
}

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Random832
Guest
 
Posts: n/a
#13: Dec 8 '06

re: Is main a registered word


2006-12-08 <1165594037.776960.120100@n67g2000cwd.googlegroups .com>,
santosh wrote:
Quote:
Keith Thompson wrote:
Quote:
>Simias <fvzvnf.a@tznvy.pbzwrites:
Quote:
Thad Smith <ThadSmith@acm.orgwrites:
>mark_bluemel@pobox.com wrote:
>In C99, executing to the closing brace of main is the equivalent of
>return 0.
>
Even if it's a recursive call to main?
>>
>I think so.
>>
>C99 5.1.2.2.3 says:
>>
> If the return type of the main function is a type compatible with
> int, a return from the initial call to the main function is
> equivalent to calling the exit function with the value returned by
> the main function as its argument; reaching the } that terminates
> the main function returns a value of 0.
>>
>The way it's phrased, I think the last clause refers to any call to
>main, not just the initial call.
>
It seems to contradict the preceding statement. Anyhow, what if the
code returns a value other than zero, say EXIT_FAILURE?
Execution paths that reach a return statement will not reach the } that
terminates the function.
dcorbit@connx.com
Guest
 
Posts: n/a
#14: Dec 8 '06

re: Is main a registered word



small TUX wrote:
Quote:
Hello programmers;
I joinded today itself. Can anyone say
about the word main, is it a registered word, justify your question
also. I would like to point about one thing that we can make variables
with name main.
Thanks.
It is possible (in some implementations) to get yourself into trouble
with this program:

C:\tmp>type foo.c
int main;

Or even this:
C:\tmp>type bar.c
main;

because it will create a public variable with signature
_main

Though most modern implementations won't try to execute a constant and
dump core.

Peter Nilsson
Guest
 
Posts: n/a
#15: Dec 8 '06

re: Is main a registered word


Simias wrote:
Quote:
Eric Sosman <esosman@acm-dot-org.invalidwrites:
Quote:

`main' is not a keyword, and `main' is not a reserved
identifier. ...
>
And what about calling main as a regular function?
Allowed in C.
Quote:
... i think i've heard that you can't call main, so is this an
undefined behaviour?
It's not allowed in C++.

--
Peter

Keith Thompson
Guest
 
Posts: n/a
#16: Dec 8 '06

re: Is main a registered word


"santosh" <santosh.k83@gmail.comwrites:
Quote:
Keith Thompson wrote:
Quote:
>Simias <fvzvnf.a@tznvy.pbzwrites:
Quote:
Thad Smith <ThadSmith@acm.orgwrites:
>mark_bluemel@pobox.com wrote:
>In C99, executing to the closing brace of main is the equivalent of
>return 0.
>
Even if it's a recursive call to main?
>>
>I think so.
>>
>C99 5.1.2.2.3 says:
>>
> If the return type of the main function is a type compatible with
> int, a return from the initial call to the main function is
> equivalent to calling the exit function with the value returned by
> the main function as its argument; reaching the } that terminates
> the main function returns a value of 0.
>>
>The way it's phrased, I think the last clause refers to any call to
>main, not just the initial call.
>
It seems to contradict the preceding statement. Anyhow, what if the
code returns a value other than zero, say EXIT_FAILURE?
>
I suppose the last statement is meant for the situation where main() is
prototyped as returning an int value, but the code itself fails to
return an explicit value, terminating by just a return.
The first clause, before the ';', says that returning a value from
main() is equivalent to calling exit() with that same value. This
applies only to the initial call, and only if main's return type is
compatible with int.

The second call, after the ';', says that reaching the '}' at the end
of main() is equivalent to returning a value of 0.

I don't see a contradiction, but I do seem an ambiguity. As Harald
pointed out, there are two conditions stated in the first clause: that
the return type is compatible with int, and that the call is the
initial call. The first condition (return type compatible with int)
logically *should* apply to the second clause (falling off the end
returns 0). The second condition (initial call) logically may or may
not. The way the sentence is structured, I don't think that *either*
condition applies to the second clause -- but if that were the intent,
it would have made more sense to write two sentences rather than using
a semicolon.

It doesn't matter much to me as a programmer; I have no intention of
taking advantage of the ability to fall off the end of main() without
an explicit exit() or return statement. And an implementer can meet
the requirements, whatever they may be, by always returning 0 when a
program reaches the closing "}" of main(), whether it's the initial
call or not (that's probably the easiest thing to do anyway). But it
would be nice to know exactly what's intended.

I'll post to comp.std.c.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson
Guest
 
Posts: n/a
#17: Dec 8 '06

re: Is main a registered word


dcorbit@connx.com writes:
Quote:
small TUX wrote:
Quote:
>Hello programmers;
> I joinded today itself. Can anyone say
>about the word main, is it a registered word, justify your question
>also. I would like to point about one thing that we can make variables
>with name main.
> Thanks.
>
It is possible (in some implementations) to get yourself into trouble
with this program:
>
C:\tmp>type foo.c
int main;
>
Or even this:
C:\tmp>type bar.c
main;
>
because it will create a public variable with signature
_main
>
Though most modern implementations won't try to execute a constant and
dump core.
The grand prize winner of the first IOCCC (International Obfuscated C
Code Contest), back in 1984, declared "main" as an array rather than
as a function. (The content of the array was machine code that could
be executed either on a PDP-11 or on a VAX.) The implementations of
the time happily executed the array. It resulted in a rule change for
the following year.

<http://www0.us.ioccc.org/years.html#1984>, "mullender".

Here's the actual program (I accept no responsibility for the
consequences if you try to run it):

short main[] = {
277, 04735, -4129, 25, 0, 477, 1019, 0xbef, 0, 12800,
-113, 21119, 0x52d7, -1006, -7151, 0, 0x4bc, 020004,
14880, 10541, 2056, 04010, 4548, 3044, -6716, 0x9,
4407, 6, 5568, 1, -30460, 0, 0x9, 5570, 512, -30419,
0x7e82, 0760, 6, 0, 4, 02400, 15, 0, 4, 1280, 4, 0,
4, 0, 0, 0, 0x8, 0, 4, 0, ',', 0, 12, 0, 4, 0, '#',
0, 020, 0, 4, 0, 30, 0, 026, 0, 0x6176, 120, 25712,
'p', 072163, 'r', 29303, 29801, 'e'
};

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson
Guest
 
Posts: n/a
#18: Dec 8 '06

re: Is main a registered word


"santosh" <santosh.k83@gmail.comwrites:
[...]
Quote:
I suppose the last statement is meant for the situation where main() is
prototyped as returning an int value, but the code itself fails to
return an explicit value, terminating by just a return.
I forgot to mention: A return statement without an expression in a
non-void function, or a return statement with an expression in a void
function, is a constraint violation. (That's in C99; I think the
rules were looser in C90.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dik T. Winter
Guest
 
Posts: n/a
#19: Dec 8 '06

re: Is main a registered word


In article <lnzm9ykmv4.fsf@nuthaus.mib.orgKeith Thompson <kst-u@mib.orgwrites:
Quote:
"santosh" <santosh.k83@gmail.comwrites:
[...]
Quote:
I suppose the last statement is meant for the situation where main() is
prototyped as returning an int value, but the code itself fails to
return an explicit value, terminating by just a return.
>
I forgot to mention: A return statement without an expression in a
non-void function, or a return statement with an expression in a void
function, is a constraint violation. (That's in C99; I think the
rules were looser in C90.)
Indeed, C90 allows a return without value in a non-void function.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mark McIntyre
Guest
 
Posts: n/a
#20: Dec 9 '06

re: Is main a registered word


On 8 Dec 2006 05:15:41 -0800, in comp.lang.c , "small TUX"
<parasuprime@yahoo.comwrote:
Quote:
I joinded today itself. Can anyone say
>about the word main, is it a registered word, justify your question
you mean justify the answer.
this sounds like homework. You probably need to do that yourself.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Richard Bos
Guest
 
Posts: n/a
#21: Dec 11 '06

re: Is main a registered word


Mark McIntyre <markmcintyre@spamcop.netwrote:
Quote:
On 8 Dec 2006 05:15:41 -0800, in comp.lang.c , "small TUX"
<parasuprime@yahoo.comwrote:
>
Quote:
I joinded today itself. Can anyone say
about the word main, is it a registered word, justify your question
>
you mean justify the answer.
No...
Quote:
this sounds like homework.
There, you've just justified the question!

Richard
Closed Thread