Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the second int in a multiple declaration always set to 1?

In this Code Golf post, there is the claim that “the second variable in a definition is always set to 1“, making this a well-formed line:

int i=-1,c,o,w,b,e=b=w=o=c;

And supposedly everything except i is set to 1 because c is automatically 1.

I thought I knew some C, and thought this was illegal (being UB and resulting if anything in random stack contents).

Does C really set c to 1?

like image 520
Felix Dombek Avatar asked Jan 30 '17 21:01

Felix Dombek


People also ask

Can you initialize multiple variables in one line in C?

Example - Declaring multiple variables in a statementIf your variables are the same type, you can define multiple variables in one declaration statement. For example: int age, reach; In this example, two variables called age and reach would be defined as integers.

How to declare multiple variables in vb net?

You can declare several variables in one declaration statement, specifying the variable name for each one, and following each array name with parentheses. Multiple variables are separated by commas. If you declare more than one variable with one As clause, you cannot supply an initializer for that group of variables.

What is declaration in C with example?

Declaration of a function provides the compiler with the name of the function, the number and type of arguments it takes, and its return type. For example, consider the following code, int add(int, int); Here, a function named add is declared with 2 arguments of type int and return type int.

What is the purpose of variable declaration?

A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable.


2 Answers

I'm the OP from CodeGolf. It seems I simply had a typo, I meant to say int i=-1,c,o,w,b,e=b=w=o=c=1; That way the second defined int is always set to 1 and the others can be set to it. The confusion is that I originally had the variable that comes next (L=3) as just l (undefined) and I was setting all of the other variables to e=b=w=o=c=(L=3); which in my mind was going to set L equal to 3, return true for that (1), then set the rest to 1.

A few tests later I realized this was just setting them all to 3 and only worked with the specific string I was using to test my code. So I deleted them and changed it to just be L=3 hard coded and the others to be e=b=w=o=c=1;L=3. At some point I must have pressed cmd+z one too many times and removed the "=" and the "1" so I was just left with e=b=w=o=c;. Due to the consistent undefined nature of this (at least on my IDE) it was always defining them as 0 and therefor the bug went un-noticed.

Now that I've corrected it back, thanks to this post, the byte lengths are the same and there was no need for any of this tricky e=b=w=o=c=1 code anyways, I only thought the byte length was different because when I copy pasted my function into a byte counter it showed it was 2 bytes smaller (I didn't know I just had a typo and was missing 2 bytes).

My IDE is always defining those variables as 0. My code is designed to work with all of the variables being defined as 1, the fact that it works w/ 0 is coincidence. Also just because it happens on my IDE doesn't mean it will on others, though I have tested it on a few IDEs now online and run many loops and it does seem to always return 0. In any event, I've still updated my original code to set them to 1 as it should be (adding 2 bytes to my program).

Thanks for everyone's input

like image 157
Albert Renshaw Avatar answered Oct 13 '22 16:10

Albert Renshaw


This code exhibits undefined behavior.

The variables c, o, b, and w are uninitialized. That means their contents are indeterminate.

From section 6.7.9 of the C standard:

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

The indeterminate value of c is then assigned to several other variables. By reading the value of an uninitialized variable, the code invokes undefined behavior.

The initial value of c could be 1, but if so it's not a predictable value.

Also note that the above statement contains both initialization (for i and e) and assignment (for c, o, b, and w), so this statement won't compile at file scope.

I attempted to run the function in the linked post and it didn't pass the first test input. Undefined behavior.

like image 41
dbush Avatar answered Oct 13 '22 16:10

dbush