473,321 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

initializing a local variable using a function

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
Jul 2 '08 #1
5 1830
iu2 wrote:
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".

--
"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

Jul 2 '08 #2
Hi

On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
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.
Jul 2 '08 #3
viza said:
Hi

On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
>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
Jul 2 '08 #4
viza wrote:
Chris Dollin wrote:
>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.
Jul 2 '08 #5
viza <to******@gmil.comwrites:
On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
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) ks***@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"
Jul 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
17
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
9
by: tai | last post by:
Hi. I'm looking for a way to define a function that's only effective inside specified function. Featurewise, here's what I want to do: bar_plugin_func = function() { ...; setTimeout(...);...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
5
by: 2b|!2b==? | last post by:
I have a class that contains C structs as member variables. By C structs, I mean they cannot have ctors/dtors because they have C linkage (extern "C"). For eg: MyClass { //Impl private:
4
by: majsta | last post by:
hello, I have probably a stupid question, but I don't get one thing. In the following source, the first constructor "does not work" - it creates a pointer variable pI, sets ii as *pI, but this...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.