Connecting Tech Pros Worldwide Forums | Help | Site Map

initializing a local variable using a function

iu2
Guest
 
Posts: n/a
#1: Jul 2 '08
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

Chris Dollin
Guest
 
Posts: n/a
#2: Jul 2 '08

re: initializing a local variable using a function


iu2 wrote:
Quote:
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?
Quote:
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".

--
"Only he had not reckoned with the ways of Estcarp." /Witch World/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

viza
Guest
 
Posts: n/a
#3: Jul 2 '08

re: initializing a local variable using a function


Hi

On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
Quote:
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.
Richard Heathfield
Guest
 
Posts: n/a
#4: Jul 2 '08

re: initializing a local variable using a function


viza said:
Quote:
Hi
>
On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
>
Quote:
>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?
Yes, it enables ALL the warnings... that the gcc team think are worth
bothering with.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google Groups users, please read: <http://improve-usenet.org/>
"Usenet is a strange place" - dmr 29 July 1999
CBFalconer
Guest
 
Posts: n/a
#5: Jul 2 '08

re: initializing a local variable using a function


viza wrote:
Quote:
Chris Dollin wrote:
>
Quote:
>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.
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


Keith Thompson
Guest
 
Posts: n/a
#6: Jul 2 '08

re: initializing a local variable using a function


viza <tom.viza@gmil.comwrites:
Quote:
On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
>
Quote:
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.
No, -Wall doesn't enable all warnings. See the gcc documentation.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Closed Thread