473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is main a registered word

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.

Dec 8 '06 #1
20 1411
small TUX wrote:
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
es*****@acm-dot-org.invalid
Dec 8 '06 #2
Eric Sosman <es*****@acm-dot-org.invalidwrites:
small TUX wrote:
>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
Dec 8 '06 #3
In article <86************@simias.hd.free.fr>,
Simias <fv******@tznvy.pbzwrote:
>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.
Dec 8 '06 #4
Simias wrote:
[...]
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
es*****@acm-dot-org.invalid
Dec 8 '06 #5

small TUX wrote:
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?

Dec 8 '06 #6
ma**********@pobox.com wrote:
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
Dec 8 '06 #7
Thad Smith <Th*******@acm.orgwrites:
ma**********@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
Dec 8 '06 #8
Simias <fv******@tznvy.pbzwrites:
Thad Smith <Th*******@acm.orgwrites:
>ma**********@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) ks***@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.
Dec 8 '06 #9
Keith Thompson wrote:
Simias <fv******@tznvy.pbzwrites:
Thad Smith <Th*******@acm.orgwrites:
ma**********@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.

Dec 8 '06 #10
In article <11**********************@n67g2000cwd.googlegroups .com>,
santosh <sa*********@gmail.comwrote:
> 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?
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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 8 '06 #11
Keith Thompson wrote:
Simias <fv******@tznvy.pbzwrites:
Thad Smith <Th*******@acm.orgwrites:
ma**********@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.

Dec 8 '06 #12
2006-12-08 <11**********************@n67g2000cwd.googlegroups .com>,
santosh wrote:
Keith Thompson wrote:
>Simias <fv******@tznvy.pbzwrites:
Thad Smith <Th*******@acm.orgwrites:
ma**********@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.
Dec 8 '06 #13

small TUX wrote:
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.

Dec 8 '06 #14
Simias wrote:
Eric Sosman <es*****@acm-dot-org.invalidwrites:

`main' is not a keyword, and `main' is not a reserved
identifier. ...

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

--
Peter

Dec 8 '06 #15
"santosh" <sa*********@gmail.comwrites:
Keith Thompson wrote:
>Simias <fv******@tznvy.pbzwrites:
Thad Smith <Th*******@acm.orgwrites:
ma**********@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) ks***@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.
Dec 8 '06 #16
dc*****@connx.com writes:
small TUX wrote:
>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) ks***@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.
Dec 8 '06 #17
"santosh" <sa*********@gmail.comwrites:
[...]
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) ks***@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.
Dec 8 '06 #18
In article <ln************@nuthaus.mib.orgKeith Thompson <ks***@mib.orgwrites:
"santosh" <sa*********@gmail.comwrites:
[...]
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/
Dec 8 '06 #19
On 8 Dec 2006 05:15:41 -0800, in comp.lang.c , "small TUX"
<pa*********@yahoo.comwrote:
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
Dec 9 '06 #20
Mark McIntyre <ma**********@spamcop.netwrote:
On 8 Dec 2006 05:15:41 -0800, in comp.lang.c , "small TUX"
<pa*********@yahoo.comwrote:
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...
this sounds like homework.
There, you've just justified the question!

Richard
Dec 11 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Steven T. Hatton | last post by:
The short sample program listed below has some features that I find to be bad style. In particular, they fail to communicate the connection between names used in this program and the location in...
2
by: anand | last post by:
Hi, We have developed a web application, which uses Interop.Word.dll to read from word documents and write to word documents. Problem is that our client is persisting that we develop the...
3
by: Hughsie | last post by:
Hi I have created an installation package for my app and I have installed the application onto another machine together with the framework and the correct MDAC version. The problem is when I...
0
by: ritesh.singhal | last post by:
Hi, I am trying to do server side office automation. I have a word template which I am trying to populate with data from SQL server but when I try to generate the Word document I get following...
2
by: Colin Halliday | last post by:
I have a Word 2003 mail merge main document (form letter) that is linked to another Word document data source for the mail merge. If I open this doc using the Word GUI, it first asks me to...
27
by: junky_fellow | last post by:
Guys, Can I return 0, from main() ? Is this equivalent to exit(EXIT_SUCCESS) ? thanks for any help...
2
by: significantBit | last post by:
n00b here. Just started learning C a couple of days ago. I'm using xcode alternatively with emacs. My question is in regards to the main function. Everytime I create a project (standard...
17
by: mariz | last post by:
hi , i m a new member to this group . i start from the beginig - main(). Can any one give me an idea for how to write a program in C without using main() ? ie while looking source code it wont...
11
by: Rahul | last post by:
Hi Everyone, I have seen code in different styles like main(argc,argv) int argc; char **argv; { if(argc != 2) exit(1); else exit(0);
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.