initializing a local variable using a function

I

iu2

Hi all,

This code compiles:

int func1()
{
return 3;
}

void func2()
{
int a = func1();
}

In func2 the initialization of 'a' is done by calling to func1().
Is this ok accroding to ANSI C? (I thougt this is legal only in C+
+...)
I need to know it because the project I work on will have to be ported
to a different compiler (other than gcc), and I need to use ANSI C.

Thanks
 
C

Chris Dollin

iu2 said:
Hi all,

This code compiles:

int func1()
{
return 3;
}

void func2()
{
int a = func1();
}

In func2 the initialization of 'a' is done by calling to func1().
Is this ok accroding to ANSI C? (I thougt this is legal only in C+
+...)

Initialising automatic variables can be done with arbitrary
expressions, including function calls.

Initialising /static/ variables -- those explicitly declarared
static, and variables declared outside functions -- requires a
compile-time constant [1]. Maybe that's what you're thinking of?
I need to know it because the project I work on will have to be ported
to a different compiler (other than gcc), and I need to use ANSI C.

Then `-ansi -pedantic` and a whole bunch of other stuff -- I appear
to use

-Wall -Wno-unused -Wwrite-strings -W -Wmissing-prototypes -fno-builtin

is your friend.

[1] And for pointer values, something which is essentially "address of
static possibly plus a constant".
 
V

viza

Hi

Then `-ansi -pedantic` and a whole bunch of other stuff -- I appear to
use

-Wall -Wno-unused -Wwrite-strings -W -Wmissing-prototypes -fno-builtin

Does -Wall really not include, um, all warnings? FFS gcc.
 
C

CBFalconer

viza said:
Does -Wall really not include, um, all warnings? FFS gcc.

No. To discover standardization problems with gcc use at least:

-W -Wall -ansi -pedantic

and I also recommend including -Wwrite-strings. For C99 (as far as
gcc has gone on implementation) replace -ansi with -std=c99.

Aha - you seem to be including all those, which I missed because of
the unusual (to me) ordering of the options.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top