Re: [Dev-C++] Question using "main" as a variable name
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: DC A <ad...@ho...> - 2008-04-07 19:00:20
|
> AD wrote:
> I am trying to learn c++ by reading a book named "C++ Primer Plus". In that book
> author says that quote:
>
> "You can use "main" as a variable name. This can cause a problem in circumstances
> too esoteric to describe here."
>
>
> So in a program that has main() function can also have a variable named main. But
> what can be the circumstance in which it can cause problem? Can anyone kindly
> shed light into this? Thanks.
>
>
>> Amynuttall25m wrote:
>> Because the main function returns an integer to the operating system, having
>> somthing like:
>>
>> int main = 5;
>>
>> is goint to cause a problem if used in a function.
>>
>> int return ( int main )
>> {
>> // do somthing
>>
>> return main;
>> }
Thanks for your reply. But i don't get what you're saying(My fault...sorry).
#include
using namespace std;
int a_func()
{
int main = 5;
return main;
}
int main()
{
int a_var = a_func();
return 0;
}
The above code compiles fine on my system. Here "main" is a variable defined in a
function called a_func() and main() function is also used. I know that main is
used as a variable when used in a statement like
return main;
because there's no parenthesis following the name of the variable. But if it were
a function i would've used:
return main();
Wouldn't I? So you can easily differentiate between the two. can you explain a little bit
more why this code can cause a problem as the author states that?
_________________________________________________________________
Find hidden words, unscramble celebrity names, or try the ultimate crossword puzzle with Live Search Games. Play now!
http://g.msn.ca/ca55/212
|