Main Parameters

Ive been my Highschool C++ programming for about 6 months now. We've gone over alot, and i now understand parameters(Well most parameters) except for the parameters of the main function. For example,I see main(void) and am not quite sure what this would acomplish or what its purpose is. I do know what void functions are but, because ive never seen another function (Or function typle in this case) would do. Can someone explain?
int main(void) is the same as int main()

This is opposed to the main function header which allows command line arguments to be passed:
int main(int argc, char *argv[])
Last edited on
So, why would you add void to the main's parameters? Since int main() has the same result
Im no expert but, i think....

If you have a function lets say. void fn(void);
void basically means that it does not do anything at the begining of a function but it's important to get into the habbit to use it because it easy to notice. but before a function so, void fn(). the void bit declares what type of function it is so, int, double, float and so on. in the parenthesis fn(void) could be replaced by what the function wants to return. so if you are asking a question you would want it to return a value, so you would say int fn(int num, int num2){} and so on.

if you still don't understand please ask..
hope this helps.
i will give you a example if you want?
cstorm
So, why would you add void to the main's parameters?
For clarification/documentation. I personally don't.

PracticingProgrammer
void basically means that it does not do anything at the beginning of a function...
I think "void" in the formal parameter list means it take no input.

Someone please correct me if I'm wrong here. As I said, "I don't do this."
You don't have to add void to parameters. but my personal opinion it helps me for readability. it's like "nothing" = " ". i suppose.
Last edited on
Well not quite because
1
2
3
4
5
6
7
void f(double x); //is not the same as
f(double x); //(will not compile)

// and
double f(void *x); //is not the same as
double f(*x); //(will not compile) or
double f(void); //for that matter 
so becarful. However in this case void behaves exactly like "".
Last edited on
Yeah thats what i was trying to explain on my first response, to get into the habbit of writing void but ill leave it there. im no expert on the subject l0l cheers for your correcting.
you can also use:
1
2
3
int main(int argc, char *argv[])
{
}

this gives you access to console arguments.
int argc stores the number of arguments and char* argv[] stores the actual arguments
@asscii http://cplusplus.com/forum/beginner/41239/#msg222330
Also, argv[] holds the name of the program as well (at index [0])
So argc is the length of argv, i.e. number of arguments + 1
What console arguments would you have access to? What would they do? and what purpose would I need to access them for?
Do you run your programs from the console? If not ignore this, but if you do...

Let's use the compiler "cl" (VC++) as an example. To compile from the comand line you type
cl prog.cpp
"cl" is the name of the compiler, and prog.cpp is the file you want to compile. cl knows your file's name because you passed it as an argument. So if you want access to arguments passed into your program just use
int main(int argc, char *argv[]) { ... }
and now the array argv is popuated with argc (number of) values, where argv[0] is the name of the program (i.e. "cl" in the above example) and argv[1 .. n-1] will have the arguments (i.e. argv[1] == "prog.cpp" and argc == 2, in the above example)
Last edited on
There is a slight difference between an explicit void and nothing at all, like Ben Lynn said:
"The declaration:
void some_function();
In C, it declares a function with an unknown number of arguments, while in C++, it declares a function with zero arguments."
(http://www-cs-students.stanford.edu/~blynn/c/ch07.html)

About main's parameters...
Like Mathhead200 said above, those arguments are the parameters passed by command line at execution. When you do a program, you get your working data on different ways (interactively from user, from a file, or hard-coded, and in a bunch of other different ways...)! Among them, parameters are yet another very useful way to pass data to a main executing module. I'll give you a tangible example: I see you almost everyone have Windows so let it be "notepad.exe" - the Windows's elementary text editor. You may open a file either after opening the Notepad's window then by menus or dragging and dropping the desired file in it's window, or... you may write in the "Command Prompt":
notepad.exe file.txt
In this way Notepad's main function will receive "argc" with value 2 (nr. of parameters), and "argv" would be the array of those two string parameters: "notepad.exe" and "file.txt". Notepad receive in this way the name of the file you want it open and I guess that if you tried to type that in your console, the result is visible. It will open the "file.txt" if there is such file in the current active directory, or prompt you asking if you want to create it if it doesn't exist! In such maner you may write your own program:

1
2
3
4
5
6
#include "stdio.h"
void main (int n, char* s[])
{
  if (n > 1)
    printf("Hello, %s!", s[1]);
}

...that do some wonders out there! ;)
Topic archived. No new replies allowed.