473,657 Members | 3,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

main function

Hi all,

I have heard many discussions among my colleagues that main is a user
defined function or not.

arguments in favour:-

1) if it is built in function it must be defined in some header file
but it is n't.

2) every time we are defining how the main function should work

arguments against:-

1)user defined functions cannot be called from command line

some say that it is a special user defined function ,which is the
starting point of execution of a C program.

now questions are

1) What exactly is the correct answer..????
2) in free standing implementations is main the starting point
of execution..????
Nov 29 '07 #1
11 3561
aa*****@gmail.c om wrote:
Hi all,

I have heard many discussions among my colleagues that main is a user
defined function or not.

arguments in favour:-

1) if it is built in function it must be defined in some header file
but it is n't.
Declared in a header, defined in a library.
2) every time we are defining how the main function should work

arguments against:-

1)user defined functions cannot be called from command line
Nor can any other, including main.
some say that it is a special user defined function ,which is the
starting point of execution of a C program.

now questions are

1) What exactly is the correct answer..????
The standard specifies main as the name of the function called at
program startup. There isn't a prototype in any headers because the
function can have one of two.

2) in free standing implementations is main the starting point
of execution..????
In a freestanding environment, the he name of the function called at
program startup is implementation defined.

--
Ian Collins.
Nov 29 '07 #2
aa*****@gmail.c om wrote:
Hi all,

I have heard many discussions among my colleagues that main is a user
defined function or not.
What does the language specification say?

I suggest you get a copy of the standard (or the draft standard which is
available free) - see the FAQ for some pointers.

A lot of the questions you seem to be asking would be answered simply
by reading the standard rather than swapping opinions. In this case,
section 5.1.2 seems relevant (I'm using the C99 draft PDF).
arguments in favour:-

1) if it is built in function it must be defined in some header file
but it is n't.
There isn't, as far as I'm aware, any definition of "built in function"
in the C language specification. There are functions which an
implementation is required to provide and "main" isn't one of them.
2) every time we are defining how the main function should work
True enough. Though the standard defines what its arguments must be.
arguments against:-
1)user defined functions cannot be called from command line

some say that it is a special user defined function ,which is the
starting point of execution of a C program.

now questions are

1) What exactly is the correct answer..????
What does the standard say?
2) in free standing implementations is main the starting point
of execution..????
That depends on the implementation. The standard only mandates main for
hosted implementations .
Nov 29 '07 #3
aa*****@gmail.c om wrote:
Hi all,

I have heard many discussions among my colleagues that main is a user
defined function or not.
Why? Did you get tired of counting the dancing angels on the needle?
Nov 29 '07 #4
aa*****@gmail.c om wrote:
Hi all,

I have heard many discussions among my colleagues that main is a user
defined function or not.
The answer to that obviously depends on exactly what you mean by "a user
defined function". C doesn't define the term, so its up to you and your
colleagues to define what you mean by it; once you've done that, you'll
know whether or not main is one.
Nov 29 '07 #5
On Thu, 29 Nov 2007 02:26:11 -0800 (PST), aa*****@gmail.c om wrote:
>Hi all,

I have heard many discussions among my colleagues that main is a user
defined function or not.
More exactly, the definition of main is user-defined. Its prototype is
not declared by the implementation, and it follows that you--the
user--must not declare its prototype.

Your definition of main must conform to the C Standard, which
specifies the following acceptable choices:

int main(void) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }

One of the most important things to notice about these definitions is
that main returns a type of int. Defining main to return any type
other than int is considered to be undefined behavior; some compilers
gladly accept a definition of main that defines a return type other
than int, such as void, but you should avoid using such a definition,
for the following reasons: 1) a return type other than int (e.g.,
void) is considered undefined behavior because it does not conform to
the C Standard, and 2) those same compilers will gladly accept a
return type of int, which does conform to the C Standard.

After you have defined your main function to return a type of int,
another important thing to keep in mind is that the actual value of
the return type can take on only a few specific values. These values
are 0, EXIT_SUCCESS and EXIT_FAILURE. Period. The latter two values
are macros defined in the Standard C header file <stdlib.h>. Returning
a value of 0 has the same effect as returning a value of EXIT_SUCCESS,
as far as the C Standard is concerned.

Whether or not you choose to return 0 or EXIT_SUCCESS from main is a
matter of preference and perhaps style. My preference is to return 0
if I never return an error status (i.e., EXIT_FAILURE) and return
EXIT_SUCCESS if I do return an error status. One benefit of this is
that for simple "Hello World!" programs, I can return 0 and do not
have to include <stdlib.h>. For example:

#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}

If I return EXIT_FAILURE somewhere, someplace, then my (pseudo)code
might look something like this:

#include <stdlib.h>
int main(void)
{
if ( /* some expression */ )
{
/* error */
return EXIT_FAILURE;
}
else
{
/* success */
return EXIT_SUCCESS;
}
}

One final note about the function main. Sometimes its return type of
int simply does not make sense. Most if not all embedded software
written in C has a main function that looks something like this:

int main(void)
{
/* initialize stuff */
for ( ; ; )
{
/* do something */
}
return 0;
}

As long as the "do something" part does not contain a break statement
(which is hardly if never the case), the return statement will never
be executed. In that sense, and in the sense that there is no
environment to which to return a value on many if not most embedded
applications, it doesn't really make sense to define main to return a
type of int, does it? Some compilers and source code analyzers will
flag a warning about the return statement in the above example, saying
something to the extent that it is unreachable code. What really makes
the most sense in this instance is that the return type of main is
void, e.g.:

void main(void)
{
/* initialize stuff */
for ( ; ; )
{
/* do something */
}
}

If and when you ever run into such a situation, just make sure that
your compiler provides the option to define main with a return type of
void. Of course your code will be non-portable, but you should only be
using such compiler-specific options in an application domain such as
the embedded world, which is highly non-portable (though not totally
non-portable), especially for any non-trivial application.

Best regards
--
jay
>
arguments in favour:-

1) if it is built in function it must be defined in some header file
but it is n't.

2) every time we are defining how the main function should work

arguments against:-

1)user defined functions cannot be called from command line

some say that it is a special user defined function ,which is the
starting point of execution of a C program.

now questions are

1) What exactly is the correct answer..????
2) in free standing implementations is main the starting point
of execution..????
Nov 30 '07 #6
On Nov 30, 12:09 am, jaysome <jays...@hotmai l.comwrote:
More exactly, the definition of main is user-defined. Its prototype is
not declared by the implementation, and it follows that you--the
user--must not declare its prototype.

Why would there be a problem providing a prototype for main(), so long
as it matches one of the two acceptable forms, and matches the
eventual definition?
Nov 30 '07 #7
ro***********@y ahoo.com wrote:
On Nov 30, 12:09 am, jaysome <jays...@hotmai l.comwrote:
>More exactly, the definition of main is user-defined. Its prototype
is not declared by the implementation, and it follows that you--the
user--must not declare its prototype.

Why would there be a problem providing a prototype for main(), so long
as it matches one of the two acceptable forms, and matches the
eventual definition?
In fact if you use the "newer" style definition for writing main() you
automatically provide the prototype too. A separate prototype for
main() only rarely needed since main() is seldom called from other
functions.

Nov 30 '07 #8
aa*****@gmail.c om wrote:
Hi all,

I have heard many discussions among my colleagues that main is a user
defined function or not.

arguments in favour:-

1) if it is built in function it must be defined in some header file
but it is n't.
That argument doesn't hold up. Depending upon the implementation, there
might be quite a few functions that are not declared in standard
headers, but which are also not user defined. These might be hidden
helper functions that are called by routines from the C standard
library. These might be routines which are executed to emulate in
software C features for which native hardware support is lacking on a
particular platform, such as long-double _Complex arithmetic on a
platform which has native support only for 8-bit integer arithmetic.
2) every time we are defining how the main function should work
That is precisely the definition of "user defined", and the only
relevant argument you've cited.
arguments against:-

1)user defined functions cannot be called from command line
That's also true for main(). What you call from the command line is the
entire program, which eventually results in a call to main(); and later
on, it eventually may result in a call to any of the other user-defined
functions in the program.
Nov 30 '07 #9
jaysome wrote:
More exactly, the definition of main is user-defined. Its prototype is
not declared by the implementation, and it follows that you--the
user--must not declare its prototype.
Because its prototype is not declared by the implementation,
it follows that you--the user--may declare its prototype.

--
pete
Nov 30 '07 #10

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

Similar topics

6
4610
by: Manuel | last post by:
Can I invoke a function before main I could do it by invoking it in a Global object's constructor . Is there any other method other than this. Manuel
192
8888
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty programmer that you pretend to be, why don't you just write some? (And oh, void main is still not allow by the C++ standard.) Seeming as how you tried to quote the standard in an attempt to pretend you're right, I'll quote the standard to once...
45
3593
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
4
2935
by: wongjoekmeu | last post by:
Hello All, >From a book where I have learned C++ it says that each application must have at least one function which is the main() function. Now I have the source code of a C++ program from someone else. But whatever I am doing to search for the main function, I can not find it. There is a very short description of the program in a Readme file where it says that the main program is located in a class "MytoolApp". I've had a look in this...
16
2322
by: Stephen Mayes | last post by:
Correct me if I am mistaken. The C standard guarantees that in a C hosted environment we can define main as: int main( int argc, char *argv ). I always assume that 'argc' is the number of pointers in the array 'argv' and that the pointers in 'argv' point to null-terminated strings, and that those strings represent a command line. Is this always the case?
5
2800
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be needed during cleanup. (Finally the two forms are obviously not equivalent in a recursive call to `main'). My questions are
8
2737
by: Michal Nazarewicz | last post by:
Hi, What does returning 0 from main() mean according to C89/C90 standard? I've found that in C99 it means successful termination (7.20.4.3p5) however as I'm editing book on C at Polish Wikibooks I'd like to know what C89/C90 says about it - quotation from C89 or C90 would be nice. Moreover, in C99 main() function is somewhat special because lack of return statement is equivalent with returning zero (5.1.2.2.3p1). Is it also the case...
28
6028
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
17
5037
by: mike-yue | last post by:
and, Is it possible to call one main function from another main function? Thanks guys.
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8820
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.