Topic: can Variable initialized by function?
Author: Evan Zeng<evanzengcn@gmail.com>
Date: Fri, 7 Oct 2011 16:08:58 -0700 (PDT)
Raw View
hi, here i met a problem: such as following code:
int func(int a,int b)
{
return a+b;
}
int main()
{
int i=func(1,2);
}
And it compiled sucessefully by g++, while has error by gcc. Can
anyone explain it in details?
thanks
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?=<daniel.kruegler@googlemail.com>
Date: Sat, 8 Oct 2011 09:36:07 -0700 (PDT) Raw View
Am 08.10.2011 01:08, schrieb Evan Zeng:
> hi, here i met a problem: such as following code:
> int func(int a,int b)
> {
> return a+b;
> }
> int main()
> {
> int i=func(1,2);
> }
> And it compiled sucessefully by g++, while has error by gcc. Can
> anyone explain it in details?
It would have been much more helpful, if you had provided the concrete
error message to us. Without this, we can only guess.
Here is my guess: g++ invokes a C++ compiler, gcc invokes a C compiler.
The program is a well-formed C++ program from the first C++ standard on.
But I think that at least in C89/C90 the program invokes undefined
behaviour, if main is exited without providing a return value. From C99
on it is well-defined to return no value from main, this has the same
effect as returning 0.
HTH& Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: pasa<pasa@lib.hu>
Date: Sat, 15 Oct 2011 09:28:09 -0700 (PDT)
Raw View
"Evan Zeng"<evanzengcn@gmail.com>
at global scope:
> int i=func(1,2);
> And it compiled sucessefully by g++, while has error by gcc. Can
> anyone explain it in details?
This is good in C++ and uses dynamic init before entering main(),
calling
func() to provide initial value of i.
But C has only static init, you can only use initializers vith
constant
expression, so the compiler can figure out the value right ahead.
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]