comma-operator

Why does this use of comma work in a expression but fail in a declaration?

纵饮孤独 提交于 2019-12-01 06:19:40
Am from high level OOP languages C# and Java and recently started scratching my head in C. I feel C a bit weird as equally as one feels JS is. So want to clarify below: Below gives error and that seems intuitive as it looks like incorrect syntax even in OOP languages int i=0,1,2; /* Error : expected identifier or ‘(’ before numeric constant int i = 0, 1, 2; ^ */ However below works surprisingly: int i; i = 0,1,2; //works Why is this behavior? Is their any significance to keep such behavior or just some parsing technicalities? This is actually a tricky question because it relies on the details

Why is this double initialization with a comma illegal?

戏子无情 提交于 2019-12-01 04:13:08
问题 I have three code snippets. This one: 1,7; //yes, that's all the code compiles okay. This one: double d = (1, 7); also compiles okay. Yet this one: double d = 1, 7; fails to compile. gcc-4.3.4 says error: expected unqualified-id before numeric constant and Visual C++ 10 says error C2059: syntax error : 'constant' Why such difference? Why don't all the three compile with , having the same effect in all three? 回答1: In the first two cases, the statements are using C++'s comma operator In the

Why does this use of comma work in a expression but fail in a declaration?

那年仲夏 提交于 2019-12-01 03:05:58
问题 Am from high level OOP languages C# and Java and recently started scratching my head in C. I feel C a bit weird as equally as one feels JS is. So want to clarify below: Below gives error and that seems intuitive as it looks like incorrect syntax even in OOP languages int i=0,1,2; /* Error : expected identifier or ‘(’ before numeric constant int i = 0, 1, 2; ^ */ However below works surprisingly: int i; i = 0,1,2; //works Why is this behavior? Is their any significance to keep such behavior or

Java - comma operator outside for loop declaration

元气小坏坏 提交于 2019-11-30 19:47:34
I know I can use the comma operator like this for (int i = 1, j = 15; j>10; i++, j--) { // do something neat } but some articles seem to suggest that the comma operator can be used outside of the for loop declaration, for example int j = 2, k = 4 ; int x ; // Assignment statement with comma operator x = j + 1, k ; source: http://www.cs.umd.edu/~clin/MoreJava/ControlFlow/comma.html or int x = (expression) ? (i++,2) : 3; source: https://stackoverflow.com/a/12047433/1084813 This would be a neat trick for a code obfuscation contest or to confuse my colleagues, but neither of the examples will

Comma operator in c [duplicate]

大城市里の小女人 提交于 2019-11-30 16:47:48
This question already has an answer here: What does the comma operator , do? 8 answers #include<stdio.h> int main(void) { int a=(1, 2), 3; printf("%d", a); return 0; } output: 2 Can any one explain how output is 2? Can any one explain how output is 2? In the statement a = (1, 2), 3; , used is a comma operator . Due to higher operator precedence of = operator than that of , operator, the expression operand (1, 2) will bind to = as (a = (1, 2)), 3; In case of comma operator, the left operand of a comma operator is evaluated to a void expression, then the right operand is evaluated and the result

C++ overloading operator comma for variadic arguments

大城市里の小女人 提交于 2019-11-30 13:54:13
is it possible to construct variadic arguments for function by overloading operator comma of the argument? i want to see an example how to do so.., maybe something like this: template <typename T> class ArgList { public: ArgList(const T& a); ArgList<T>& operator,(const T& a,const T& b); } //declaration void myFunction(ArgList<int> list); //in use: myFunction(1,2,3,4); //or maybe: myFunction(ArgList<int>(1),2,3,4); It is sort-of possible, but the usage won't look very nice. For exxample: #include <vector> #include <iostream> #include <algorithm> #include <iterator> template <class T> class list

Is the comma operator allowed in a constant-expression in C++11?

半腔热情 提交于 2019-11-30 08:05:20
In the process of answering this question on SO for C++11, I realized that in C++03 (as well as in C) the use of the comma operator is explicitly forbidden in a constant-expression . Paragraph 5.19/1 of the C++03 Standard on constant expressions says: [...] In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used . In C++11, however, that last part mentioning the comma operator seems to be vanished. And while paragraph 5.19/2 of the C++11 Standard

Comma operator returns first value instead of second in argument list?

↘锁芯ラ 提交于 2019-11-30 03:57:07
MDN claims that: The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand . However, when I tried running <script> alert(1, 2); </script> , it shows a "1" instead of a "2". Am I misunderstanding something? In the context of a function call, the comma is used to separate parameters from each other. So what you're doing is passing a second parameter to alert() which gets silently ignored. What you want is possible this way: alert((1,2)); The extra brackets form a parameter on their own; inside them you can use the comma as an operator.

Javascript “tuple” notation: what is its point?

我是研究僧i 提交于 2019-11-30 00:02:29
At wtfjs , I found that the following is legal javascript. ",,," == Array((null,'cool',false,NaN,4)); // true The argument (null,'cool',false,NaN,4) looks like a tuple to me, but javascript does not have tuples! Some quick tests in my javascript console yields the following. var t = (null,'cool',false,NaN,4); // t = 4 (null,'cool',false,NaN,4) === 4; // true (alert('hello'), 42); // shows the alert and returns 42 It appears to behave exactly like a semicolon ; separated list of statements, simply returning the value of the last statement. Is there a reference somewhere that describes this

Comma Operator with indexing 2D arrays

夙愿已清 提交于 2019-11-29 18:23:48
I have this algorithm that is pseudocode for the dijkstra algorithm for graph theory. The first thing that goes on is a basic for loop. visitedSet[0] = true //visitedSet is a array of bools for (int i = 1; i <= numberNodes; ++i) { distanceArray[i] = adjacencyMatrix[0,i]; //distanceArray is 1D with size of fifty //adjacencyMatrix is 2D with size of fifty //Both arrays hold values of unsigned ints } Here are the array definitions enum GraphLimit300 {MAX_NODES = 50}; unsigned int adjacencyMatrix[MAX_NODES][MAX_NODES]; unsigned int distanceArray[MAX_NODES]; Visual studio is giving me an array