ksap
ksap

Reputation: 43

What is this C behavior called

I'm learning C as Javascript developer and a common mistake that I make is when I'm supposed to define multiple variables in C like this

int a, b, c;
a = b = c = 0;

I accidentally do it the Javascript way

a,b,c = 0;

I'm wondering what the above is called and when I should define variables like this.

Upvotes: 0

Views: 143

Answers (4)

H.S.
H.S.

Reputation: 12679

I'm wondering what the above is called....

Two things about this statement

a,b,c = 0;

First, this is same as

a;
b;
c = 0;

The expression a; and b; result is unused.

The compiler must be throwing warning messages on this statement. When I compiled with clang compiler, I am getting following warnings:

p.c:6:2: warning: expression result unused [-Wunused-value]
        a, b, c = 9;

        ^
p.c:6:5: warning: expression result unused [-Wunused-value]
        a, b, c = 9;
           ^

and Second, the , in the statement is , (comma) operator.

Precisely stated, the meaning of the comma operator in the general expression

e1 , e2

is evaluate the subexpression e1 and discards the result , then evaluate e2; the value of the expression is the value of e2.

So, the value of expression a,b,c = 0 is value of c = 0. The variable a and b will remain uninitialised.

May you can try this and check the variable values after this statement:

a = 99, b = 5, c = 0;

Since you are learning C, let me tell you one more thing - The , (comma) act as separator in function calls and definitions, variable declarations, enum declarations, and similar constructs. To begin with, check this.

and when I should define variables like this.

The statement a,b,c = 0; is not definition of variable a, b and c. You have defined the variable a, b and c here

int a, b, c;

Note that in this statement, the , is act as separator.

Its use is completely depends on you as long as you know very well about it. One of the very common use of , (comma) operator is in for loop, where it can be used to initialise multiple variables and/or increment/decrement loop counter variable and other variables etc., for example :

for (i = 0, j = some_num; i < some_num; ++i, --j) ....

Upvotes: 6

H Hasan
H Hasan

Reputation: 55

In c language you can define the variable using one line

    int a,b,c;

This is same as

    int a;
    int b;
    int c;

You can define and initialize this one like this

    int a,b,c;
    a=b=c=10;

if you write

    int a,b,c;
    a,b,c=10;

it will initialize c as 10 but others will not inilizes as 10; a and b will be defined but not initialized it will return garbage value.

Upvotes: -2

The comma used as an operator is called the comma operator.

For details, read a C standard, like n1570 (§6.5.17) or later. The semantics is defined as:

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value

Of course, inside function calls like printf("x=%d y=%d\n", x, y) the comma is separating arguments. Inside macro invocations and definitions also. In that case, it is the comma punctuator (see n1570 §6.5.2, 6.7, 6.7.2.1, 6.7.2.2,6.7.2.3, 6.7.9)

If you compile with GCC, invoke it as gcc -Wall -Wextra -g. You could get useful warnings.

Consider, if so allowed, to use the Clang static analyzer (or other tools like Frama-C or Bismon; you may contact me in 2021 by email to [email protected])

Take inspiration from the source code of existing free software programs like GNU make or GNU bash. They rarely use the comma operator.

Upvotes: 1

mediocrevegetable1
mediocrevegetable1

Reputation: 4217

Divide it up by the operators:

int a, b;
a = b = 10;
^   ^^^^^^
|   Right operand
Left operand

Here, first it calculates b = 10 to assign to a. b = 10 "returns" the new value of b which is 10, and that is assigned to a. It works similarly with more variables:

int a, b, c;
a = b = c = 10;
        ^^^^^^-Done first
    ^^^^^-Done second
^^^^^-Done last

a, b, c = 10 does not work the same way. First of all, you have to declare them first before assigning. Also, this uses the , operator which has a lower precedence than =, so the line is equivalent to a; b; c = 10;. As you can see, nothing really happens to a or b and only c is set to 10.

Upvotes: 1

Related Questions