syntax question

Discussion in 'C++' started by Chris, Sep 4, 2006.

  1. Chris

    Chris Guest

    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!
     
    Chris, Sep 4, 2006
    #1
    1. Advertising

  2. 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;
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 4, 2006
    #2
    1. Advertising

  3. Chris

    Chris Guest

    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;
     
    Chris, Sep 4, 2006
    #3
  4. Chris

    jimmy Guest

    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;
     
    jimmy, Sep 4, 2006
    #4
  5. "Chris" <> wrote in message
    news:...
    > 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
     
    Philip Potter, Sep 4, 2006
    #5
  6. 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
     
    Thomas Tutone, Sep 4, 2006
    #6
  7. >"jimmy" <> wrote in message
    >news:...
    >
    >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)
     
    Philip Potter, Sep 4, 2006
    #7
  8. 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
     
    Frederick Gotham, Sep 4, 2006
    #8
  9. Chris

    jimmy Guest

    Philip Potter 写é“:

    > >"jimmy" <> wrote in message
    > >news:...
    > >
    > >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.
     
    jimmy, Sep 9, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabriele renzi
    Replies:
    2
    Views:
    242
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    246
  3. Good Night Moon
    Replies:
    9
    Views:
    319
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    352
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    348
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page