📜  C和C++中的逗号

📅  最后修改于: 2021-05-30 17:05:03             🧑  作者: Mango

在C和C++中,可以在两种情况下使用逗号(,):
1)以逗号作为运算符:
逗号运算符(由令牌表示)是一个二进制运算符,它评估其第一个操作数并丢弃结果,然后评估第二个操作数并返回该值(和类型)。逗号运算符具有所有C运算符最低的优先级,并充当序列点。

C
/* comma as an operator */
int i = (5, 10); /* 10 is assigned to i*/
int j = (f1(), f2()); /* f1() is called (evaluated) first followed by f2().
                      The returned value of f2() is assigned to j */


C
/* comma as a separator */
int a = 1, b = 2;
void fun(x, y);


C
/* Comma acts as a separator here and doesn't enforce any sequence.
    Therefore, either f1() or f2() can be called first */
void fun(f1(), f2());


C
// PROGRAM 1
#include 
int main()
{
    int x = 10;
    int y = 15;
 
    printf("%d", (x, y));
    getchar();
    return 0;
}


C
// PROGRAM 2:  Thanks to Shekhu for suggesting this program
#include 
int main()
{
    int x = 10;
    int y = (x++, ++x);
    printf("%d", y);
    getchar();
    return 0;
}


C
// PROGRAM 3:  Thanks to Venki for suggesting this program
#include 
int main()
{
    int x = 10, y;
 
    // The following is equivalent
    // to y = x + 2 and x += 3,
    // with two printings
    y = (x++,
         printf("x = %d\n", x),
         ++x,
         printf("x = %d\n", x),
         x++);
 
    // Note that last expression is evaluated
    // but side effect is not updated to y
    printf("y = %d\n", y);
    printf("x = %d\n", x);
 
    return 0;
}


C++
#include 
 
using namespace std;
 
int main()
 
{
 
    int a = 5;
 
    a = 2, 3, 4;
 
    cout << a;
 
    return 0;
}


CPP
#include 
using namespace std;
int main()
{
    cout << "First Line\n",
        cout << "Second Line\n",
        cout << "Third Line\n",
        cout << "Last line";
    return 0;
}


2)以逗号分隔:
与函数调用和定义,宏等函数,变量声明,枚举声明和类似构造一起使用时,逗号充当分隔符。

C

/* comma as a separator */
int a = 1, b = 2;
void fun(x, y);

将逗号用作分隔符不应与用作运算符相混淆。例如,在下面的语句中,可以按任意顺序调用f1()和f2()。

C

/* Comma acts as a separator here and doesn't enforce any sequence.
    Therefore, either f1() or f2() can be called first */
void fun(f1(), f2());

有关使用逗号运算符的C与C++的区别,请参见此内容。
您可以尝试以下程序来检查您对C语言中的逗号的了解。

C

// PROGRAM 1
#include 
int main()
{
    int x = 10;
    int y = 15;
 
    printf("%d", (x, y));
    getchar();
    return 0;
}

C

// PROGRAM 2:  Thanks to Shekhu for suggesting this program
#include 
int main()
{
    int x = 10;
    int y = (x++, ++x);
    printf("%d", y);
    getchar();
    return 0;
}

C

// PROGRAM 3:  Thanks to Venki for suggesting this program
#include 
int main()
{
    int x = 10, y;
 
    // The following is equivalent
    // to y = x + 2 and x += 3,
    // with two printings
    y = (x++,
         printf("x = %d\n", x),
         ++x,
         printf("x = %d\n", x),
         x++);
 
    // Note that last expression is evaluated
    // but side effect is not updated to y
    printf("y = %d\n", y);
    printf("x = %d\n", x);
 
    return 0;
}

C++

#include 
 
using namespace std;
 
int main()
 
{
 
    int a = 5;
 
    a = 2, 3, 4;
 
    cout << a;
 
    return 0;
}

3)逗号运算符代替分号。
我们知道在C和C++中,每个语句都以分号终止,但是在满足以下规则后,逗号运算符也用于终止该语句。

  • 变量声明语句必须以分号终止。
  • 声明语句之后的语句可以由逗号运算符终止。
  • 程序的最后一条语句必须以分号终止。

例子:

CPP

#include 
using namespace std;
int main()
{
    cout << "First Line\n",
        cout << "Second Line\n",
        cout << "Third Line\n",
        cout << "Last line";
    return 0;
}

输出:

First Line
Second Line
Third Line
Last line
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。