comma-operator

C comma operator

本小妞迷上赌 提交于 2019-11-26 08:47:04
Why is the expression specified inside a comma operator (such as the example below) not considered a constant expression? For example, int a = (10,20) ; when given in global scope yields an error "initializer is not a constant", though both the expressions separated by a comma operator are constants (constant expressions). Why is the entire expression is not treated as a constant expression? For clarification I have read What does the ‘,’ operator do in C? and Uses of C comma operator . They have not dealt this aspect of comma operator. Section 6.6/3 , "Constant expressions", of the ISO C99

When is the comma operator useful?

旧时模样 提交于 2019-11-26 07:47:36
I read this question about the "comma operator" in expressions ( , ) and the MDN docs about it, but I can't think of a scenario where it is useful. So, when is the comma operator useful? pimvdb The following is probably not very useful as you don't write it yourself, but a minifier can shrink code using the comma operator. For example: if(x){foo();return bar()}else{return 1} would become: return x?(foo(),bar()):1 The ? : operator can be used now, since the comma operator (to a certain extent) allows for two statements to be written as one statement. This is useful in that it allows for some

What does i = (i, ++i, 1) + 1; do?

♀尐吖头ヾ 提交于 2019-11-26 05:55:49
问题 After reading this answer about undefined behavior and sequence points, I wrote a small program: #include <stdio.h> int main(void) { int i = 5; i = (i, ++i, 1) + 1; printf(\"%d\\n\", i); return 0; } The output is 2 . Oh God, I didn\'t see the decrement coming! What is happening here? Also, while compiling the above code, I got a warning saying: px.c:5:8: warning: left-hand operand of comma expression has no effect [-Wunused-value] i = (i, ++i, 1) + 1; ^ Why? But probably it will be

When is the comma operator useful?

主宰稳场 提交于 2019-11-26 02:09:25
问题 I read this question about the \"comma operator\" in expressions ( , ) and the MDN docs about it, but I can\'t think of a scenario where it is useful. So, when is the comma operator useful? 回答1: The following is probably not very useful as you don't write it yourself, but a minifier can shrink code using the comma operator. For example: if(x){foo();return bar()}else{return 1} would become: return x?(foo(),bar()):1 The ? : operator can be used now, since the comma operator (to a certain extent

C comma operator

流过昼夜 提交于 2019-11-26 02:00:59
问题 Why is the expression specified inside a comma operator (such as the example below) not considered a constant expression? For example, int a = (10,20) ; when given in global scope yields an error \"initializer is not a constant\", though both the expressions separated by a comma operator are constants (constant expressions). Why is the entire expression is not treated as a constant expression? For clarification I have read What does the ‘,’ operator do in C? and Uses of C comma operator. They

Uses of C comma operator [duplicate]

怎甘沉沦 提交于 2019-11-25 23:25:15
问题 This question already has an answer here: What does the comma operator , do? 8 answers You see it used in for loop statements, but it\'s legal syntax anywhere. What uses have you found for it elsewhere, if any? 回答1: C language (as well as C++) is historically a mix of two completely different programming styles, which one can refer to as "statement programming" and "expression programming". As you know, every procedural programming language normally supports such fundamental constructs as

How does the Comma Operator work

吃可爱长大的小学妹 提交于 2019-11-25 22:14:46
问题 How does the comma operator work in C++? For instance, if I do: a = b, c; Does a end up equaling b or c? (Yes, I know this is easy to test - just documenting on here for someone to find the answer quickly.) Update: This question has exposed a nuance when using the comma operator. Just to document this: a = b, c; // a is set to the value of b! a = (b, c); // a is set to the value of c! This question was actually inspired by a typo in code. What was intended to be a = b; c = d; Turned into a =

What does a comma do in JavaScript expressions?

天大地大妈咪最大 提交于 2019-11-25 21:43:38
问题 If I use: 1.09 * 1; // returns \"1.09\" But if I use: 1,09 * 1; // returns \"9\" I know that 1,09 isn\'t a number. What does the comma do in the last piece of code? More Examples if (0,9) alert(\"ok\"); // alert if (9,0) alert(\"ok\"); // don\'t alert alert(1); alert(2); alert(3); // 3 alerts alert(1), alert(2), alert(3); // 3 alerts too alert(\"2\", foo = function (param) { alert(param) }, foo(\'1\') ) foo(\'3\'); // alerts 1, 2 and 3 回答1: The comma operator evaluates both of its operands

What does the comma operator , do?

和自甴很熟 提交于 2019-11-25 21:37:03
问题 What does the , operator do in C? 回答1: The expression: (expression1, expression2) First expression1 is evaluated, then expression2 is evaluated, and the value of expression2 is returned for the whole expression. 回答2: I've seen used most in while loops: string s; while(read_string(s), s.len() > 5) { //do something } It will do the operation, then do a test based on a side-effect. The other way would be to do it like this: string s; read_string(s); while(s.len() > 5) { //do something read