标签: comma-operator

当 for 循环中有逗号而不是分号时,没有编译器警告

为什么 gcc 不给出有关此代码的任何警告?

#include <stdio.h>

void printDigits(int *arr, int arrsize);

int main() {
    int number[] = { 1, 7, 8, 3, 6, 5, 4, 2 };
    size_t arraysize = (sizeof(number) / sizeof(number[0]));

    printDigits(number, arraysize);

    return 0;
}

void printDigits(int *arr, int arrsize) {
    int i;  
    for (i=0; i<arrsize, i++;) {
        printf("%d ", arr[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

特别是关于 printDigits 函数中的 for 循环。它是: for(i=0; i<arrsize, i++;) 虽然它真的应该是for(i=0; i<arrsize; i++)

Gcc 没有给我任何警告,我花了一段时间才弄清楚为什么数组没有打印出来。

c for-loop compiler-warnings comma-operator

4
推荐指数
1
解决办法
82
查看次数

逗号循环中的逗号

为什么以下行会产生错误?

for(int i = 0, int pos = 0, int next_pos = 0; i < 3; i++, pos = next_pos + 1) {
  // …
}

error: expected unqualified-id before ‘int’
error: ‘pos’ was not declared in this scope
error: ‘next_pos’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

编译器是g ++.

c++ for-loop comma-operator

3
推荐指数
1
解决办法
4047
查看次数

cout是否需要以分号结束?

我正在阅读Bjarne Stroustrup的编程:使用C++的原理和实践

在第2章的练习部分中,它讨论了在编译hello_world程序时查看输入错误的各种方法

#include "std_lib_facilities.h"

int main()  //C++ programs start by executing the function main
{
    cout << "Hello, World!\n",  // output "Hello, World!"
    keep_window_open();         // wait for a character to be entered
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

特别是本节要求:

想想你在程序中输入的错误至少还有五个(例如忘记 keep_window_open(),在键入单词时保持Caps Lock键,或者输入逗号而不是分号)并尝试每个错误,看看当你尝试时会发生什么编译并运行这些版本.

对于该cout行,您可以看到有一个逗号而不是分号.
这编译并运行(对我来说).是否做出了假设(如在javascript问题中:为什么使用分号?)语句已被终止?

因为当我尝试keep_terminal_open();编译时通知我分号排除.

c++ cout comma-operator

3
推荐指数
1
解决办法
1017
查看次数

c ++中的逗号运算符和逗号分隔符

可能重复:
当所有逗号运算符都不作为逗号运算符时?

什么时候逗号(,)表现为运算符?它什么时候表现为分隔符?它的后果是什么.如果可能,请为两者提供小例子.

c++ comma-operator

3
推荐指数
1
解决办法
1989
查看次数

为什么双x = 0,1; 不编译?

double x = 0,1;
Run Code Online (Sandbox Code Playgroud)

不编译(尝试在MSVC9.0上).错误是

C2059 syntax error : 'constant'
Run Code Online (Sandbox Code Playgroud)

确实知道那里有一个逗号而不是一个点,但上面的行不应该被解释为以下几行吗?

double x = (0,1); //which is double x = 1;
Run Code Online (Sandbox Code Playgroud)

顺便提一下,初始化用括号成功编译.

我正在考虑operator ,优先级低的行operator =,但在这种情况下=不是运算符,所以这应该不是问题.什么句法规则决定了这一点

 double x = 0,1; 
Run Code Online (Sandbox Code Playgroud)

应该是非法的?

c++ syntax comma-operator

3
推荐指数
2
解决办法
623
查看次数

从逗号分隔的for循环内部的语句的从左到右的评估

这两个代码是否相同?for循环中的这些语句写在用逗号分隔的同一行中.他们会从左到右进行评估吗?

此外,我想问我可以使用逗号分隔的for循环内的语句.喜欢(i = 0,j = 0,k = 0; .......)?

for(i=0, j= strlen(s)-1; i<j; i++, j--){ 
     c=s[i];
     s[i]=s[j];
     s[j]=c;
}
Run Code Online (Sandbox Code Playgroud)

for(i=0, j= strlen(s)-1; i<j; i++, j--)
      c=s[i],s[i]=s[j],s[j]=c;
Run Code Online (Sandbox Code Playgroud)

c for-loop comma-operator

3
推荐指数
1
解决办法
115
查看次数

为什么在未评估的操作数内部的包扩展会导致最后一个元素?

我可以在里面做到这一点decltype():

auto g() -> decltype(1, "", true, new int);
Run Code Online (Sandbox Code Playgroud)

但不是这个:

template <class... Args>
auto g(Args&&... args) -> decltype(args...);
Run Code Online (Sandbox Code Playgroud)

它失败了,因为包内扩展出现在里面,decltype()但我认为包扩展会导致以逗号分隔的参数列表.因此返回类型g(a, b, c)将是decltype(c)因为逗号运算符的工作原理(它返回最后一个元素).当你在函数参数列表,模板参数列表,初始化列表等内部扩展时,它可以工作.但为什么这不是这种情况?

c++ templates comma-operator variadic-templates c++11

3
推荐指数
1
解决办法
125
查看次数

有人可以为我得到的输出清除这个概念吗?

这是在接受采访时被问到的.以下代码段的输出是什么?

#include <iostream>
using namespace std;

int main() {
    cout << (3,2,1)-(1,2,3) << endl; // in C++ too this prints -2
    printf("%d\n",(3,2,1)-(1,2,3)); // prints -2
    printf("%d\n",("%d",3,2,1)-(1,2,3)); // prints -2
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

通过输出我猜它(1-3)= -2.但是如何从(3,2,1)价值1中选择,同样从(1,2,3)价值3中选择?我猜对了吗?

c++ comma-operator

3
推荐指数
1
解决办法
129
查看次数

为什么我必须用括号表示逗号表达式的初始化表达式?

煮一个问题我已经了解它的本质,我可以通过首先在逗号表达式中执行do-nothing lambda来将变量初始化为int,如下所示:

int main(){
  auto x = ( []{}(), 10 );          // same effect as auto x = 10;
}
Run Code Online (Sandbox Code Playgroud)

但如果我没有将初始化表达式括起来,

int main(){
  auto y = []{}(), 10;              // won't compile
}
Run Code Online (Sandbox Code Playgroud)

所有gcc,clang和MSVC都抱怨尝试yvoid表达式初始化.

为什么我必须将逗号表达式括起来以将其用作初始值设定项?

c++ initialization comma-operator

3
推荐指数
1
解决办法
139
查看次数

三元和逗号运算符

int a,b;
a = 1 ? 1,2 : 3,4; // a = 2
b = 0 ? 1,2 : 3,4; // b = 3
Run Code Online (Sandbox Code Playgroud)

逗号运算符总是返回逗号的右侧,但是如果我们对变量赋值,则返回左侧,除非我们使用().那么第一个表达式如何赋予2.

我认为它是a = 1,2所以它应该是1但实际上a = 2.

为什么?

c c++ ternary-operator comma-operator

3
推荐指数
1
解决办法
476
查看次数