2

So I am starting with c++ (i am trying to broaden my mind with new languages) but I came across a little issue which confuses me more than what I guess it should...

Using Visual Studio Express 2012, I created a console win32 application in C++ and this is my main method decleration:

// TestApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

however, since I dont know anything about c++, I searched for some tuts online and all of them had there declerations setup in a different manner

#include <iostream>

using namespace std;

int main()
{
  cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
  cin.get();
}

and

// my first program in C++
#include <iostream>

int main()
{
  std::cout << "Hello World!";
}

I tried typing in the "std::cout", but it wouldnt accept it, could someone just clarify why and the significance of the difference ?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 7
    http://stackoverflow.com/questions/895827/what-is-the-difference-between-tmain-and-main-in-c – Maroun Dec 26 '13 at 14:29

3 Answers3

0

The main method can be define with or without parameters. It all depends upon what you are using your application for.

Take a look at this: https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fmainf.htm

Also for your program you need to have a return value

// my first program in C++
#include <iostream>

int main()
{
  std::cout << "Hello World!";
  return 0;
}
Jesse Laning
  • 490
  • 4
  • 12
0
int _tmain(int argc, _TCHAR* argv[])

is (at least I think so) a Windows only library and compiler depending way of declaring the main function.

Definitly not wrong is to declare main like this:

int main(int argc, char const *argv[])
{
    //do something
    return 0;
}

or like this:

int main()
{
    //do something
    return 0;
}

This is definitly proper C++ and you can use this universially.

Magnus
  • 1,550
  • 4
  • 14
  • 33
0

C++ programs may have one of two beginnings:

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

or

        int wmain(int argc, wchar_t *argv[])

The first of these gets its arguments (argv) as ANSI charachers, while the second gets "wide" characters -- generally UTF-16 or UTF-32, depending on the platform.

Microsoft define a framework to allow you make code that can compile with either ANSI or wide characters.

        int _tmain(int argc, TCHAR *argv[])

Behind the scenes, they have something like this:

        #if defined UNICODE
            #define _tmain wmain
            #define TCHAR wchar_t
        #else
            #define _tmain main
            #define TCHAR char
        #endif

They also have helper functions like _tprintf() and _tcscpy().

NOTE: as pointed out by others, the argc and argv params are optional, so you can also have

        int main()

and

        int wmain()

and (for Microsoft and compatible compilers)

        int _tmain()

Also note that while _tmain() is not strictly portable, you can easily create your own #define macros if you want to be portable to other platforms.

Michael J
  • 7,631
  • 2
  • 24
  • 30
  • 1
    No. `wmain` is not legal C++ – it’s a Microsoft extension, same as `_tmain`. – Konrad Rudolph Dec 26 '13 at 16:19
  • I stand corrected. I thought wmain() had been added to the standard, but a quick search did not reveal any support for that. Clearly I've been doing too much Windows programming lately. – Michael J Dec 26 '13 at 16:36