Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
hi
what is this line means :
C++
int main(int argc, char argv[])  

and what does it does?

and too this:
C++
int _tmain(int argc, _TCHAR* argv[])


and what is the difference between those and follows :
C++
int main ()

please explain it simply

thanks
Posted
Updated 9-Feb-13 7:50am
v2
Comments
Philippe Mori 2-Apr-13 13:09pm    
You should read the documentation...

C++
int main(int argc, char *argv[])
int main ()

These are the entry points of any program according to C++ standard. The first is with the command line parameters, second is without them.

C++
int _tmain(int argc, _TCHAR* argv[])

This is Microsoft's extension to C++ standard entry point. It made for multibyte and unicode strings compatibility.

upd.
About main in ISO C++ standard 3.6.1
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf[^]
Please see page 51
 
Share this answer
 
v2
Comments
hor_313 10-Feb-13 4:46am    
i remember when i was learning c++ we use some forms like this :

int main(int a , int b )

what is the difference between this form and that form i written in topic?
still i cant underestand what do the bracketed phrases do?
skydger 10-Feb-13 9:53am    
Either you was learning a different language or it was a misprint. :) Non of entry point functions has such notation. C++ standard specifies only two of them mentioned in all solutions for your question. I updated this solution with link to ISO C++ standard.

As for 'bracketed phrases'... If you meant char* argv[] then see the complete Solution 1. Well... if you never faced programs with cpmmand-line parameters, then I understand your confusion. Please see this article, maybe it will make it clear for you:
http://en.wikipedia.org/wiki/Command-line_interface
hor_313 10-Feb-13 11:49am    
yes i didnt work by command line and i dont know about how it works
skydger 10-Feb-13 12:36pm    
Ok, it explains your confusion. So you may achieve this by launching Command Prompt (cmd.exe) in MS Windows (or launch a console in Linux). Then you should execute your program like "myapp.exe -hello -world". "-hello" and "-world" will be in char *argv[] array.
There is another way to do this. In Visual Studio in your executable project's poperties window go to Debuging section, then write down your "Command arguments" which will appear at the start of you app.
hor_313 11-Feb-13 3:58am    
thanks a lot
A user can run your program with command-line parameters, for example:
youApplication.exe param1 -i filename.txt /verbose

In this example, the user passes the parameters "param1", "-i", "filename.txt" and "/verbose". That they mean is totally defined by your application.

You can access these parameters via the arguments of main you show. First parameter gives you the number of command line parameters, and the second one, array of strings, gives you the parameter values.

It's your business to interpret them and use as the user input.

[EDIT]

Please see: http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B[^].

—SA
 
Share this answer
 
v2
C++ as a language is derived from C, which was originally a console based language. The api "main()" (with or without parameters) is the entry point to your program. If you do not provide parameters, the api is called and your program is not able to see any parameters that may have been passed to the program.

Moving along a few decades now, Microsoft Windows supports ANSI and UNICODE. The API _tmain is a macro that expand to the program entry point main (ANSI) or wmain (UNICODE) according to _UNICODE macro. Starting with VC2005 (?), a program that is UNICODE enabled (ie. _UNICODE defined) will expand _tmain to wmain (not main) and _TCHAR expands to wchar_t.

The Microsoft explanation can be found here[^]. Beware that they have some square brackets in the text that are confusing and can be misinterpreted.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 10-Feb-13 13:22pm    
Command line parameters have nothing to do with console at all! you can always use then with UI application, and virtually with anything.
—SA
Hey.. I think you are student too. However my teacher says that simply remove this
C++
int _tmain(int argc, _TCHAR* argv[])

and write a main like this:
C++
int main()

because at that level we are now, we should not consider that.!!
Moreover you write
C++
int main(int a, int b)

it will work but, that is wrong actually this concept is called functions, main is also a function but in main we call the other function and the int a, int b are called parameters.
You should write main as:
C++
int main()


REMEMBER: EVERYTHING COMES TO YOU AT RIGHT TIME. . .
 
Share this answer
 
Comments
hor_313 10-Feb-13 11:54am    
thanks dear .
after i thought about that i remember we didnt write it as int main(int a , int b).
just i confused and did mistak.
Usman Hunjra 10-Feb-13 13:06pm    
You Are Always Welcome .,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900