473,513 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do i have to have a main function?

Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?

May 23 '07 #1
5 1817

<Pe*********@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?
To keep from getting errors? :-)

It's the function which gets executed when you run the program.

Whether you actually need a function called "main" or not depends on what
platform and type of executable or library you're compiling. For example,
it may be WinMain (or something like that, I forget) on Windows. And a
library may not even *have* an entry point, because it doesn't get executed
(at least not in the same way that a stand-alone application gets executed).
Instead, it may "export" any number of relative addresses in the library
where specific functions can be called.

Assuming you didn't have a main function, what would you do instead? How
would you expect the compiler to tell the operating system where to start
executing your code?

-Howard
May 23 '07 #2
<Pe*********@gmail.comwrote:
Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?
Yes you do.

a) a bunch of guys got together many years ago, probably in some nice place
like Geneva, and decided you needed one, and that should be how to spell
it.

b) A sizable program has many separate source files. The operating system
has to know how to find which one of them gets control when the program
executes.
May 23 '07 #3
Pe*********@gmail.com wrote:
Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?
You don't, if you're not in a hosted environment. If you are, you need
one because the Standard says you do. Why don't you want one?


Brian
May 23 '07 #4

<Pe*********@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?
You don't have to. At least some compilers let you specify the entry point
(like Visual C++ for example). If you're not overriding the entry point
though, then you'll have to have a main. The entry point though is not
really main but some other function in the runtime library that calls main.
If you change the entry point, you can't use the C++ libraries because they
won't be initialized. Other things such as destruction of global objects
with constructors won't work unless you find a way to do so (not too hard).
I don't have an exhaustive list of the things you'll encounter on every
platform. Note that I have done this (created my own entry point) on the
WinTel platform and it was very instructional. I didn't figure it out as
much as I just did the research on the web on how to do it. I got into that
while trying to wittle down executable sizes to the minimum.

John
May 23 '07 #5
On May 23, 8:35 pm, "osmium" <r124c4u...@comcast.netwrote:
<PencoOdS...@gmail.comwrote:
Do i have to have a main function?
If i don't have a function called main,i get erors.Why do i have to
have a function called main?
Yes you do.
a) a bunch of guys got together many years ago, probably in some nice place
like Geneva, and decided you needed one, and that should be how to spell
it.
It wasn't a bunch---more like two or three. And it was
someplace in New Jersey.
b) A sizable program has many separate source files. The
operating system has to know how to find which one of them
gets control when the program executes.
There are a number of ways of handling this. Kernighan, Thomson
and Richie just chose one.

And of course, in C++, you get control before main starts, in
static initializers.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 24 '07 #6

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

Similar topics

19
2181
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 which they are declared and or defined. I'd like to know if there are any features of this code you believe represents bad programming technique. How...
2
1628
by: marco | last post by:
the problem: I use a typedef inside a class template, than I use this type (dim_v<N1>::Type) to define the argument of a template function f but when I call this function from main, the compiler (gcc 3.4.1) tell me: "no matching function found". If someone, more expert than me, could tell me what is wrong I would be very happy
9
1268
by: sojin | last post by:
#include<stdio.h> void change() { /* Write something in this function so that the output of printf in main function should give 5 . Do not change the main function */ } void main() {
89
6392
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
16
398
by: junky_fellow | last post by:
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...
8
3571
by: Rody Reulen | last post by:
I made an console application with Visual Basic .net. Visual Basic will automatically create the sub procedure main for you. I try to convert the main procedure to a function. During the compilation I will get an error message: No accessible 'Main' method with an appropriate signature was found in <application>. I took a look at the...
19
28541
by: beliavsky | last post by:
I think I read a suggestion somewhere to wrap the code where a Python script starts in a main() function, so one has def main(): print "hi" main() instead of
28
6010
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,
80
3513
by: Ioannis Vranos | last post by:
Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int main(int argc, char *argv)"? AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv) - and the **argv syntax" are the only valid ones.
17
5016
by: mike-yue | last post by:
and, Is it possible to call one main function from another main function? Thanks guys.
0
7269
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...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7123
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...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5100
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...
0
4756
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...
0
3248
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...
0
1611
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

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.