473,324 Members | 2,193 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,324 software developers and data experts.

Initialising variables with the comma

pod
Hello

Using ANSI C...

The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?

cheers
m

Mar 2 '07 #1
10 1804
"pod" <ma**************@googlemail.comwrote:
The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?
Depends. If this line occurs outside any function definition (hence with
file scope, and therefore with static duration), then the default static
object initialisation will set them to 0. If it's inside a function
definition, then (because of the lack of an explicit static keyword)
they're automatic objects, and must therefore be assumed to contain
garbage values.

Richard
Mar 2 '07 #2
On Mar 2, 2:39 pm, "pod" <marcus.d.poll...@googlemail.comwrote:
Hello

Using ANSI C...

The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?

cheers
m
Not if they have automatic storage duration, just as I wouldn't expect

char*x,y,z; /* whitespace ommited to avoid style arguments */

to allocate three pointers. If declared with file scope, it seems
likely that they would all be initialised to 0 regardless of whether
there's an =0 there or not (although I can't tell without knowing the
definition of UINT16 (I can guess, but that's not the same as
knowing), and I've come across broken implementations that fail to do
this initialisation so I generally initialise by hand anyway).
--
ais523

Mar 2 '07 #3
In article <45****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
>"pod" <ma**************@googlemail.comwrote:
>The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?

Depends. If this line occurs outside any function definition (hence with
file scope, and therefore with static duration), then the default static
object initialisation will set them to 0. If it's inside a function
definition, then (because of the lack of an explicit static keyword)
they're automatic objects, and must therefore be assumed to contain
garbage values.

Richard
Of course, nobody is addressing the real point here, which is that the
line above doesn't (alas) mean what you think it means (in C).

What it does mean is:

UINT16 x
UINT16 y
UINT16 z = 0

(leaving aside for the moment the missing semicolons)

Mar 2 '07 #4
pod wrote:
Hello

Using ANSI C...

The following code is causing a bit of bother....

UINT16 x, y, z = 0
[Note: UINT16 isn't an ANSI C type.]
Would people expect x and y and z to be set to 0 ?
`z` is explicitly initialised to 0.

`x` and `y` will be 0 if this is a non-automatic declaration,
and garbage otherwise.

The initialiser applies to the /most recent/ declarator, viz
`z`, not all of them.

--
Chris "electric hedgehog" Dollin
"- no longer a stranger to the truth." - The Reasoning, /Awakening/

Mar 2 '07 #5
pod
On 2 Mar, 15:05, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <45e8384c.884502...@news.xs4all.nl>,

Richard Bos <r...@hoekstra-uitgeverij.nlwrote:
"pod" <marcus.d.poll...@googlemail.comwrote:
The following code is causing a bit of bother....
UINT16 x, y, z = 0
Would people expect x and y and z to be set to 0 ?
Depends. If this line occurs outside any function definition (hence with
file scope, and therefore with static duration), then the default static
object initialisation will set them to 0. If it's inside a function
definition, then (because of the lack of an explicit static keyword)
they're automatic objects, and must therefore be assumed to contain
garbage values.
Richard

Of course, nobody is addressing the real point here, which is that the
line above doesn't (alas) mean what you think it means (in C).

What it does mean is:

UINT16 x
UINT16 y
UINT16 z = 0

(leaving aside for the moment the missing semicolons)- Hide quoted text -

- Show quoted text -
Kenny is correct.

Each variable is not treated the same.

Both x and y are garbage and z is set to zero !!

note. the scope of this declaration was within a function.

m

Mar 2 '07 #6
pod wrote:
>
Using ANSI C. The following code is causing a bit of bother.

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?
One period is enough to terminate a sentence. Edited accordingly.

No. Why should they? They should expect z to be set to 0, but
only if the UINT16 type exists, which is not guaranteed. As the
statement stands it should generate an error.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 2 '07 #7

"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message
news:45****************@news.xs4all.nl...
"pod" <ma**************@googlemail.comwrote:
>The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?

Depends. If this line occurs outside any function definition (hence with
file scope, and therefore with static duration), then the default static
object initialisation will set them to 0. If it's inside a function
definition, then (because of the lack of an explicit static keyword)
they're automatic objects, and must therefore be assumed to contain
garbage values.
A more revealing example would be
UINT16 x,y,z=1;

where, for file level scope, x and y are set to zero, and z to one.
For automatic values, x and y are garbage, z is one.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Mar 2 '07 #8
pod wrote:
On 2 Mar, 15:05, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <45e8384c.884502...@news.xs4all.nl>,

Richard Bos <r...@hoekstra-uitgeverij.nlwrote:
>"pod" <marcus.d.poll...@googlemail.comwrote:
>The following code is causing a bit of bother....
> UINT16 x, y, z = 0
>Would people expect x and y and z to be set to 0 ?
>Depends. If this line occurs outside any function definition (hence with
>file scope, and therefore with static duration), then the default static
>object initialisation will set them to 0. If it's inside a function
>definition, then (because of the lack of an explicit static keyword)
>they're automatic objects, and must therefore be assumed to contain
>garbage values.
Of course, nobody is addressing the real point here, which is that the
line above doesn't (alas) mean what you think it means (in C).

What it does mean is:

UINT16 x
UINT16 y
UINT16 z = 0

(leaving aside for the moment the missing semicolons)- Hide quoted text -

Kenny is correct.

Each variable is not treated the same.

Both x and y are garbage and z is set to zero !!

note. the scope of this declaration was within a function.
Yes, C's behaviour is like that. To initialise all the objects to
zero, do:

UINT16 x = 0, y = 0, z = 0;

or a stylistic variant like:

UINT16 x = 0;
UINT16 y = 0;
UINT16 z = 0;

As Richard noted, file scope or static objects not explicitly
initialised will be automatically set to zero.

Mar 2 '07 #9
"pod" <ma**************@googlemail.comwrote:
On 2 Mar, 15:05, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
Richard Bos <r...@hoekstra-uitgeverij.nlwrote:
>"pod" <marcus.d.poll...@googlemail.comwrote:
>The following code is causing a bit of bother....
> UINT16 x, y, z = 0
>Would people expect x and y and z to be set to 0 ?
>Depends. If this line occurs outside any function definition (hence with
>file scope, and therefore with static duration), then the default static
>object initialisation will set them to 0. If it's inside a function
>definition, then (because of the lack of an explicit static keyword)
>they're automatic objects, and must therefore be assumed to contain
>garbage values.
Of course, nobody is addressing the real point here, which is that the
line above doesn't (alas) mean what you think it means (in C).

What it does mean is:

UINT16 x
UINT16 y
UINT16 z = 0

(leaving aside for the moment the missing semicolons)- Hide quoted text -

Kenny is correct.

Each variable is not treated the same.

Both x and y are garbage and z is set to zero !!
Er... and how does any of this contradict what I wrote?

Richard
Mar 5 '07 #10
In article <45*****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
>"pod" <ma**************@googlemail.comwrote:
>On 2 Mar, 15:05, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
Richard Bos <r...@hoekstra-uitgeverij.nlwrote:
"pod" <marcus.d.poll...@googlemail.comwrote:

The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?

Depends. If this line occurs outside any function definition (hence with
file scope, and therefore with static duration), then the default static
object initialisation will set them to 0. If it's inside a function
definition, then (because of the lack of an explicit static keyword)
they're automatic objects, and must therefore be assumed to contain
garbage values.

Of course, nobody is addressing the real point here, which is that the
line above doesn't (alas) mean what you think it means (in C).

What it does mean is:

UINT16 x
UINT16 y
UINT16 z = 0

(leaving aside for the moment the missing semicolons)- Hide quoted text -

Kenny is correct.

Each variable is not treated the same.

Both x and y are garbage and z is set to zero !!

Er... and how does any of this contradict what I wrote?

Richard
It doesn't - and I never said that it did. It just seems to me (and
others) that you (and others of your ilk) were focussing on an obscure
esoteric point (that it would "work" as "expected" *if* the variables
declared were globals) (yes, I know that's another entry on the clc "DO
NOT SAY" list!), rather than on what is almost certainly OP's actual
problem, which is that (Yes, I'm being a little sloppy here, but we all
know what I mean) only the "int" part propagates across the list. It is
equivalent to the fact that:

char* a,b,c;

doesn't declare 3 pointer variables.

P.S. Now that I think about it, isn't "variable" on the "DO NOT SAY"
list as well?

P.P.S. Notice how much extra work one has to go to, to post in clc, to
keep the animals in their cages?

Mar 5 '07 #11

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

Similar topics

122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
9
by: Rory Campbell-Lange | last post by:
We have a set of classes using static methods to retain reference variables between operations. The problem is that the static variables are not reset between operations when used through...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.