main function

J

junky_fellow

I was reading the various posts in this newsgroup, and found
that main function should be declared as
int main(void) // if no arguments are passed to main

i wanted to know whats the purpose of return value int ?
where does main return ? As far as i know it is the first function
to be given control. So where does this first function return ?

If it returns, some function then how does this return value interpreted
by that function ?

thanx in advance for any help ...
 
J

Jens.Toerring

junky_fellow said:
I was reading the various posts in this newsgroup, and found
that main function should be declared as
int main(void) // if no arguments are passed to main
i wanted to know whats the purpose of return value int ?
where does main return ? As far as i know it is the first function
to be given control. So where does this first function return ?

Typically, a program has a (very system-dependent) startup function,
which initializes everything for the program and then calls main().
To this function main() returns and that function passes the return
value back to the operating system. That in turn gives back that
value (often after adding some additional bits of information) to
whatever invoked the program, which then can use it to check if the
program exited successfully or failed.
If it returns, some function then how does this return value interpreted
by that function ?

E.g. under Unix it's a very common practice that within a shell script
a program is started and afterwards its return value is checked to
determine how to proceed. An (admittedly stupid) way to check for the
existence of a file would be to call 'ls' (a program that lists files
in a directory) with the name of the file as its argument. When it
returns you can check the return value to find out if the file exists
- if it does the 'ls' program returned a value indicating success,
otherwise a value indicating failure. Using the return value is much
simpler than having the script read and interpret the output of the
'ls' program.
Regards, Jens
 
E

Emmanuel Delahaye

junky_fellow wrote on 13/08/04 :
I was reading the various posts in this newsgroup, and found
that main function should be declared as
int main(void) // if no arguments are passed to main
Correct.

i wanted to know whats the purpose of return value int ?
where does main return ? As far as i know it is the first function
to be given control. So where does this first function return ?

main() and exit() return a value to the system.
If it returns, some function then how does this return value interpreted
by that function ?

Yes. This value can be interpreted by the current command interpreter
(shell, bash, command.com or whatever), specifically in scripts (or
batch etc.) to act on decisions (if ...).
 
S

suri

Typically, a program has a (very system-dependent) startup function,
which initializes everything for the program and then calls main().
To this function main() returns and that function passes the return
value back to the operating system. That in turn gives back that
value (often after adding some additional bits of information) to
whatever invoked the program, which then can use it to check if the
program exited successfully or failed.


E.g. under Unix it's a very common practice that within a shell script
a program is started and afterwards its return value is checked to
determine how to proceed. An (admittedly stupid) way to check for the
existence of a file would be to call 'ls' (a program that lists files
in a directory) with the name of the file as its argument. When it
returns you can check the return value to find out if the file exists
- if it does the 'ls' program returned a value indicating success,
otherwise a value indicating failure. Using the return value is much
simpler than having the script read and interpret the output of the
'ls' program.
Regards, Jens


I used linux and there are some commands, to check the exit status of
the previous programs, In that case the return value from main will be
Used to check wheater the program is successful or not
 
K

Kenny McCormack

junky_fellow wrote on 13/08/04 :

Correct.

Not really.

I'm surprised that no one has pointed out that this fragment (pick one or
more of the following, as you see fit):
1) Isn't C (*)
2) Doesnt't compile in c.l.c (**)
3) Is OT here.

(*) comp.lang.c++ is down the hall.
(**) Might compile in C99 - but we're mostly C89 around here.
 
K

Kieran Simkin

Kenny McCormack said:
Not really.

I'm surprised that no one has pointed out that this fragment (pick one or
more of the following, as you see fit):
1) Isn't C (*)

Please explain how this isn't C, I don't understand what makes this
definition C++ or C99 rather than C(89).
 
R

Rich Grise

Kieran said:
Please explain how this isn't C, I don't understand what makes this
definition C++ or C99 rather than C(89).

You've snipped the very part you're asking about. That's really stupid.
But, since I'm so fucking wonderful, I'll do your homework for you:

---<quote>---
int main(void)  // if no arguments are passed to main
---<endquote>---

It's not C because '//' is a syntax error.

Now, please be kind enough to go read something.

HTH!
Rich
 
K

Keith Thompson

Not really.

I'm surprised that no one has pointed out that this fragment (pick one or
more of the following, as you see fit):
1) Isn't C (*)
2) Doesnt't compile in c.l.c (**)
3) Is OT here.

(*) comp.lang.c++ is down the hall.
(**) Might compile in C99 - but we're mostly C89 around here.

Kenny's complaint is that "//" comments are not supported in ANSI C89
(or, equivalently, in ISO C90). They are supported in ISO C99 (and in
C++, but that's beside the point).

It's certainly worth pointing out that "//" comments are not supported
in what is still the most commonly implemented C standard. It is
absurd, however, to claim that they aren't C.

We frequently discuss C99 here. We also discuss C90, and K&R C, and
sometimes even the versions that preceded the publication of K&R1.
 
K

Keith Thompson

Rich Grise said:
You've snipped the very part you're asking about. That's really stupid.
But, since I'm so ****ing wonderful, I'll do your homework for you:

---<quote>---
int main(void)  // if no arguments are passed to main
---<endquote>---

It's not C because '//' is a syntax error.

Now, please be kind enough to go read something.

HTH!
Rich

[Quotation edited for content.]

Rich, Kieran's question was perfectly reasonable, and your response
was rude and incomplete (and arguably incorrect).

"//" comments are illegal in C90, but legal in C99. That's certainly
worth pointing out, and there are other valid reasons not to use "//"
comments when posting to comp.lang.c (line wrapping can cause
problems), but claiming that it's "not C" is misleading at best.

I'll also point out that Kieran did not snip anything; he quoted the
entire article to which he was following up.

Rich, you may indeed be ****ing wonderful most of the time, but this
was not your finest moment. You might want to re-read your own
excellent article <[email protected]>,
posted to comp.lang.c about a year ago (2003-08-07).
 
E

E. Robert Tisdale

Emmanuel said:

No.

The C programming language does *not* specify
how function main is to be called.
Generally, programmers have no control
over what arguments are *passed* to main.
The programmer decides only which arguments main will *accept*.

My C++ programs call

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

Other C++ compilers must, at least, emit code to call

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

When you define

int main(void);

all you are telling the compiler is that
main will ignore all of the arguments passed to it.
 
M

Martin Ambuhl

Kenny said:
Not really.

I'm surprised that no one has pointed out that this fragment (pick one or
more of the following, as you see fit):
1) Isn't C (*)
2) Doesnt't compile in c.l.c (**)
3) Is OT here.

I'm more surprised that someone would be so ignorant as to claim that C
code isn't C, doesn't compile, and is off-topic.
(*) comp.lang.c++ is down the hall.
(**) Might compile in C99 - but we're mostly C89 around here.

The current standard defines the current standard. CLC has historically
allowed legacy "standards." After C89 was adopted, K&R C continued to
be considered topical. Now that C99 has been adopted, C89 (C90)
continues to be considered topical. Only a damn fool would consider
questions about code written according to the current C standard as
off-topic.
 
M

Martin Ambuhl

Rich said:
Kieran Simkin wrote:




You've snipped the very part you're asking about. That's really stupid.
But, since I'm so fucking wonderful, I'll do your homework for you:

---<quote>---
int main(void) // if no arguments are passed to main
---<endquote>---

It's not C because '//' is a syntax error.

No, it isn't. Where did the trolls Kenny McCormack and Rich Grise come
from?
Now, please be kind enough to go read something.

Good advise. Follow it.
 
J

junky_fellow

Typically, a program has a (very system-dependent) startup function,
which initializes everything for the program and then calls main().
To this function main() returns and that function passes the return
value back to the operating system. That in turn gives back that
value (often after adding some additional bits of information) to
whatever invoked the program, which then can use it to check if the
program exited successfully or failed.


E.g. under Unix it's a very common practice that within a shell script
a program is started and afterwards its return value is checked to
determine how to proceed. An (admittedly stupid) way to check for the
existence of a file would be to call 'ls' (a program that lists files
in a directory) with the name of the file as its argument. When it
returns you can check the return value to find out if the file exists
- if it does the 'ls' program returned a value indicating success,
otherwise a value indicating failure. Using the return value is much
simpler than having the script read and interpret the output of the
'ls' program.
Regards, Jens

thank you all who devoted their precious time to answer the stupid questions
of a "c learner".
 
K

Keith Thompson

thank you all who devoted their precious time to answer the stupid
questions of a "c learner".

Contrary to popular belief, there *are* stupid questions (I've seen
some in this newsgroup), but yours wasn't one of them.
 
R

Rich Grise

Keith said:
Rich Grise <[email protected]> writes:
Rich, Kieran's question was perfectly reasonable, and your response
was rude and incomplete (and arguably incorrect).

Yes, I've since been informed of my error.
"//" comments are illegal in C90, but legal in C99. That's certainly
worth pointing out, and there are other valid reasons not to use "//"
comments when posting to comp.lang.c (line wrapping can cause
problems), but claiming that it's "not C" is misleading at best.

I apologize for that.
I'll also point out that Kieran did not snip anything; he quoted the
entire article to which he was following up.

I'm not sure what this means.
Rich, you may indeed be ****ing wonderful most of the time, but this
was not your finest moment. You might want to re-read your own
excellent article <[email protected]>,
posted to comp.lang.c about a year ago (2003-08-07).

I stand chastised. It won't happen again.

Thanks,
Rich
 
K

Keith Thompson

Rich Grise said:
I'm not sure what this means.

In your response to Kieran, you wrote:

] You've snipped the very part you're asking about.

which was incorrect; Kieran didn't snip anything.

I can't speak for anyone else, but for myself: Apology accepted.
(Resisting the temptation to add "Captain Needa." :cool:} )
 

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,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top