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

V

vlsidesign

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.
 
F

furious_joe

vlsidesign said:
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
 
F

furious_joe

furious_joe said:
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.
 
V

vlsidesign

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.
 
T

TJ

vlsidesign said:
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
 
R

Richard Heathfield

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;
 
S

Sensei

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?
 
C

CBFalconer

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.
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/>
 
T

TJ

x = 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
 
R

Richard Heathfield

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?

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.
 
R

rzed

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?
 
B

Barry Schwarz

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
 
C

Chris Torek

x = y = z = whatever;
[/QUOTE][/QUOTE]
Sensei said:

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.)
 
R

Random832

2006-12-24 said:
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.
 
R

Richard Heathfield

Chris Torek said:

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

vlsidesign

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.
 
C

CBFalconer

vlsidesign said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top