-4

The following declarations that include the comma are not clear to me; about how the left side variables are mapped to the right side values. Therefore what would be the values assigned to the left side variables in each case?

Declaration 1:

long int x, y, z = d[0], k, len;

Declaration 2:

long int x, y, z = d[0], k;

(consider the long int d[100] array is initiated earlier and the values are assigned.)

Dami
  • 677
  • 6
  • 12
  • 5
    What exactly are you confused about? The first declaration creates 5 `long ints`, while the second one creates 4. – Arnav Borborah Feb 13 '18 at 20:59
  • What exactly confuses you? Those declarations refer to the 1st element declared in the `d` array. –  Feb 13 '18 at 21:00
  • 3
    A good idea to avoid confusion is to declare each variable separately. Saving characters were *possibly* good for K&R having a 10 characters/second interface for their [Teletype terminal](https://edition.cnn.com/2011/10/14/tech/innovation/dennis-ritchie-obit-bell-labs/index.html). – Bo Persson Feb 13 '18 at 21:00
  • See this question: https://stackoverflow.com/questions/6838408/how-can-i-declare-and-define-multiple-variables-in-one-line-using-c – Anon Mail Feb 13 '18 at 21:00
  • 2
    Flagged for _off-topic_ closure as _unclear_. –  Feb 13 '18 at 21:02
  • 1
    "I'm confused" is _not_ a clear programming question. – Drew Dormann Feb 13 '18 at 21:07
  • _@DaminduLiyanage_ And never use uninitialized variables BTW! –  Feb 13 '18 at 21:17
  • @AnonMail that's what I was looking for. Thanks. – Dami Feb 14 '18 at 04:21
  • Oh no, this is so stupid there is no left or right side. This is a declaration separated by the comma and one variable given the value. This made my day. I just looked at it the wrong way. – Dami Feb 14 '18 at 04:36

1 Answers1

2
long int x, y, z = d[0], k, len;

This declares 5 variables, all are long int.

x, y, k and len are uninitialized.

z is initialized with the value of d[0].

Maybe it's easier to understand the declaration if you write it like this:

long int x, 
         y, 
         z = d[0], 
         k, 
         len;
Sid S
  • 6,037
  • 2
  • 18
  • 24
  • _"Maybe it's easier to understand ..."_ I'm still biased if this (simple formatting) is good advice. Mentioning _better practice_ to use separate variable declarations (especially in regards of pointers or references) should be worth it. –  Feb 13 '18 at 21:30
  • @TheDude, Best practice is to write clear code. To me, the above is more clear than what you suggest. – Sid S Feb 13 '18 at 21:34
  • Wait! Having separate variable declarations is _less clear_ than simply breaking down _multiline_ variable declarations? Seriously? –  Feb 13 '18 at 21:51
  • Yes, I think so. You don't think so. So my opinion is not the same as your opinion. Big deal. – Sid S Feb 13 '18 at 23:54