Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 7th, 2006, 2:38 PM   #1
sharadpro
Newbie
 
Join Date: Feb 2006
Location: India
Posts: 25
Rep Power: 0 sharadpro is on a distinguished road
main() and main(int argc,char*argv[])

as we cant use function overloading in c..
then how are
main()
and
main(int argc, char *argv[]) used?
sharadpro is offline   Reply With Quote
Old Jul 7th, 2006, 2:51 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You don't have to worry about overloading, since you only use one or another (never two or more). All you have to worry about is what the compiler does with the particular one it encounters. It does well.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 7th, 2006, 4:20 PM   #3
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
main(int argc, char *argv[])
is used when arguments need to be passed to your program. Usually this is done from the command prompt, but may also be used when the program is called from another application. Also, using this method you may supply predefined input to your program without hasseling with files or hard-coding them in. (Note: This works with CodeWarrior and I believe Dev-Cpp, although I'm not sure this is standards compliant for it to do so. I have not tested it in GCC or VS)
main()
is used when your program does not require input from outside sources.
nindoja is offline   Reply With Quote
Old Jul 7th, 2006, 5:35 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
The main function is a specific case in the standard, and all implementations are required to support both forms (or equivalent). This is not overloading, as Dawei said, which means that your programs are only allowed to use one of them.
grumpy is offline   Reply With Quote
Old Jul 8th, 2006, 12:24 AM   #5
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
Quote:
Originally Posted by grumpy
The main function is a specific case in the standard, and all implementations are required to support both forms (or equivalent). This is not overloading, as Dawei said, which means that your programs are only allowed to use one of them.
Grumpy, I think my wording was confusing. What I was saying when I said "I'm not sure if this is standards compliant" I was referring to me using
main(int argc, char *argv[]) to pass input to my program. Of course main(int argc, char *argv[]) is standards compliant, all it is doing is accepting arguments to a function, albeit a function that the compiler calls to begin execution of the program.
nindoja is offline   Reply With Quote
Old Jul 8th, 2006, 3:42 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Using int main(int argc, char *argv[]) is standard if you want your program to accept parameters at startup

In practice, on most hosted implementations such as those under Unix or windows, the input received via the arguments argc and argv typically come from a command line, but the standard does not specifically require that. In fact, the standard is silent on where the information passed in argc and argv comes from .... that is part of the implementation details of your compiler (eg the compiler specific code that sets up a few things, and then calls your main function).
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC