2

I made a simple program to test whether I am able to call main in any other function like we call different functions in other function.

Therefore, I made a program to generate the maximum number by getting input of 3 numbers from the user. For this I made the function containing if else. Surprisingly (especially for me) it worked. I got that I am keep inputting the numbers then I made another variable to control the inputs. I made it to input 3 times. I got answer from bottom to end and 3 answers are generated.

#include <iostream>
using namespace std;
int main();
void max(int a, int b, int c,int p);
int p = 0;
int main()
{
    p++;
    int a, b, c;
    cout << "\n\n";
    cout << "Enter 1st number :\t";
    cin >> a;
    cout << "Enter 2nd number :\t";
    cin >> b;
    cout << "Enter 3rd number :\t";
    cin >> c;
    max(a, b, c, p);
    cout << "\n\n\n";
    system("pause");
    return 0;
}
void max(int a, int b, int c,int p)
{
    if (p < 3) 
    {
        main();
    }
    if (a > b&&a > c)
    {
        cout << a << " is maximum";
    }
    else if (b > a&&b > c)
    {
        cout << b << " is maximum";
    }
    else
    {
        cout << c << " is maximum";
    }
}

The output was as follows: -



Enter 1st number :      12                                                                                              
Enter 2nd number :      14                                                                                              
Enter 3rd number :      15


Enter 1st number :      45                                                                                              
Enter 2nd number :      69                                                                                              
Enter 3rd number :      88  


Enter 1st number :      14                                                                                      
Enter 2nd number :      20                                                                                              
Enter 3rd number :      11                                                                                             
20 is maximum             


                                                                                                                                                                                                                                                                                                                                              Press any key to continue . . .                                                                                         
88 is maximum                                                                                                                                                                                                                                                                                                                                                           



Press any key to continue . . .                                                                                         
15 is maximum                                                                                                                                                                                                                                                                                                                                                           



Press any key to continue . . .  

I don't get the logic behind it. I used Visual Studio 2017 for this.

  • 6
    You can't call `main` in C++. Trying to is Undefined Behavior so there is no logic to explain. – François Andrieux Aug 28 '19 at 16:43
  • 2
    It may or may work as you expect (or at all). Calling main explicitly is undefined behavior. – Chris Uzdavinis Aug 28 '19 at 16:44
  • 3
    "Calling main() in any other function in c++" - Calling `main()` is *not* allowed in C++. If you do it your program has [Undefined Behaviour](https://en.cppreference.com/w/cpp/language/ub) and is essentially meaningless and your compiler is no longer required to generate anything meaningful for *the entire program*. Nor is the compiler required to issue any diagnostic - it's explicitly allowed to silently assume that no UB is present. It's *your* responsibility to know *all* the rules of C++ and *never* break them. The compiler will not help you, it will just asume you didn't break any rules. – Jesper Juhl Aug 28 '19 at 16:45
  • Please review the post i added the output i got. Thank you. – Ghazanfar Ateeb Aug 28 '19 at 16:48
  • 5
    It doesn't matter what output you got - UB is UB. –  Aug 28 '19 at 16:49
  • 2
    @StrangePerson The output of a program is part of it's observable behavior. Since your program encounters Undefined Behavior during it's execution the language makes no guarantee about it's observable behavior. That means it makes no guarantee about it's output, so it's meaningless. Sure, it might have *this* output this time, but on another day, or with another compiler, or due to unrelated changes in the code or for any other possible reason the output might change. – François Andrieux Aug 28 '19 at 16:52
  • 1
    I didn't see a question in your post. Do you have a specific question? – R Sahu Aug 28 '19 at 16:56
  • @RSahu My question was to get the logic behind this unusual behavior. – Ghazanfar Ateeb Aug 28 '19 at 17:00
  • 3
    @StrangePerson As soon as a program has *any* UB *anywhere*, all attempts to reason about logic are futile. There is *no* expected behaviour when the program contains UB. It may do exactly what you expect, it may do nothing at all, it may crash, it may format your harddrive, it may do random things on each invocation - literally *anything* is OK if there's UB in your code. – Jesper Juhl Aug 28 '19 at 17:10
  • 1
    I suggest creating a function `myMain` with the same signature as `main`, moving the contents of `main` to `myMain`, using `myMain` instead of `main` from `max`, and changing the contents of `main` to `return myMain();`. That would remove UB and you are more likely to get a response. – R Sahu Aug 28 '19 at 17:11
  • 1
    you got absolute correct output. in what your problem/question ? *unusual behavior.* ? absolute usual and logic behavior. correct output (based on your program logic) – RbMm Aug 28 '19 at 17:15
  • 1
    BTW, there is a `max` function in the `std` namespace. So to avoid conflicts with your `max` function, *don't use `using namespace std;`*. Either us the `using std::cout;` for the individual elements you are using or prefix them with `std::`. – Thomas Matthews Aug 28 '19 at 17:22
  • 1
  • 1
    [Useful discussion](https://stackoverflow.com/a/39442655/4581301) – user4581301 Aug 28 '19 at 17:57
  • 1
    @JesperJuhl I feel your comment may be misinterpreted. There can be UB in a branch and, as long as control flow never reaches that branch, the program's behavior is fine. Some sources of UB invalidate the entire code base immediately (for example violating ODR) but that is not usually the case. – François Andrieux Aug 28 '19 at 18:54
  • 1
    @FrançoisAndrieux Got a standard quote? As *I* read the standard, *any* UB, *anywhere*, renders the entire program meaningless. Branch or no branch, taken or not. – Jesper Juhl Aug 28 '19 at 19:02
  • 1
    @JesperJuhl Well, yes, if you *have* UB everything is broken. But my comment addresses the wording of your comment. It's not clear to an unfamiliar reader that most UB is only UB if it's encountered (or would be encountered). Example : Having an integer division by zero is UB. But only if the offending expression would be evaluated. So, the potential division would have UB, it is in the code, but the program isn't UB unless that expression becomes unavoidable. – François Andrieux Aug 28 '19 at 19:07
  • My task was to find can we call main() in any other function or not. Therefore, my first priority was to find whether the programs works or not. Secondly, if it works what kind of behavior is observed and the third priority was to find why it is haopening. So I got the answers. Thank you all my task was done. – Ghazanfar Ateeb Aug 28 '19 at 20:15

1 Answers1

1

So I worked a bit on your code, and managed to maybe make it work as you intended it do to. It really wasn't such a big deal. I found a cleaner and easier to read code.

The changes in the code are 1st in the beginning :

#include <iostream>
void max(int a, int b, int c);
int a, b, c;
int main()
{
for(int p = 0; p < 3; p++) {
    std::cout << "\n\n";
    std::cout << "Enter 1st number :\t";
    std::cin >> a;
    std::cout << "Enter 2nd number :\t";
    std::cin >> b;
    std::cout << "Enter 3rd number :\t";
    std::cin >> c;
    max(a, b, c);
    std::cout << "\n\n\n";
}
system("pause");
return 0;
}

You can see I removed the using namespace std; because I really don't like having that, you can add it if you want it it's just my preference. You see I made the whole code repeat in a for loop to remove the calling of the main function, and don't worry many of us have done it too. As you can see I also changes the max() function's parameters The second change in the code is in the last part of the code:

void max(int a, int b, int c)
{

if (a > b&&a > c)
{
    std::cout << a << " is maximum";
}
else if (b > a&&b > c)
{
    std::cout << b << " is maximum";
}
else
{
    std::cout << c << " is maximum";
}
}

Here I just removed the part of the code which calls the main function. And overall it's all that had to change in your code to work.

  • `using namespace std;` is pretty much unanimously viewed as a bad idea by experienced developers. There are cases where it's not so bad (private .cpp files, and even then it's not a good idea) but nobody should complain that you didn't use it. If anybody complains that you removed it, you can safely ignore them. – François Andrieux Aug 28 '19 at 18:56
  • Congratulations on fixing your code. One note, that `while` loop where you loop, increment a variable until it reaches a value, is more clearly expressed by a `for` loop. – François Andrieux Aug 28 '19 at 18:58
  • Yeah a for loop would do it, It really was the first thing that came to my mind with the while loop, thanks for noting that. @FrançoisAndrieux – xX randomryze Xx Aug 28 '19 at 19:01
  • My task was to find can we call main() in any other function or not. Therefore, my first priority was to find whether the programs works or not. Secondly, if it works what kind of behavior is observed and the third priority was to find why it is haopening. So I got the answers. Thank you all my work is done. – Ghazanfar Ateeb Aug 28 '19 at 20:14