What Does a Comma Separated List of Values, Enclosed in Parenthesis Mean in C? a = (1, 2, 3);

What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3);

This is the comma operator: evaluation of a, b first causes a to be evaluated, then b, and the result is that of b.

int a = (1, 2, 3); first evaluates 1, then 2, finally 3, and uses that last 3 to initialise a. It is useless here, but it can be useful when the left operand of , has side effects (usually: when it's a function call).

Why does putting parentheses around a list of comma separated values change the assignment?

First one is equivalent to (i = 1),2,3,4,5; which means the commas have no effect. When used with parentheses it returns the last value in the "comma delimited list"

This is all due to operator precedence, which you can view a table about here

Please explain the comma operator in this program

The , operator evaluates a series of expressions and returns the value of the last.

c=a,b is the same as (c=a),b. That is why c is 10

c=(a,b) will assign the result of a,b, which is 20, to c.

As Mike points out in the comments, assignment (=) has higher precedence than comma

How does the Comma Operator work

It would be equal to b.

The comma operator has a lower precedence than assignment.

JavaScript expressions, comma delimited within parentheses

In some programming languages (including c++ and js), the comma operator stands for "Evaluate all the expressions separated by commas and return the last one". For example:

var x = (true, false, 1);
console.log(x) // 1

Here's another one to show you that expressions are evaluated:

var i = 0;
var j = 0;
var x = (i++, j--);
console.log(i, j, x) // 1 -1 0

The function AddWaterMark essentially can be rewritten as:

function AddWatermark(controlName, defaultValue, cssValue) {
if (document.getElementById(controlName).value == "") {
document.getElementById(controlName).value = defaultValue;
document.getElementById(controlName).className = cssValue;
}
}

Comma operator in condition of loop in C

Comma operator evaluates i<0 Or i>0 and ignores. Hence, it's always the 5 that's present in the condition.

So it's equivalent to:

for(i=0;5;i++)

How do I use variables with stored user input from a function within another function?

You cannot return more than one value. One option is to pass pointers to those variables to a function instead:

void Variables(int *a, int *b, int *c)
{
printf("input variables");
scanf("%d", a);
scanf("%d", b);
scanf("%d", c);
printf("%d:%d:%d\n", *a, *b, *c);
}

int main(void)
{
int a, b, c;
Variables(&a, &b, &c)
printf("\n%d:%d:%d\n", a, b, c);
return 0;
}

Notice there are additional & and *.

Another options is to do this using structures:

struct variables {
int a, b, c;
};

struct variables Variables(void)
{
struct variables var;
scanf("%d", &var.a);
scanf("%d", &var.b);
scanf("%d", &var.c);
return var;
}

int main(void)
{
struct variables var = Variables();
printf("\n%d:%d:%d\n", var.a, var.b, var.c);
return 0;
}

You have to understand comma , operator. This why your code didn't work as you expected. You can read about it here:

  • How does the Comma Operator work
  • What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3);


Related Topics



Leave a reply



Submit