Thread: initializing variable

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    Smile initializing variable

    I have tried the following program and I am getting output as a:1 b:3. Can someone help me to understand why this behaviour.


    Code:
    int main()
    {
       int a = 1;2;3;
       int b = (1,2,3);
       
       printf("a:%d b:%d",a,b);
    
    
       return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Generally, the solution is to avoid writing such code in the first place. The reason is that this:
    Code:
    int a = 1;2;3;
    is the same as this except for formatting:
    Code:
    int a = 1;
    2;
    3;
    That is, you have code that initialises a with 1, and then two lines that have no net effect.

    The other thing is that for the expression (1,2,3), the comma operator is in use, which evaluates the left operand, then the right operand, with the result being the value of the right operand. Hence, you end up initialising b with 3. Sometimes this has some use when the left operand has a side effect, but in this case it doesn't so there's no point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Compile with lots of warnings enabled.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:4:14: warning: statement with no effect [-Wunused-value]
        4 |    int a = 1;2;3;
          |              ^
    foo.c:4:16: warning: statement with no effect [-Wunused-value]
        4 |    int a = 1;2;3;
          |                ^
    foo.c:5:14: warning: left-hand operand of comma expression has no effect [-Wunused-value]
        5 |    int b = (1,2,3);
          |              ^
    foo.c:5:16: warning: left-hand operand of comma expression has no effect [-Wunused-value]
        5 |    int b = (1,2,3);
          |                ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    >> Sometimes this has some use when the left operand has a side effect, but in this case it doesn't so there's no point.
    Can you please provide an example for the "," usage when the left operand has a side effect?

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    >> Sometimes this has some use when the left operand has a side effect, but in this case it doesn't so there's no point.
    Can you please provide an example for the "," usage when the left operand has a side effect?

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    The following assignment is giving compilation error(expected identifier)
    int a = 1,2,3;


    But the following is assigning a to 1.
    int a;
    a = 1,2,3;


    Can you help to understand.

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    > Can you help to understand.

    No. I'm not sure I can, but have a read of Comma in C and C++ - GeeksforGeeks.

    After 40 years of C programming this atypical use of the comma has never been a problem, and the compiler generates appropriate warnings and errors when it is.

    If it really bothers you, put the 1,2,3 in brackets.
    Last edited by hamster_nz; 12-04-2022 at 07:40 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The following assignment is giving compilation error(expected identifier)
    > int a = 1,2,3;
    Probably because , is lower precedence than =
    C Operator Precedence - cppreference.com

    Plus this is valid (see those identifiers).
    int a = 1, b = 2, c = 3;


    The only time I recall using the comma operator for anything near what it's for is say palindrome checking
    Code:
    for ( int i = 0, j = n-1 ; i < j ; i++, j-- ) {
      if ( arr[i] == arr[j] )
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing array of variable size
    By Anton in forum C Programming
    Replies: 11
    Last Post: 11-23-2011, 07:51 AM
  2. Problem with initializing tm * variable.
    By PaulBlay in forum C Programming
    Replies: 2
    Last Post: 04-21-2009, 08:08 AM
  3. initializing of strucutre variable
    By rajkumarmadhani in forum C Programming
    Replies: 9
    Last Post: 11-27-2007, 11:04 PM
  4. problem with initializing a variable
    By Junktyz in forum C Programming
    Replies: 3
    Last Post: 06-27-2007, 08:46 AM
  5. different way of initializing variable?
    By The Gweech in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2001, 06:44 PM

Tags for this Thread