question: Why does main need parameters?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

question: Why does main need parameters?

Post by Avishaiozeri »

Hi guys, i have a question. Lately i'm starting to learn Sdl more seriously, and now when i practice i'm getting used to adding the (int argc, char* args[]) to the main as parameters. Why do i need these? is it some parameters the operating system needs to send the main function in order to communicate with it or something?
Thanks
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: question: Why does main need parameters?

Post by short »

argc is the number or arguments in argv.

argv is an array of strings, with i believe the first one argv[0] being the name of your program. Debug your program and inspect the contents on the argv array.

I believe sdl needs them for some of its internal workings, but I am not positive on that. GOOGLE! :)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: question: Why does main need parameters?

Post by Milch »

This is for additional arguments for your program.
Like the '-console' for Source games to enable console on startup.

http://www.site.uottawa.ca/~lucia/cours ... ments.html
Follow me on twitter!
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: question: Why does main need parameters?

Post by Avishaiozeri »

Milch wrote:This is for additional arguments for your program.
Like the '-console' for Source games to enable console on startup.

http://www.site.uottawa.ca/~lucia/cours ... ments.html
I'v tried the link you gave, and used the code they wrote there to check what is in the argv array.
I saw it contains the path to the software on my computer. thank you. but i have another question,
do i need to put these parameters if i compile the project for other systems, like linux or something?
User avatar
cndr
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Sat Apr 24, 2010 7:03 am
Programming Language of Choice: etc.

Re: question: Why does main need parameters?

Post by cndr »

you do need the parameters (int argc, char *argv[]) for linux.
Long Live The Queen!
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: question: Why does main need parameters?

Post by Avishaiozeri »

cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: question: Why does main need parameters?

Post by ismetteren »

EDIT: i dident realize the link posted by Milch contained all the information... but now when i have used time on writing it, i will let it be here ;)
Avishaiozeri wrote:
cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?
No linux uses / instead of \ and you just start with a / to indicate root. No such thing as drives.

argv contains arguments you can send to your program on startup. Usually you start your program like this:

Code: Select all

>myprogram.exe
You can pass args to the program like this:

Code: Select all

>myprogram.exe arg1 arg2 arg3
You could then do like this in your program:

Code: Select all

for(int i = 1; i<argc; ++i) {
    cout << argv[i] << endl;
}
this would the output:

Code: Select all

arg1
arg2
arg3
if you ran your program like this:

Code: Select all

>myprogram.exe hello im ismetteren
it would output this:

Code: Select all

hello
im
ismetteren
nearly all command line tools i know uses args. ant, git, svn, make and so on.
Image ImageImage Image
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: question: Why does main need parameters?

Post by Avishaiozeri »

ismetteren wrote:EDIT: i dident realize the link posted by Milch contained all the information... but now when i have used time on writing it, i will let it be here ;)
Avishaiozeri wrote:
cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?
No linux uses / instead of \ and you just start with a / to indicate root. No such thing as drives.

argv contains arguments you can send to your program on startup. Usually you start your program like this:

Code: Select all

>myprogram.exe
You can pass args to the program like this:

Code: Select all

>myprogram.exe arg1 arg2 arg3
You could then do like this in your program:

Code: Select all

for(int i = 1; i<argc; ++i) {
    cout << argv[i] << endl;
}
this would the output:

Code: Select all

arg1
arg2
arg3
if you ran your program like this:

Code: Select all

>myprogram.exe hello im ismetteren
it would output this:

Code: Select all

hello
im
ismetteren
nearly all command line tools i know uses args. ant, git, svn, make and so on.
Thank you, but does the dreamcast and ps has this thing?
Edit: The games that people tell you to add things like "-window" and stuff to the shortcut path, to make them act differently, is this
the arg?
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: question: Why does main need parameters?

Post by Milch »

Avishaiozeri wrote: Edit: The games that people tell you to add things like "-window" and stuff to the shortcut path, to make them act differently, is this
the arg?
Yes.
So you can use the args for stuff like skipping the main menu and loading a level, starting the programm in window mode and so on.
Follow me on twitter!
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: question: Why does main need parameters?

Post by Ginto8 »

Avishaiozeri wrote:does the dreamcast and ps has this thing?
they probably have something along these lines, but a bit different. I know that GBA and DS homebrew, for the most part don't use them, but I'm not sure how they work if you do use them.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: question: Why does main need parameters?

Post by Avishaiozeri »

All my questions were answered, thank you all! :)
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: question: Why does main need parameters?

Post by RyanPridgeon »

It's also useful to know for Windows, that If you right click on a file and choose Open With, then choose your program, the path to that file will be stored in argv[1].
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: question: Why does main need parameters?

Post by Avishaiozeri »

So, it can be used to determine the format your program will support?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: question: Why does main need parameters?

Post by dandymcgee »

RyanPridgeon wrote:It's also useful to know for Windows, that If you right click on a file and choose Open With, then choose your program, the path to that file will be stored in argv[1].
Or if you drag and drop a file onto your program in explorer. :)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: question: Why does main need parameters?

Post by eatcomics »

Never knew that, that could be handy for a map editor or something similar :P
Just drag and drop your map and it opens up
Image
Post Reply