By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,443 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,443 IT Pros & Developers. It's quick & easy.

what is the least amount of typing to assign the same value to multiple variables

P: n/a
This syntax does not to work
nl, nt, ns = 0;
The only one that get's initialized is ns.
nl and nt because they don't initialize seem to get some junk from
memory.

I have done these two versions that work:
nl = 0; nt = 0; ns = 0;
nl = nt = ns = 0;

I wonder what is the normal way it is done? In the context below I
wonder how the commas are interpreted as:
nl, nt, ns = 0;

Thanks for any insight.

Dec 24 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
vlsidesign wrote:
This syntax does not to work
nl, nt, ns = 0;
The only one that get's initialized is ns.
nl and nt because they don't initialize seem to get some junk from
memory.

I have done these two versions that work:
nl = 0; nt = 0; ns = 0;
nl = nt = ns = 0;

I wonder what is the normal way it is done? In the context below I
wonder how the commas are interpreted as:
nl, nt, ns = 0;

Thanks for any insight.
Just curious: why does it matter? If you need millions of variables,
just make and array, and initialize it with a loop. or use calloc(),
which works like malloc(), but initializes the condense to 0.
merry Xmas, Furious_joe
Dec 24 '06 #2

P: n/a
furious_joe wrote:
Just curious: why does it matter? If you need millions of variables,
just make and array, and initialize it with a loop. or use calloc(),
which works like malloc(), but initializes the condense to 0.
merry Xmas, Furious_joe
on line 3, between "make" and "array" is a reason to proofread, and not
trust a spell checker.
Dec 24 '06 #3

P: n/a
It doesn't, I am a newbie and just wondering. I am going through the
"The C programming language" by Kernighan and Ritchie, the Second
edition -- this book seems very good.

furious_joe wrote:
vlsidesign wrote:
This syntax does not to work
nl, nt, ns = 0;
The only one that get's initialized is ns.
nl and nt because they don't initialize seem to get some junk from
memory.

I have done these two versions that work:
nl = 0; nt = 0; ns = 0;
nl = nt = ns = 0;

I wonder what is the normal way it is done? In the context below I
wonder how the commas are interpreted as:
nl, nt, ns = 0;

Thanks for any insight.
Just curious: why does it matter? If you need millions of variables,
just make and array, and initialize it with a loop. or use calloc(),
which works like malloc(), but initializes the condense to 0.
merry Xmas, Furious_joe
Dec 24 '06 #4

P: n/a
by the way "furious_joe", thank you for the response, and Merry
Christmas.

furious_joe wrote:
furious_joe wrote:
Just curious: why does it matter? If you need millions of variables,
just make and array, and initialize it with a loop. or use calloc(),
which works like malloc(), but initializes the condense to 0.
merry Xmas, Furious_joe

on line 3, between "make" and "array" is a reason to proofread, and not
trust a spell checker.
Dec 24 '06 #5

P: n/a
TJ

vlsidesign wrote:
This syntax does not to work
nl, nt, ns = 0;
This is perfectly correct syntax, but it's not what you thik it is. The
comma operator evaluates it's operands from left to right. So in your
example, the first thing it does is evaluate the value of nl, and then
throw it away. Then it evals the value of nt, and throws that away too.
Now it evaluates "ns = 0", which assigns the value 0 to ns. So nl and
nt were not changed in any way...
The only one that get's initialized is ns.
nl and nt because they don't initialize seem to get some junk from
memory.
That's right. the "junk from memory" that's in nl and nt is probably
because you declared them as "auto" variables, and they're not declared
at file scope. So they just get whatever's on the stack at the time
they're created.

(Forgive me if I'm using "evaluate" wrongly. I've been reading up on
funtional languages and I can't remember if we use the same word for
C...)

TJ

Dec 24 '06 #6

P: n/a
vlsidesign said:

[Subject line: "what is the least amount of typing to assign the same value
to multiple variables"]

That's the wrong criterion for professional programming. Instead, ask
yourself "how can I make my intent clearest to a maintenance programmer?"

If what matters is that all of the objects have the same value, then do
this:

x = y = z = whatever;

If there is no connection between them, however, and the requirement to have
the same value is merely a coincidence (e.g. you're "zeroing them out"),
then do this:

x = whatever;
y = whatever;
z = whatever;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 24 '06 #7

P: n/a
On 2006-12-24 10:01:02 +0100, Richard Heathfield <rj*@see.sig.invalidsaid:
If what matters is that all of the objects have the same value, then do this:

x = y = z = whatever;

If there is no connection between them, however, and the requirement to
have the same value is merely a coincidence (e.g. you're "zeroing them
out"), then do this:

x = whatever;
y = whatever;
z = whatever;
I'm curious... Is this just syntactic sugar? In the sense that both of
them yield the same results, so they are not distinguishable in the
end. Is it so?

--
Sensei <senseiwa@Apple's mail>

Research (n.): a discovery already published by a chinese guy one month
before you, copying a russian who did it in the 60s.

Dec 24 '06 #8

P: n/a
TJ
x = whatever;
y = whatever;
z = whatever;

I'm curious... Is this just syntactic sugar? In the sense that both of
them yield the same results, so they are not distinguishable in the
end. Is it so?
No. "=" is an operator which returns a value, and is evaluated from
right to left. It can be used anywhere an expression can. So, an
expression containing "=" can be passed to "=" as an operand too.

TJ

Dec 24 '06 #9

P: n/a
vlsidesign wrote:
furious_joe wrote:
>vlsidesign wrote:
>>This syntax does not to work
nl, nt, ns = 0;
The only one that get's initialized is ns. nl and nt because
they don't initialize seem to get some junk from memory.

I have done these two versions that work:
nl = 0; nt = 0; ns = 0;
nl = nt = ns = 0;

I wonder what is the normal way it is done? In the context
below I wonder how the commas are interpreted as:
nl, nt, ns = 0;
This may cause undefined behaviour, because nl and nt may be
uninitialized, yet they are being evaluated and the result
discarded. See the comma operator in K&R.
>>
Just curious: why does it matter? If you need millions of
variables, just make and array, and initialize it with a loop.
or use calloc(), which works like malloc(), but initializes the
condense to 0.

It doesn't, I am a newbie and just wondering. I am going through
the "The C programming language" by Kernighan and Ritchie, the
Second edition -- this book seems very good.
It is, and you will learn a lot from it.

I habitually use the "a = b = c = 0;" form. This has the potential
of generating very efficient code, although many compilers miss
here in optimization.

For automatic storage I tend to avoid initializers in the
definitions, unless it significantly adds to the clarity. Such
initializers generate code in the function body, and you don't have
a way to re-execute that code. So, to me, they just hide things to
no purpose. Many will disagree, some violently.

Please don't top-post. Your answer belongs after, or intermixed
with, the material you quote, after snipping portions not germane
to your answer. See the following links:

--
Some informative links:
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Dec 24 '06 #10

P: n/a
Sensei said:
On 2006-12-24 10:01:02 +0100, Richard Heathfield <rj*@see.sig.invalid>
said:
>If what matters is that all of the objects have the same value, then do
this:

x = y = z = whatever;

If there is no connection between them, however, and the requirement to
have the same value is merely a coincidence (e.g. you're "zeroing them
out"), then do this:

x = whatever;
y = whatever;
z = whatever;

I'm curious... Is this just syntactic sugar? In the sense that both of
them yield the same results, so they are not distinguishable in the
end. Is it so?
Well, yes, in this case. The compiler won't care either way. The reason I
drew a distinction between them is that the "x = y = z = whatever" version
suggests that "there is a value, 'whatever', which - at this point in the
program - it is important for x, y, and z to share", whereas the other
version suggests that each assignment is independent of the others. It's a
very minor point, but it may help in some small way to convey to the
maintenance programmer the intent of the original programmer.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 24 '06 #11

P: n/a
furious_joe <ri***********@gmail.comwrote in
news:em**********@aioe.org:
furious_joe wrote:
>Just curious: why does it matter? If you need millions of
variables, just make and array, and initialize it with a loop.
or use calloc(), which works like malloc(), but initializes
the condense to 0.
merry Xmas, Furious_joe

on line 3, between "make" and "array" is a reason to proofread,
and not trust a spell checker.
So are you saying that "condense" (for contents) is *not* a reason to
proofread?

--
rzed
Dec 24 '06 #12

P: n/a
On Sun, 24 Dec 2006 18:24:51 +1100, furious_joe
<ri***********@gmail.comwrote:
>vlsidesign wrote:
>This syntax does not to work
nl, nt, ns = 0;
The only one that get's initialized is ns.
nl and nt because they don't initialize seem to get some junk from
memory.

I have done these two versions that work:
nl = 0; nt = 0; ns = 0;
nl = nt = ns = 0;

I wonder what is the normal way it is done? In the context below I
wonder how the commas are interpreted as:
nl, nt, ns = 0;

Thanks for any insight.
Just curious: why does it matter? If you need millions of variables,
just make and array, and initialize it with a loop. or use calloc(),
which works like malloc(), but initializes the condense to 0.
Only for integer types. calloc initializes storage to all bits zero
which need not represent 0.0 for floating point types or NULL for
pointer types.
Remove del for email
Dec 24 '06 #13

P: n/a
>On 2006-12-24 10:01:02 +0100, Richard Heathfield <rj*@see.sig.invalid>
>said:
>>x = y = z = whatever;
>Sensei said:
>I'm curious... Is this just syntactic sugar? In the sense that both of
them yield the same results, so they are not distinguishable in the
end. Is it so?
In article <AO******************************@bt.com>
Richard Heathfield <rj*@see.sig.invalidwrote:
>Well, yes, in this case.
More precisely: yes, unless the types of x, y, and z are such that
the answer is "no". :-)

For instance, consider:

unsigned char x;
unsigned short y;
unsigned long z;

x = y = z = ULONG_MAX;

The effect here is to set z to ULONG_MAX, then assign ULONG_MAX to
y so that it is set to USHRT_MAX, then assign USHRT_MAX to x so
that it is set to UCHAR_MAX (assuming UCHAR_MAX <= USHRT_MAX and
USHRT_MAX <= ULONG_MAX, which I am not sure is entirely required
but is reasonably assume-able).

Note that if we change the names on the declarations or (easier
in this case) the order of the assignments, the result changes:

z = y = x = ULONG_MAX;

The effect here is to set x to UCHAR_MAX, then set y to the value
stored in x, then set z to the value stored in y. If, as is usually
the case, UCHAR_MAX < USHRT_MAX, this sets all three to just
UCHAR_MAX. Only if the three MAXes are all equal does this result
in all three being set to ULONG_MAX (which is then the same as
UCHAR_MAX).
>... The reason I drew a distinction between them is that the
"x = y = z = whatever" version suggests that "there is a value,
'whatever', which - at this point in the program - it is important
for x, y, and z to share", whereas the other version suggests that
each assignment is independent of the others. It's a very minor
point, but it may help in some small way to convey to the maintenance
programmer the intent of the original programmer.
I would add that, unless the types of x, y, and z are all the same,
it is often unwise to use the "multiple assignment" form. (If the
types *are* the same, there is no possibility that one of the
assignments modifies the value along the way. It also works if
the values are "obviously unmodified" -- for instance, if all the
variables are scalars and the value is a tiny integer. Hence, the
form with "x = y = z = 0" is OK even if x, y, and z are different
integral types, or one or more are floating-point. Still, the ice
is thin here: you skate on it at your own risk.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 24 '06 #14

P: n/a
2006-12-24 <em*********@news2.newsguy.com>,
Chris Torek wrote:
More precisely: yes, unless the types of x, y, and z are such that
the answer is "no". :-)
[etc]

Right.

To summarize,

x = y = z = whatever;

is equivalent to this:

z = whatever;
y = z;
x = y;

and NOT this.

x = whatever;
y = whatever;
z = whatever;

And should be used only if you fully understand the implications of
this fact.
Dec 24 '06 #15

P: n/a
Chris Torek said:

<snip>
I would add that, unless the types of x, y, and z are all the same,
it is often unwise to use the "multiple assignment" form. (If the
types *are* the same, there is no possibility that one of the
assignments modifies the value along the way.
One good nit deserves another. :-)

The rightmost assignment *might* modify the value being assigned. For
example, where UINT_MAX UCHAR_MAX,

unsigned char x, y, z;
x = y = z = UINT_MAX;

sees the value UINT_MAX being modified for the assignment to z (and thus to
y and x).

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 25 '06 #16

P: n/a
Please don't top-post. Your answer belongs after, or intermixed
with, the material you quote, after snipping portions not germane
to your answer. See the following links:

--
Some informative links:
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Thanks for these links, and clueing me in. These are very good for
newbies like me.

Dec 25 '06 #17

P: n/a
vlsidesign wrote:
Please don't top-post. Your answer belongs after, or intermixed
with, the material you quote, after snipping portions not germane
to your answer. See the following links:

--
Some informative links:
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>

Thanks for these links, and clueing me in. These are very good for
newbies like me.
First newbie I've heard say that :)

Dec 25 '06 #18

P: n/a
In article <11**********************@42g2000cwt.googlegroups. com>,
vlsidesign <fo*****@gmail.comwrote:
>Please don't top-post. Your answer belongs after, or intermixed
with, the material you quote, after snipping portions not germane
to your answer. See the following links:

--
Some informative links:
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>

Thanks for these links, and clueing me in. These are very good for
newbies like me.
Glad you liked them. Here are some even more important and useful links
that you should familiarize yourself with if you intend to continue
posting here:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Dec 25 '06 #19

P: n/a
vlsidesign wrote:
>
>Please don't top-post. Your answer belongs after, or intermixed
with, the material you quote, after snipping portions not germane
to your answer. See the following links:

--
Some informative links:
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>

Thanks for these links, and clueing me in. These are very good for
newbies like me.
Well done. You obviously have read and understood them. This is
the sort of reaction that encourages me (and others) to give such
advice. Just one more fairly minor point, please don't snip
attributions for material you quote. The attributions are the "joe
wrote:" lines.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Dec 25 '06 #20

P: n/a
Kenny McCormack wrote:
In article <11**********************@42g2000cwt.googlegroups. com>,
vlsidesign <fo*****@gmail.comwrote:
Please don't top-post. Your answer belongs after, or intermixed
with, the material you quote, after snipping portions not germane
to your answer. See the following links:

--
Some informative links:
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Thanks for these links, and clueing me in. These are very good for
newbies like me.

Glad you liked them. Here are some even more important and useful links
that you should familiarize yourself with if you intend to continue
posting here:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language
Cool, I'll read up on these too. Thanks.

Dec 26 '06 #21

P: n/a
vlsidesign said:
Kenny McCormack wrote:
<snip>
>>
http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Cool, I'll read up on these too. Thanks.
The Wiki article on C is flawed (or at least it was the last time I looked,
and probably will be the next time I look, too), and the other two are
irrelevant to the language and to this newsgroup. You Have Been Trolled.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 26 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.