Can i call int main() in a switch statement?

Daniel Shepherd 246 Reputation points
2021-07-12T13:06:42.52+00:00

I am kind of a beginner to win32 apps, and I was thinking, can I call int main() in a switch statement? Here is an example that I am hoping will work:

case WM_LBUTTONDOWN:
    {
        MessageBox(hWnd, L"test", L"test", MB_OKCANCEL | MB_ICONINFORMATION);
        int main(){
             //note: I would have already included the iostream header
             //could this work?
        }
        break;
        return 0;
    }
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,689 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,787 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 55,481 Reputation points
    2021-07-12T13:33:25.18+00:00

    Can you call a function main inside a switch - of course, you can call any function you want. It would be very limiting if you couldn't.

    Is your syntax valid? no. You aren't calling a function given your syntax, you are declaring a function called main that returns an int. C++ doesn't support local functions (and even if it did, not inside a case statement). The correct syntax would be.

    case WM_LBUTTONDOWN:
         {
             MessageBox(hWnd, L"test", L"test", MB_OKCANCEL | MB_ICONINFORMATION);
             main();
             break;
             return 0;
         }
    

    Now the actual question is when this would ever make sense. It wouldn't. Let's be very clear here - main is the entry point to your program. It is called automatically by the runtime when your app starts and should initialize and run your app. Until it returns your program will continue to run. In this regard it would never make sense to call the entry point while your program is already running. Firstly it would reinitialize your app. Secondly it would block until your program starts its termination process and then it would "continue" anyway. Hence this wouldn't logically make any sense.

    Note that it does make sense that there might be logic inside main that you want to call somewhere else in your code. That is exactly what functions are for. Move any logic you want to use outside of main into one or more helper functions and then call the helper functions wherever you need them. main should initialize, run and clean up your app - that's it.

    Finally, it looks like you are creating a Windows app, not a console app. You appear to be inside a message pump. Therefore it wouldn't make sense to call main as that is for console apps. Windows apps use Winmain instead. Going back to what was stated previously, if you are converting an existing console app to a WIndows app and there is functionality in the original app's main that you want to reuse then move that logic into helper functions. Then call the helper functions from your message pump. This will make your app easier to understand and more flexible.

    2 people found this answer helpful.

  2. Castorix31 86,316 Reputation points
    2021-07-12T13:13:05.31+00:00

    This has no sense...
    main is used instead of WinMain in a Console application : Build and run a C++ console app project

    0 comments No comments

  3. rupesh shukla 16 Reputation points
    2021-07-14T20:24:21.35+00:00

    What kind of win32 App you are using . And what is the purpose of calling main inside a function. In general the main function is where a program starts its execution. So if you can elaborate little bit more about what you are trying to do then it will be easy to answer. In the end it's a function you can call it in a function or you can call main() inside main(). You are free to do what you want.

    Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.