473,497 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

syntax question

can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?

thanks for any help!

Sep 4 '06 #1
8 2182
Chris wrote:
can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?
Not in an initializer.

You can however do
int x,y,z;
x = y = z = 5;
Sep 4 '06 #2
oh, duh. Thanks
Nils O. Selåsdal wrote:
Chris wrote:
can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?
Not in an initializer.

You can however do
int x,y,z;
x = y = z = 5;
Sep 4 '06 #3

Nils O. SelÃ¥sdal 写é“:
Chris wrote:
can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?
Not in an initializer.
But in fact it works in an initializer, at least in my VC++.
int x = y = z = 5;

You can however do
int x,y,z;
x = y = z = 5;
Sep 4 '06 #4
jimmy wrote:
Nils O. SelÃ¥sdal 写é“:
Chris wrote:
can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?
Not in an initializer.

But in fact it works in an initializer, at least in my VC++.
int x = y = z = 5;
Only if y and z were already declared.

Try the following on your VC++ and please report back on the results:

int main()
{
int x = y = z = 5;
}

Best regards,

Tom

Sep 4 '06 #5
"Chris" <ch*********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?

thanks for any help!
try:
int x=5, y=5, z=5;

Philip

Sep 4 '06 #6
>"jimmy" <tb****@gmail.comwrote in message
>news:11**********************@m73g2000cwd.googleg roups.com...

Nils O. SelÃ¥sdal 写é“:
>Chris wrote:
can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?
Not in an initializer.

But in fact it works in an initializer, at least in my VC++.
int x = y = z = 5;
That's funny, it doesn't work on my g++. Can we talk about Standard C++ now?

Philip

me@here~% cat tmp.cpp
int main(void) {
int x = y = z = 5;
return 0;
}
me@here~% g++ -ansi -pedantic tmp.cpp -otmp
tmp.cpp: In function `int main()':
tmp.cpp:2: `y' undeclared (first use this function)
tmp.cpp:2: (Each undeclared identifier is reported only once for each
function
it appears in.)
tmp.cpp:2: `z' undeclared (first use this function)

Sep 4 '06 #7
Chris posted:
can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?

Your question is ambiguous.

Are you defining objects and initialising them, or are you simply performing
an assignment with previously defined objects?

If the former, then:

int const val=5;

int x=val,y=val,z=val;

If the latter:

z=5;

x=y=z;

(Assignment binds from right to left, so that is equivalent to:

x=(y=z);
--

Frederick Gotham
Sep 4 '06 #8

Philip Potter 写é“:
"jimmy" <tb****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...

Nils O. SelÃ¥sdal 写é“:
Chris wrote:
can you assign values to variables like:
int x, y, z = 5;
so that x = 5, y = 5, and z = 5?
Not in an initializer.
But in fact it works in an initializer, at least in my VC++.
int x = y = z = 5;

That's funny, it doesn't work on my g++. Can we talk about Standard C++ now?

Philip

me@here~% cat tmp.cpp
int main(void) {
int x = y = z = 5;
return 0;
}
me@here~% g++ -ansi -pedantic tmp.cpp -otmp
tmp.cpp: In function `int main()':
tmp.cpp:2: `y' undeclared (first use this function)
tmp.cpp:2: (Each undeclared identifier is reported only once for each
function
it appears in.)
tmp.cpp:2: `z' undeclared (first use this function)
yes , i agree with you.

Sep 9 '06 #9

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

Similar topics

699
33308
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
4
2041
by: Aaron Walker | last post by:
Greetings, I'm attempting to write my first *real* template function that also deals with a map of strings to member function pointers that is making the syntax a little tricky to get right. ...
2
1877
by: bor_kev | last post by:
Hi, First of all, i want to use the new managed class syntax and STL.NET under Microsoft Visual (C++) Studio 2005 Beta. I read in a Microsoft...
5
4471
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
3
1487
by: astromog | last post by:
I have some significantly extended syntax for Python that I need to create a reference implementation for. My new syntax includes new keywords, statements and objects that are sort of like classes...
11
5185
by: deppy_3 | last post by:
Hi! The syntax of fputs() is similar with the syntax of fgets(); For example if we have:fgets(str,maxlen,stdin) which is the syntax of the fputs();
8
3129
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
2
5187
by: berrylthird | last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example: var myInstance:myClass = new...
17
3106
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
6
2537
by: Daniel | last post by:
I hope this question is OK for this list. I've downloaded Rpyc and placed it in my site packages dir. On some machines it works fine, on others not so much. Here is one error I get when I try...
0
7160
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,...
0
7196
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...
1
6878
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...
0
7373
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...
0
5456
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,...
1
4897
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...
0
4583
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...
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.