473,383 Members | 1,929 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,383 software developers and data experts.

Significance of main()

2
Hi, I would like to know what is the significance of main( ) in C/C++ programs?
Even though it is a user defined function, it is not as flexibile as other user defined function?
Apr 29 '07 #1
11 3794
Hi, I would like to know what is the significance of main( ) in C/C++ programs?
Even though it is a user defined function, it is not as flexibile as other user defined function?

Its a user defined function, but the declaration is part of the C run time. As your program begins to execute, its doing a whole lot of stuff to initialise the process to the point where your code can execute. Once the initialisation is complete (DLLs loaded, memory allocated etc) it calls main(). It expects an integer return, which is passed back the OS. Its defaulted to zero so you don't have to bother if you don't want to.
Apr 30 '07 #2
Banfa
9,065 Expert Mod 8TB
Its defaulted to zero so you don't have to bother if you don't want to.
This isn't right, since main returns int most compilers will issue an error if you do not return int from the function therefore you always have to bother to return a value.
Apr 30 '07 #3
This isn't right, since main returns int most compilers will issue an error if you do not return int from the function therefore you always have to bother to return a value.

Stroustrup 3rd Ed Section 3.2...
The minimal C++ program is...
int main() {}
Apr 30 '07 #4
Alse
2
In simple words i can say it is the starting point. Hence it is must to specify the starting point for any program.

Thanks to all
Alse
Apr 30 '07 #5
JosAH
11,448 Expert 8TB
Stroustrup 3rd Ed Section 3.2...
The minimal C++ program is...
int main() {}
Yes, C++ differs from C in that: returning an int value is optional while it isn't
in C. The main method also can't be called recursively in C++ while it can in C.
In C++ main is more 'special' than it is in C. Microsoft's convention of defining
main (in either C or C++) as a void function is a big nono.

kind regards,

Jos
Apr 30 '07 #6
Banfa
9,065 Expert Mod 8TB
In C++ main is more 'special' than it is in C. Microsoft's convention of defining main (in either C or C++) as a void function is a big nono.
I don't think that this a Microsoftism, I haven't see it said anywhere before that it is OK to return void from main. Although checking the help they do not seem to specify the return type at all.

What MS does do as an extention is put a 3 parameter in main containing the environment it was called with.

I have seen compilers that allow void main (specifically mcc18 PIC compiler) but this was strictly as an extension.
Apr 30 '07 #7
JosAH
11,448 Expert 8TB
I don't think that this a Microsoftism, I haven't see it said anywhere before that it is OK to return void from main. Although checking the help they do not seem to specify the return type at all.

What MS does do as an extention is put a 3 parameter in main containing the environment it was called with.

I have seen compilers that allow void main (specifically mcc18 PIC compiler) but this was strictly as an extension.
In this link Microsoft states this:
Alternatively, the main and wmain functions can be declared as returning void
(no return value). If you declare main or wmain as returning void, you cannot
return an exit code to the parent process or operating system using a return
statement; to return an exit code when main or wmain is declared as void, you
must use the exit function.
Microsoft have been doing this for years; even when the first ISO C draft was
out of the bag Microsoft continued writing 'void main' and a lot of people believed
it was correct (which it isn't according to the Standard).

kind regards,

Jos
Apr 30 '07 #8
Banfa
9,065 Expert Mod 8TB
In this link
Interesting, you just can't trust em can you :rolleyes:

That paragraph does not appear in the help files supplied with MSVC++

At the very least it should be tagged as a MS extension.
Apr 30 '07 #9
JosAH
11,448 Expert 8TB
Interesting, you just can't trust em can you :rolleyes:

That paragraph does not appear in the help files supplied with MSVC++

At the very least it should be tagged as a MS extension.
Well, to be honest they do: that void main thing is discussed in the paragraph
titled: "END Microsoft Specific" ;-) they use it all over the place themselves
which renders their own code non-Standard and non-portable.

kind regards,

Jos
Apr 30 '07 #10
Banfa
9,065 Expert Mod 8TB
Well, to be honest they do: that void main thing is discussed in the paragraph
titled: "END Microsoft Specific";
Erm, yes this puts it outside Microsoft Specific stuff which is

Microsoft Specific

in this section here

END Microsoft Specific

So they are claiming that is how C/C++ works not that is how there implementation works.

... they use it all over the place themselves
which renders their own code non-Standard and non-portable.
Would you want to port it? (I'm being flipant, I do get the point)

I have to say one of the things I enjoy about embedded programming is that MS doesn't feature.
Apr 30 '07 #11
JosAH
11,448 Expert 8TB
Erm, yes this puts it outside Microsoft Specific stuff which is

Microsoft Specific

in this section here

END Microsoft Specific

So they are claiming that is how C/C++ works not that is how there implementation works.

Would you want to port it? (I'm being flipant, I do get the point)

I have to say one of the things I enjoy about embedded programming is that MS doesn't feature.
I don't even want to be found dead porting anything to or from a Microsoft OS.
IMHO what Microsoft has done and still is doing is a major setback for the
entire computing industry. But I guess that's what monopolies are for ;-)

kind regards,

Jos

ps. I especially like unhosted embedded programming, i.e. you can be king
over that poor little processor ;-)
Apr 30 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Anonymous | last post by:
In PHP Whats the difference between $yourname = @$_POST; AND $yourname = $_POST; In particular, what is the significance of '@' as a prefix to the variable? Thanks in advance
4
by: Prem Mallappa | last post by:
Hello can anybody please tell me what is the significance of 0; 1; or something like 100;
2
by: Prem Mallappa | last post by:
what is the significance of 0; 1; or 100;
5
by: kernel.lover | last post by:
hello, I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here. Does void * is used when function has the flexibility to return any type of...
4
by: Frank Rizzo | last post by:
In some examples with inheriting, I see the overloaded function always call its base. For instance, in this case where the ListView control is being extended. public class ListViewEx :...
2
by: | last post by:
What is the significance in using MVC architechture in Dotnet? I think the framework itself is based on MVC The view being the Asp.Net The Controller being the code-behind file .... Is that...
26
by: joe | last post by:
My experiments show that the random number generator in Microsoft's VC++6 compiler is a statistical RNG with a significance level 1.0%. Statistical testing at SL >1.0% (for example 1.001%) passes...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.