Is main a registered word

S

small TUX

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.
 
E

Eric Sosman

small said:
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.)
 
S

Simias

Eric Sosman said:
`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?
 
R

Richard Tobin

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
 
E

Eric Sosman

Simias said:
[...]
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;
}
 
M

mark_bluemel

small said:
Hello programmers;
I joinded today itself.

(UK readers, can you hear Bluebottle saying that as well?)
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.
justify your question also.

I don't have a question, so why do I need to justify it?
 
S

Simias

Thad Smith said:
(e-mail address removed) 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?
 
T

Thad Smith

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.
 
K

Keith Thompson

Simias said:
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.
 
S

santosh

Keith said:
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.
 
R

Random832

2006-12-08 said:
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.
 
R

Richard Tobin

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 }.
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
 
G

Guest

Keith said:
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.
 
D

dcorbit

small said:
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.
 
K

Keith Thompson

santosh said:
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.
 
K

Keith Thompson

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'
};
 
K

Keith Thompson

santosh said:
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.)
 
D

Dik T. Winter

>
> 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.
 
M

Mark McIntyre

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
 

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,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top