Thread: Different kinds of main() function

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Philippines
    Posts
    20

    Question Different kinds of main() function

    Hi! I'm currently familiar with 2 'kinds' of main() function. The first one is:
    Code:
    int main(); 
    int main(void);
    int main(int argc, char *argv[]);
    I think this is the standard 'main()' in C and C++.

    The second one is:
    Code:
    int WINAPI 
    WinMain(HINSTANCE hInstance, 
          HINSTANCE hPrevInstance, 
          LPSTR lpCmdLine, 
          int nCmdShow)
    I use this in Win32 Programming.

    One day, I was trying to compile a program I found online (I forgot what program is it) and found a "t_main()" sitting on the main file. What does this main function do? Also, what are the other main function in C (and/or C++)?

    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,558
    Quote Originally Posted by Bryan_James
    I'm currently familiar with 2 'kinds' of main() function.
    It might be more accurate to talk about functions meant to be defined as "the designated start of the program". After all, WinMain is unlikely to be an alias for main, so it isn't another 'kind' of main function. Note that of the three possible main functions that you outlined, the first two are equivalent, except that the second is more commonly used in C because in C++ an empty parameter list never means "unknown number of parameters", whereas in C that is its meaning for a function declaration that is not a function definition.

    Quote Originally Posted by Bryan_James
    One day, I was trying to compile a program I found online (I forgot what program is it) and found a "t_main()" sitting on the main file. What does this main function do?
    Perhaps you saw _tmain rather than t_main. A quick search of the Web brings up: main: Program Startup.

    Quote Originally Posted by Bryan_James
    Also, what are the other main function in C (and/or C++)?
    You will find out when you need to use them (i.e., there are too many possibilities to feasibly enumerate exhaustively), or perhaps even define your own if you feel an alternative is more convenient for say, handling the command line arguments.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,756
    t_main() is what microsoft use as an extension (much line winmain is used for GUI programs) for console programs, to facilitate compiling in either ANSI or UNICODE modes.

    In unicode mode, argv arrives as an array of wide character strings.
    unicode - What is the difference between _tmain() and main() in C++? - Stack Overflow
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-08-2014, 10:57 AM
  2. Different kinds of machines
    By Satya in forum C Programming
    Replies: 1
    Last Post: 04-24-2014, 09:52 PM
  3. Replies: 35
    Last Post: 12-01-2011, 08:31 PM
  4. Replies: 3
    Last Post: 06-01-2011, 03:08 AM

Tags for this Thread