270 questions
26
votes
3
answers
5k
views
Is `(expression, lvalue) = rvalue` a valid assignment in C or C++? Why do some compilers accept/reject it?
Some time ago I stumbled upon the idea of how a C construct, such as (expr0, expr1, expr2), evaluates (see "What does the comma operator , do?" for more context).
I've started experimenting ...
2
votes
4
answers
125
views
wierd syntax with parentheses in C [duplicate]
I was given the code:
#include <stdio.h>
int main(void) {
int a = 0, b = 0, c = 0;
c = (a -= a - 5), (a = b, b + 3);
printf("a = %d, b = %d, c = %d", a, b, c); // a = 0, b =...
3
votes
2
answers
143
views
What is C++ doing when I use the comma operator in the initializer expressions?
My program was crashing, no debugger readily available, so I spiked it with printfs, narrowed it down to the (lengthy) initializer list, and tried to get it to show which initializer expression causes ...
2
votes
1
answer
90
views
JavaScript: Are (0, x.f) and (x.f) really different
x={f(){return this}}
x.f() //x
(x.f)() //x
(0,x.f)() //window
I've learned that parenthesis (grouping operator) does not apply GetValue to the expression inside it, while other operators such as , ...
0
votes
1
answer
57
views
How to only call left side of comma (,) separator
const test = (
(success) => {
console.log('SUCCESS CALLED');
},
(error) => {
console.log('ERROR CALLED');
}
);
test(); // output is **ERROR CALLED**
In ...
1
vote
3
answers
83
views
Syntax to execute some code before initializing member class objects
Is there a way to execute some code as a precursor to initializing class members? Take the following code as an example:
void enableCreationOfAObjects();
class AObject
{
public:
AObject(int i, ...
0
votes
2
answers
78
views
a parenthesis expression assigned to single lvalue [duplicate]
Why does this code compile ?
int X = (2,4);
I compiled this line with c++ replit
the second value (4) is assigned to X
2
votes
4
answers
166
views
List context and the comma operator in Perl
Having some significant confusion about what it means to "evaluate in list context", specifically related to the comma operator. In the linked perlop doc, it says: In list context, it's just ...
1
vote
2
answers
102
views
What's the syntax of following c define? [duplicate]
Following snippet is from mac sdk signal.h:
#define sigemptyset(set) (*(set) = 0, 0)
just wonder what does , 0) do?
0
votes
3
answers
233
views
c++: What does 'while (cin >> variable, variable)' exactly do?
So here's that piece of code, very basic but i just can't find any similar questions that have two variables in the while loop with cin in it.
void Perm(int start, int end, int a[]) {
//some ...
1
vote
1
answer
105
views
decltype evaluating the wrong type from an expression list
while experimenting with the answer from This post,
ended up with the following piece of code:
#include <iostream>
#include <typeinfo>
namespace Test
{
struct MyPtr
{
...
3
votes
3
answers
89
views
What would be the output obtained out of the program and how?
#include<stdio.h>
void main() {
int g = 83;
int h = (g++, ++g);
printf(“%d”, h);
}
**g++** will increment **g** after **;**
My answer: h = 84
Correct answer: h = 85
I am a ...
2
votes
1
answer
52
views
Comma operator with undeclared variable - why does it compile?
Why does this code not throw a compilation error for y being undeclared?
int x = 10, y;
printf("%d", y);
There's no expression like int y;. In my case, the console print out is 32764, ...
1
vote
2
answers
135
views
Function returns different numbers when called in another function
\
I've created a practice project for my test and I can't seem to get around this problem. I need a function that grabs the input and when called in another function, the input will be used to solve a ...
1
vote
5
answers
1k
views
Using multiple instructions inside ternary operator in C
I am new to learning C and I would like to know if it is possible to launch more than one instruction inside ternary operator in C - for example:
int a = 5;
int b = 7;
int max;
int min;
max = (a>b) ...
0
votes
1
answer
56
views
What determines whether the right operand of comma operator is evaluated? [duplicate]
I am confused on the rules on the evaluation of the comma operator operands within a ternary conditional. Why are ++x, --a, and --b evaluated but not ++y?
#include <iostream>
int main()
{
...
0
votes
2
answers
2k
views
Google Sheet - Add Timestamp When Any Cell on Specific Sheet is Modified
I have a Google Sheet file that has many tabs (sheets) in it. What I am looking to do is place a timestamp when any cell on a specific page is updated or modified.
Sheet name = "Data-Dashboard&...
0
votes
1
answer
96
views
Apparent GCC bug with comma operator in constant initializer
I was messing around with C and discovered an apparent bug in GCC:
#include <stdio.h>
int y = (1, 2);
int main()
{
return 0;
}
in gcc gives error: initializer element is not constant while ...
1
vote
5
answers
470
views
Question on while (true), while (cin >> x && x), while (cin >> x, x)
I have encountered a problem with this task:
your input is a number and the output is from 1 to the input number. If the number is zero, the output will be nothing.
For instance your input is
5
10
3
...
0
votes
3
answers
95
views
Error: 'else' without a previous "if' even though there is not a semicolon [closed]
I made a very simple program but even though there is not a semicolon, I still get this error. Please ignore the weird intention of this program, it's for comedy purposes.
#include <iostream>
#...
1
vote
1
answer
89
views
Comma operator with IIFEs [duplicate]
Inside of the react-redux library, in the file defining useSelector, I have found multiple instances of comma operators used with IIFEs in the follow manner:
var _useReducer = (0, _react....
0
votes
1
answer
101
views
Using macro as a range in C. Nested Loops. Logic
I am trying to make this program which takes YYYY and MM from the user. I have defined macros outside the main function, which is kinda range for years to be taken as input. For months, i have ...
1
vote
1
answer
49
views
Multiple arguments to the index of the array in c
I went through the following code :
#include <stdio.h>
#define N 10000000
int main() {
static int rot[N], dd[N], qu[N];
int a, t, i, j, p, head, cnt;
scanf("%d%d", &a,...
1
vote
2
answers
75
views
A very strange way of writing arrays [duplicate]
template<typename T>
void print(T &t) {
cout << t << " ";
}
template<typename ...Ts>
void gogo(Ts&& ...agvs) {
int arr[] = { (print(agvs),0)... }...
1
vote
2
answers
47
views
Why does using parenthesis make a multi dimentional array behave like this?
I accidentally covered the numbers with these instead of the curly brackets normally used and got "2 4 0 0". Why does this shifting happen?
#include<stdio.h>
#include<stdlib.h>
#...
1
vote
0
answers
84
views
confusion of comma operator in variable initializations
If i'm not wrong, (1,2,3,4,5) expression will evaluate to 5( the order of evaluation is from left to right), then why int m({1,2,3,4,5}) is not being compiled fine?
Following is the compile-time error ...
-1
votes
2
answers
281
views
assigning tuple to an int variable and printing the variable
I tried assigning a tuple to an int variable. Here is my code. i = (12, 45, 58, -1, 90, 100); When I print i, it shows the value of 100. How does tuple work in C language ?
Thanks
6
votes
1
answer
532
views
The full story about the decltype + comma trick
The title should clarify what my confusion is about. I'd like to use this question to get a comprehensive answer which helps me understand how the comma operator works with decltype within SFINAE ...
3
votes
1
answer
139
views
C++ Templates - The Complete Guide: Wording of footnote about decltype and return type
The 2nd edition of C++ Templates - The Complete Guide features the following footnote at page 436 (my bold):
Except that decltype(call-expression) does not require a nonreference, non-void return ...
0
votes
2
answers
737
views
Executing several actions in parallel with std::jthread and comma operator in C++20
In C++20 we got a new thread-class std::jthread that unlike old std::thread
waits
for thread termination in the destructor. So it becomes easy to execute several actions in parallel using unnamed ...
1
vote
1
answer
155
views
Trouble in differentiating the Comma operator vs comma separator
I am having trouble differentiating comma as operator or separator. Have gone through various questions but none explains when the comma acts a operator vs when it acts as separator. The answers just ...
-3
votes
1
answer
73
views
Unexpected Output of c program [duplicate]
void main()
{
clrscr();
if(sizeof(!6.0))
printf("%d",sizeof(6.0,2));
else
printf("i don't know");
getch();
}
The output of this function is 4....
1
vote
2
answers
81
views
Why it is not printing 2d array value when access directly [closed]
#include<stdio.h>
int main(){
int i,j;
int ans[5][5];
ans[0][0] = 10;
printf(" why this %d \n\n",ans[0,0] );
}
and output is some garbage value
i tried this in codeblocks
1
vote
3
answers
122
views
C language and operators [duplicate]
#include <stdio.h>
int main()
{
int i=2;
if(i==3,4)
{
printf("If block");
}
else
{
printf("Else block");
}
return 0;
}
Why is this code ...
2
votes
1
answer
177
views
I want to implement python list in cpp, but stuck at overloading subscript operator [ ] and comma , [closed]
I want to implement puthon list in cpp, bug do not fin a way to implement pyton's slice operator, e.g.:
list[3:7] // sublist including item 3 included to item 7 excluded
As colon is not an operator ...
0
votes
1
answer
49
views
How does comma operator behaves with statements?
#include <stdio.h>
void main()
{
int i,printf("Hello");
}
when i compiled this code , compiler raised an error:
error: expected declaration specifiers or '...' before string ...
0
votes
2
answers
75
views
Is there a special use of the parenthesis operator in C?
The following code works fine, but I don't find a basis on which I can initialize the array with (). Can someone explain it?
#include <stdio.h>
int main(void){
int a[3][2] = {(0, 1), (2, 3),...
-8
votes
3
answers
160
views
What does it mean to assign (2, 6, 8) to an integer in C? [closed]
What will be the output of the below piece of code? Why?
#include <stdio.h>
int main()
{
int x = (2,6,8);
printf("%d",x);
return 0;
}
1
vote
4
answers
346
views
C/C++: Is this a 2D array? Why parentheses in array is syntactically correct?
I am just starting to learn C++ and saw the following code sample.
The code declared an integer array with paratheses and can compile without a syntax error. Is this a 2D array declation?
int array[] =...
0
votes
1
answer
162
views
The expression " x = (a,b); " [duplicate]
In this comma-separated expression. Why doesn't the x gets value an instead of b?
int main() {
int a=20;
int b=100;
int x;
x=(a,b);
cout<<x;
}
Gives output: 100
4
votes
3
answers
154
views
No compiler warning when there's a comma instead of a semicolon in a for loop
Why doesn't gcc give any warnings about this code?
#include <stdio.h>
void printDigits(int *arr, int arrsize);
int main() {
int number[] = { 1, 7, 8, 3, 6, 5, 4, 2 };
size_t arraysize =...
1
vote
1
answer
459
views
How does this logic work: k+=(x=5,y=x+2,z=x+y) ;
How does this logic work: k+=(x=5,y=x+2,z=x+y);
How it will give result k == 22. When I initialize the value of k = 10
#include <stdio.h>
int main()
{
int x,y,z,k=10;
k+=(x=5,y=x+2,z=x+...
0
votes
2
answers
113
views
Is this comma-separated Python line creating a tuple? [duplicate]
I was looking at different discussions for this LeetCode problem, (basically, we need to remove all instances of a given value in an array, without creating or using another array) and I came across ...
1
vote
1
answer
327
views
Printf with conditional format [duplicate]
I want to print number of variables based on mode value. Here is example:
char format[64] = {}
int mode_1, mode_2, mode_3;
int a,b,c,d,e,f;
...
// Get mode value here ....
...
// Build the format ...
3
votes
1
answer
81
views
Grokking the comma operator [duplicate]
I am trying to grok the comma operator. The reference says:
In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded (...), and its side effects are completed before ...
1
vote
3
answers
116
views
Nuances of comma seperator and comma operator when used in initialization and declaration statements
I was playing around with the , operator after reading several of the answers on a Stack Overflow post (What does the comma operator , do?). Further, I was playing around with the , separator after ...
11
votes
2
answers
346
views
Why`()` changes `this` in function call
Talk is cheap; show me the code.
// equals to this.test = "inside window"
var test = "inside window";
function f () {
console.log(this.test)
};
var obj = {
test: "inside object",
fn: f
};
...
6
votes
2
answers
2k
views
Comma Operator in subscript operator?
I am quite confused with the comma operator. I have never seen such code with such syntax? but I am curious if it useful at any place? why is it deprecated in c++20?
#include <iostream>
int main(...
0
votes
1
answer
46
views
explanation for C program (uses comma operator) [duplicate]
#include<stdio.h>
int main(){
int i=0;
while(i<4,5){
printf("loop");
i++;
}
}
This code goes infinite loop. can anyone explain logic behind these?
1
vote
1
answer
246
views
C++ for with multiple control statements using comma operator
How does comma operator if it is used in a 'for loop' for writing multiple control statements?
i tried
#include <iostream>
using namespace std;
int main() {
for (int x = 0, y = 0; x &...