473,657 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1820
"pod" <ma************ **@googlemail.c omwrote:
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.c omwrote:
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.xs4al l.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwr ote:
>"pod" <ma************ **@googlemail.c omwrote:
>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...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article <45e8384c.88450 2...@news.xs4al l.nl>,

Richard Bos <r...@hoekstr a-uitgeverij.nlwr ote:
"pod" <marcus.d.poll. ..@googlemail.c omwrote:
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.nlwr ote in message
news:45******** ********@news.x s4all.nl...
"pod" <ma************ **@googlemail.c omwrote:
>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...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article <45e8384c.88450 2...@news.xs4al l.nl>,

Richard Bos <r...@hoekstr a-uitgeverij.nlwr ote:
>"pod" <marcus.d.poll. ..@googlemail.c omwrote:
>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.c omwrote:
On 2 Mar, 15:05, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
Richard Bos <r...@hoekstr a-uitgeverij.nlwr ote:
>"pod" <marcus.d.poll. ..@googlemail.c omwrote:
>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

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

Similar topics

122
5279
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) { /* array differs from value, do something*/
107
4099
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 variables and hence possible bugs. Your thoughts on the above would be welcome (as an aside), but my main problem follows. Now I've recently upgraded to gcc 4, and I find I'm missing the compiler warnings I used to get on gcc 3 regarding...
9
2210
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 mod_python. Although it is possible to reset the class variables between invocations of the system, this has the potential of 'wiping out' these variables when another user is using the system. Is there a way of getting the equivalent of 'local class...
9
2134
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 main(int argc, char * argv) { valarray<floatdata; float init=0; for(int i=0; i<argc; i++)
33
7162
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 array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.