Home > Backend Development > C++ > body text

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

王林
Release: 2023-09-09 17:33:02
forward
941 people have browsed it

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());
}
Copy after login

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);
      }
      Copy after login

      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);
      }
      Copy after login

      Output

      52
      Copy after login

      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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template