0

In one of my assignments I am seeing this line used:

int index = -1, k;

Im not sure what is happening when there are to entries for the one variable. What exactly is the variable "index" holding when it has two entries?

vsoftco
  • 52,188
  • 7
  • 109
  • 221
  • 3
    An assignment? Get your money back. This is really bad coding style and your instructors should know better. –  Oct 15 '15 at 23:12
  • @Rhymoid some assignments use *bad style* intentionally so you get trained understanding even weird-looking constructs in C. –  Oct 15 '15 at 23:17
  • Is it not ambiguous? How the compiler knows that the comma here is not being used as the comma operator. After all, `index` is being assigned to the value of an expression, and the expression -1,k is valid... unless maybe k is not defined. I wonder what would happen if I do: `int k=3; int index=-1,k;`. Should it assign 3 to index then? – mcleod_ideafix Oct 15 '15 at 23:18
  • @mcleod_ideafix a definition is NOT an assignment. Try your example, it will just be rejected as a redeclaration. –  Oct 15 '15 at 23:20
  • But the initialization is, and the right side of the initialization could be any valid expression, including one with the comma operator. Of course I tried it and as you said, it doesn't compile, but if I write: `int k=3; int index=(-1,k);` it works – mcleod_ideafix Oct 15 '15 at 23:22
  • @mcleod_ideafix sure you *can* use a comma operator, because any **constant** expression is ok. Still it's a declaration and in a declaration, the comma separates identifiers to be declared, so there's no ambiguity. –  Oct 15 '15 at 23:24

6 Answers6

4

This is the definition of 2 variables, both of the same type int: index and k. Only index is initialized with -1, and k is left un-initialized.

vsoftco
  • 52,188
  • 7
  • 109
  • 221
  • "This is the definition of 2 variables" - no, it's not. Definition != Declaration – Mecki Oct 15 '15 at 23:36
  • 4
    @Mecki Those **are** definitions, you allocate storage. `extern int j;` is a declaration! See e.g. http://stackoverflow.com/a/1410632/3093378 So please don't downvote unless you are 100% sure. – vsoftco Oct 15 '15 at 23:37
  • I would so prefer if they chose declaration and instantiation, rather than two words that are mildly ambiguous and look similar. – Russ Schultz Oct 15 '15 at 23:49
  • @vsoftco downvoted me as well, obviously in some act of revenge about my downvote for his partially incorrect answer. Just don't care about that :) –  Oct 15 '15 at 23:54
  • 1
    Wrong source. SO is not authoritative. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf ISO-C says storage is not required for a variable unless you ever use it beyond the declaration statement. E.g. page 123 says `int *pi;` is a declaration, even though it is not `external` and a pointer would need some space (usually 4/8 bytes) but it will only need space when ever being initialized or assigned or referenced by address. – Mecki Oct 16 '15 at 00:11
  • 1
    ISO C stanadard Page 129: `consider a declaration -- T D1 -- where T contains the declaration specifiers that specify a type T (such as int) and D1 is a declarator that contains an identifier ident.`. Page 137: `typedef T type_ident; type_ident D;` Both are named declarations one line above and that holds true for any type `T`, also if `T` is `int`. – Mecki Oct 16 '15 at 00:14
  • @Mecki very funny. To use your own words: show me one C compiler on earth that doesn't allocate space as soon as a variable declaration is seen. Haha. But I guess you still don't get what's wrong with your answer. –  Oct 16 '15 at 00:16
  • @FelixPalmen Easy. I can name you three: Neither GCC, nor Clang, nor MSVC will if you write `int a;` in a function and don't ever use it beyond that. They will see that it is unused and not reserve any space for it on stack as there is absolutely no need to waste stack space on a guaranteed unused variable. – Mecki Oct 16 '15 at 00:21
  • @Mecki as I thought, you still don't get it. (and just in case: that, too, is the typical result of optimizations being done. But it **doesn't matter**. C abstracts from the machine for a reason.) –  Oct 16 '15 at 00:24
  • @FelixPalmen Removing `int a = 1;` when it is not used beyond that is an optimization. Removing just `int a;` when a is never assigned nor addressed is not an optimization as removing an unused declaration cannot really be called an optimization. – Mecki Oct 16 '15 at 00:35
  • @Mecki sure it is, allocating it would be equally valid in terms of the C abstract machine. Stop thinking in machine code, that doesn't help here. Talk about declarations and definitions, THEN you have a point that it should be correctly described as *declaring* one variable and *defining* another. Much more helpful to the OP would be the concept of initialization. –  Oct 16 '15 at 00:38
  • @FelixPalmen I think not calling a declaration a definition, as you did in your answer would be much more helpful for the OP and for everyone else on this planet. While I'm pretty sure everyone with only tiny C knowledge understands my answer and note that the word "equivalent" that you keep arguing for over an our now is not even in my answer! So you are not even arguing about my answer. – Mecki Oct 16 '15 at 00:42
  • @Mecki no, you write "the same", which is an even stronger error. –  Oct 16 '15 at 00:43
  • Correct, concise and opinion-free. Nice answer. – Andy Thomas Oct 16 '15 at 00:49
  • 1
    @Mecki [Related question](http://stackoverflow.com/q/33163028/3093378), so maybe we can figure out exactly what's going on. – vsoftco Oct 16 '15 at 05:39
2

It's just defining two variables of type int, one of them (index) is initialized to -1, the other (k) is left uninitialized.

This is btw bad style because it really looks confusing.

  • There is no such thing as good or bad style as there is no objective definition of these terms, no God of style or style laws. Just because you consider it bad style doesn't make it bad style. Your personal taste/preference is just how you think about it and that is subjective. – Mecki Oct 15 '15 at 23:38
  • Yes there is. And in your answer, I see why. –  Oct 15 '15 at 23:39
  • And you are also misusing the word "defining" according to ISO C standard. You are trying to nitpick on my answer and then you make such a mistake? How about you start nitpicking on your own answers? – Mecki Oct 16 '15 at 00:23
  • @Mecki I'm not the one talking about "equivalence" of code that clearly has differing semantics. Just stop your crap. –  Oct 16 '15 at 00:25
  • I never said the code is equivalent in terms of the C standard, I said it is just equivalent, meaning written the way I have written it, it will do exactly the same thing, regardless of C compile, platform, code, system or the phases of the moon (that's what people call equivalence by a definition of that word that predates computers by centuries). – Mecki Oct 16 '15 at 00:38
  • @Mecki still wrong, "equivalence" says "meaing the same" ... and it doesn't! Leading to the same executable code (given you assume it's allocated on the stack, which isn't even obvious from the question) on today's typical architectures doesn't make it equivalent. –  Oct 16 '15 at 00:40
1

As several other people have said, this is a declaration of two variables. It is 100% equivalent to

int index = -1;
int k;

and modern coding style would encourage you to write it that way. However - there is a lot of old C out there, and in the 1980s and 1990s, grouping variable declarations was the preferred style. Pick a random dusty deck and I guarantee you you'll see things like

register f, r, (*hstat)(), (*istat)(), (*qstat)();
int status;

or maybe

extern char level;
int newtry[31],newother[31],*r,*q,*p,n,sum,first;
int ii,lastwhite,lastred;

So you need to understand what it means. Sadly, they can get quite confusing, e.g.

int const* a, b;

which is equivalent to

const int *a;
int b;

and that sort of thing is why modern coding styles prefer one variable per declaration.

(Why did people prefer to group declarations back in the day? I don't know. Personally, I would guess that it helps you see more code at once on your 80x25 glass tty, but I've never actually had that experience, so.)

zwol
  • 121,956
  • 33
  • 219
  • 328
  • Good answer ... just a guess, maybe back in these "dark days", it might have helped the compiler to perform its task in less time :) –  Oct 16 '15 at 01:07
0

In this code "index" and "k" both are integer type variable and the variable "index" is assigned by -1.(i.e. the value of index is -1). It's called variable initialization.

Md Mahfuzur Rahman
  • 2,219
  • 2
  • 16
  • 27
-2

Assuming this is code within the scope of a function:

int index = -1, k;

will do just the same as

int index = -1;
int k;

or the same as

int index, k;
index = -1;

even if the code is not equivalent by the C standard, no known C compiler in the world would treat these three code blocks any different .

In C you can declare multiple variables of the same type at once (int index, k;) or you define a variable (declare and initialize a variable at the same time, int index = -1;), or you can do both at once, declare multiple and initialize them or just one of them as in your case (int index = -1, k;).

Mecki
  • 106,869
  • 31
  • 201
  • 225
  • 1
    Your last claim is wrong. `int index = -1;` is an initialization, while `int index; index = -1;` is just an assignment. –  Oct 15 '15 at 23:15
  • 1
    An initialiser is not the same as an assignment. Try `int a[3] = { 1,2,3};` vs. `int a[3]; a = {1,2,3}`. – too honest for this site Oct 15 '15 at 23:16
  • Also the fact that we can use , and = in the same declaration and initialization because , has a lower priority (actually lowest). – Samboy786 Oct 15 '15 at 23:16
  • @Samboy786: If there is an initialisation, it is a definition, not (just) a declaration. – too honest for this site Oct 15 '15 at 23:17
  • @Olaf for the other variable ( k here ) , its still a declaration ... right – Samboy786 Oct 15 '15 at 23:18
  • @FelixPalmen The last sentence does not even refer to the assignment, the assignment is only there for code equivalence, but the sentence only referred to the first line of the last code sample. – Mecki Oct 15 '15 at 23:21
  • I don't know what you're talking about now, but the code is **not** equivalent. –  Oct 15 '15 at 23:22
  • @FelixPalmen Show me just one C compiler on earth, that will not emit exactly the same code for all 3 code blocks in my post. – Mecki Oct 15 '15 at 23:44
  • 1
    @Mecki that doesn't make them equivalent. try it with static storage duration and you will see the difference even in the executable code emitted (if not, disable optimizations) –  Oct 15 '15 at 23:45
  • @FelixPalmen If you add static storage you make different statement! Did I write static anywhere? No. So what is that for a nonsense saying "Hey, if you make a different statement out of it, it will be different". Within a function (and this code obviously to all readers was meant to be function scope) and exactly the way I wrote it (and not by adding any other keywords), every C compiler on earth will treat all three codes block as equivalent and emit exactly same code and everything is stupid nitpicking for no reason and without being helpful. – Mecki Oct 16 '15 at 00:02
  • @Mecki please don't expose any more being clueless and just read up on *static storage duration* ... it's about scopes and the *keyword* "static" isn't necessary for it. **And again** even if a compiler emits exactly the same executable code for two programs, it **doesn't** make them "equivalent" in terms of C. –  Oct 16 '15 at 00:04
  • @Olaf I was never talking about an assignment anywhere. I was just placing an assignment to reach equivalent code (not by ISO-C terms but by what a compiler makes out of it) and for a non-static scalar (like int), there is no functional difference between initializing or assigning (I don't know any C compiler in the world that would not treat them the same for non-static scalars). Your sample already fails because an array is not a scalar and I was not talking about non-scalars, I was referring to the concrete code of the question and nothing else. – Mecki Oct 16 '15 at 00:32
  • 1
    @Mecki and the edit doesn't help, too ... if you're so keen about the assignment scenario, write it leads to the same code on your typical stack-based C implementation ... THAT would be correct. But it is not the same! –  Oct 16 '15 at 00:48
  • @FelixPalmen Unless someone can show me one C compiler in the word that doesn't treat the code the same, I will stick to that statement. I have not to proof that my statement is right, it's you who claim it is wrong. Okay, then disproof it. Should be easy if I'm wrong. Calling people wrong but then being unable to disproof them is so poor. And that's it. – Mecki Oct 16 '15 at 00:52
  • 1
    @Mecki for the disproof, read the C standard. It doesn't know anything about a stack (just a hint). –  Oct 16 '15 at 00:52
  • @FelixPalmen Only when you talk about something else but scalars... what we don't do here, we talk about scalars. This is like you always talking about static. I talk about apples and you an Olaf come here and say "when we talk about pears"... yes, when we talk about pears, but we don't do that, we talk about apples (at least I did) so keep your pears out of it. – Mecki Oct 16 '15 at 01:02
  • @Mecki some very basic question here: do you think there is a different level of abstraction between C and assembler? –  Oct 16 '15 at 01:04
  • 1
    You are writing C code and not Assembler. So it is **not** the same! @FelixPalmen is perfectly right (and I do not use this phrase carelessly;-). Things were different if you just added something like "for this code". But as you insist to defend the general case, you are plain wrong (otherwise try to reword your answer). Another couter-example: `int j = -1, k = j;` Also, a declaration is no statement and adding a storage class specifier does not change its gramatical meaning. (Btw: in English, it is "oranges", not "Birnen") – too honest for this site Oct 16 '15 at 11:50
-2

In C, the comma operator , has lower precedence than the assignment operator =. As such, the expression

int index = -1, k;

is parsed as

//The parentheses are not legal in C, but it's what the parser does.
int ((index = -1), k);

You see, that the line declares variables of type int. The first one of which is called index and is initialized to -1, the second one is called k and is not initialized.

You can find a good overview of the operator precedences here:
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
Note that the comma operator is the very last one in that list!


Likewise, you may see C-code like this, or similar:

if(condition) foo += 7, doSomething();
while(i += 2, i < 42) ...;

This is equivalent to

if(condition) {
    foo += 7;
    doSomething();
}

i += 2;
while(i < 42) {
    ...
    i += 2;
}

but much more terse (many C programmers like terseness!). Again, in both cases the comma operator serves to fuse two things into a single statement, which avoids writing a full block {} and prevents repetition of the increment i += 2.

Whether such uses of the comma operator are good or bad is a matter of taste and circumstance. But you can be certain to find all possible uses of it in the wild.

cmaster - reinstate monica
  • 33,875
  • 7
  • 50
  • 100
  • 1
    Sorry, that's just wrong, in a declaration, the comma is *not* an operator but just separates declarators. And about C programmers liking terseness? well, that's opinionated and no programmer (worth his salary) likes confusing constructs. –  Oct 16 '15 at 00:23
  • @FelixPalmen You are right that my formulation is imprecise since not all commas in the code are actually instances of the comma operator. However, it is a helpful simplification. No matter how it is called grammatically, commas never have a higher precedence than any other operator. It is irrelevant whether you call `(foo = 7, bar)` a list of two expressions or two expressions separated by a comma when the question is how the `7` is interpreted. Yes, the comma serves a different function in the two interpretations, but the structure of the parse is the same. – cmaster - reinstate monica Oct 16 '15 at 07:31