Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
hi
 
what is this line means :
int main(int argc, char argv[])  
and what does it does?
 
and too this:
int _tmain(int argc, _TCHAR* argv[])
 
and what is the difference between those and follows :
int main ()
please explain it simply
 
thanks
Posted 9-Feb-13 8:47am
hor_313627
Edited 9-Feb-13 8:50am
v2
Comments
Philippe Mori at 2-Apr-13 13:09pm
   
You should read the documentation...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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
  Permalink  
v2
Comments
hor_313 at 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 at 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 at 10-Feb-13 11:49am
   
yes i didnt work by command line and i dont know about how it works
skydger at 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 at 11-Feb-13 3:58am
   
thanks a lot
Sergey Alexandrovich Kryukov at 2-Apr-13 1:55am
   
My 5.
—SA
skydger at 3-Apr-13 13:19pm
   
Thank you :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v3
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hey.. I think you are student too. However my teacher says that simply remove this
int _tmain(int argc, _TCHAR* argv[])
and write a main like this:
int main()
because at that level we are now, we should not consider that.!!
Moreover you write
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:
int main()

 
REMEMBER: EVERYTHING COMES TO YOU AT RIGHT TIME. . .
  Permalink  
Comments
hor_313 at 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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100