Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what is the output of this programme
C++
#include <stdio.h>

int main(int x)
{
    printf("Hello, World!\n");
main(10);
    return 0;
}


and suppose if we mention main(10)
without mentioning its prototype as main(int x) then why is the result still being printed multiple times?
and suppose if i use the main(void) term then what it actually means...are we calling main 10 times or so?
Posted
Updated 19-Jan-15 6:48am
v2
Comments
PIEBALDconsult 19-Jan-15 12:50pm    
These are all things you can try yourself.
Member 11324568 19-Jan-15 12:53pm    
I have tried ...I got mtiple printed hello worlds.....I didn't understand why it got printed that many times
Richard MacCutchan 19-Jan-15 12:56pm    
Because main is calling itself so it never ends.
PIEBALDconsult 19-Jan-15 12:57pm    
Yes. That's what you told it to do.
KarstenK 19-Jan-15 13:11pm    
you shouldnt call a function main(). It is only too confusing, because it is the standard entry point in C++.

That program runs twice, because inside the main function you're again making a call to the function and guess what, it will never end... It will continously call the main function and pass that (useless) parameter of value 10 to it, and again print the message on the screen.

Using main(void) means that you're not passing any parameter at all, whereas main(int a) means a parameter of integer data type is required, but it doesn't cut short the number of times the hello world will print. If you want to, you can write it as,

C++
#include <stdio.h>

int main(int x)
{
    // create a loop to print the values
    for (int i = 0; i < x; i++)
       printf("Hello, World!\n");
    return 0;
}


Call the main function and pass the value now. You can use the same logic, to call the main function 10 times by using a for loop and so on.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 19-Jan-15 14:58pm    
Ha-ha, 5.
—SA
Afzaal Ahmad Zeeshan 19-Jan-15 14:59pm    
Thanks a lot for your vote, Sergey Sir. :-)
Have also a look at this page: C-recursion[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jan-15 14:55pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900