473,769 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
21 2070
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
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
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
by the way "furious_jo e", 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
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
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
On 2006-12-24 10:01:02 +0100, Richard Heathfield <rj*@see.sig.in validsaid:
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
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
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.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Dec 24 '06 #10

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

Similar topics

28
3305
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
7
3563
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that you do not actually need to fork Python in order to implement prototypes. It seems to me that Python metaclasses + descriptors are more than powerful enough to implementing prototypes in pure Python. I wrote a module that implements part of what...
7
2209
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes is: > def f(this:that=other): > print this This means that f() has a 'this' parameter, of type 'that'. And 'other' is the default value.
56
3761
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that }
4
12580
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; document.getElementById('myelement') = new Function("myfnc(param1,param2,param3)");
121
10165
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
46
4255
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
4
1467
by: hufel | last post by:
Hi, I'm doing my first big project in C# and I'm stuck with a problem that I believe has a simple and efficient solution for it (I just haven't bumped into it yet...). The concept is the following: //Users manage clients. When the system creates the client it must fetch the information from the DB and allow some modifications: Client client = new Client("test"); MasterAccount ma = new MasterAccount(1324651);
669
26192
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
0
9583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9423
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
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9860
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
8869
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 projectplanning, coding, testing, and deploymentwithout 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...
0
6668
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
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
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.