syntax question

C

Chris

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

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Chris said:
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;
 
P

Philip Potter

Chris said:
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
 
T

Thomas Tutone

jimmy said:
Nils O. SelÃ¥sdal 写é“:


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
 
P

Philip Potter

jimmy said:
Nils O. SelÃ¥sdal 写é“:


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

Frederick Gotham

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);
 
J

jimmy

Philip Potter 写é“:
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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top