fork download
  1. #include <iostream>
  2. int main
  3. {
  4. // 基本的逗号表达式使用
  5. int a = 1, b = 2, c;
  6. c = (a = 5, b = 10); // 先计算a = 5,再计算b = 10,整个逗号表达式的值是b = 10的值,即10
  7. std::cout << "a的值: " << a << ", b的值: " << b << ", c的值: " << c << std::endl;
  8.  
  9. // 在for循环中的逗号表达式
  10. for (int i = 0, j = 5; i < 3 && j > 2; i++, j--) {
  11. std::cout << "i的值: " << i << ", j的值: " << j << std::endl;
  12. }
  13.  
  14. // 函数参数中的逗号表达式
  15. int result = std::max((2 + 3, 5), (4 * 2, 8)); // 先计算每个逗号表达式,再传递给std::max函数
  16. std::cout << "取最大值的结果: " << result << std::endl;
  17.  
  18. return 0;
  19. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:5: error: cannot declare ‘::main’ to be a global variable
 int main
     ^~~~
prog.cpp:5:5: error: expected primary-expression before ‘int’
     int a = 1, b = 2, c;
     ^~~
prog.cpp:5:5: error: expected ‘}’ before ‘int’
prog.cpp:3:1: note: to match this ‘{’
 {
 ^
prog.cpp:6:5: error: ‘c’ does not name a type
     c = (a = 5, b = 10);  // 先计算a = 5,再计算b = 10,整个逗号表达式的值是b = 10的值,即10
     ^
prog.cpp:7:10: error: ‘cout’ in namespace ‘std’ does not name a type
     std::cout << "a的值: " << a << ", b的值: " << b << ", c的值: " << c << std::endl;
          ^~~~
In file included from prog.cpp:1:
/usr/include/c++/8/iostream:61:18: note: ‘std::cout’ declared here
   extern ostream cout;  /// Linked to standard output
                  ^~~~
prog.cpp:10:5: error: expected unqualified-id before ‘for’
     for (int i = 0, j = 5; i < 3 && j > 2; i++, j--) {
     ^~~
prog.cpp:10:28: error: ‘i’ does not name a type
     for (int i = 0, j = 5; i < 3 && j > 2; i++, j--) {
                            ^
prog.cpp:10:44: error: ‘i’ does not name a type
     for (int i = 0, j = 5; i < 3 && j > 2; i++, j--) {
                                            ^
prog.cpp:16:10: error: ‘cout’ in namespace ‘std’ does not name a type
     std::cout << "取最大值的结果: " << result << std::endl;
          ^~~~
In file included from prog.cpp:1:
/usr/include/c++/8/iostream:61:18: note: ‘std::cout’ declared here
   extern ostream cout;  /// Linked to standard output
                  ^~~~
prog.cpp:18:5: error: expected unqualified-id before ‘return’
     return 0;
     ^~~~~~
prog.cpp:19:1: error: expected declaration before ‘}’ token
 }
 ^
stdout
Standard output is empty