intmain(){ intx=(50,60); inty=(func1(),func2());} Here 60 will be assigned to x. For the next statement, func1( will be executed first"/> intmain(){ intx=(50,60); inty=(func1(),func2());} Here 60 will be assigned to x. For the next statement, func1( will be executed first">

Home >Backend Development >C++ >In C and C++, comma is used to separate expressions or statements

In C and C++, comma is used to separate expressions or statements

王林
王林forward
2023-09-09 17:33:02979browse

In C and C++, comma is used to separate expressions or statements

In C or C, the comma "," has different uses. Here we will learn how to use them.

  • Comma as operator.

    • The comma operator is a binary operator that evaluates the first operand, discards the result, then evaluates the second operand and returns the value. The comma operator has the lowest precedence in C or C.

Example

#include<stdio.h>
int main() {
   int x = (50, 60);
   int y = (func1(), func2());
}

Here 60 will be assigned to x. For the next statement, func1() will be executed first, then the second one.

  • Comma as separator.

    • Comma as separator.

      • Comma as separator. strong>

        • It acts as a delimiter during a function call or definition. This is different from the comma operator. When using comma as separator all comma separated items will be used but with operator it only gets the last item.

      Example

      #include<stdio.h>
      int main() {
         int x = 5, y = 10;
         void function(x, y);
      }

      Here both x and y will be used as function parameters. The following program will be used to show how to use the comma operator.

      Example

      #include<stdio.h>
      main() {
         int a = 50;
         int b = (a++, ++a);
         printf("%d", b);
      }

      Output

      52

The above is the detailed content of In C and C++, comma is used to separate expressions or statements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete