
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Comma in C and C++
In C or C++, the comma ‘,’ is used in different purposes. Here we will see how they can be used.
Comma as an Operator.
The comma operator is a binary operator, that evaluates its first operand, and then 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 the 60 will be assigned to x. For the next statement, the func1() will be executed first, then the second one will be executed.
Comma as a Separator.
During function call or definition, it acts a separator. This is not like comma operator. When comma is used as separator, then all of the items separated by comma will be used, but for operator, it only gets the last one.
Example
#include<stdio.h> int main() { int x = 5, y = 10; void function(x, y); }
Here x and y both will be used as function parameters. The following program will be used to display how the comma operator is used.
Example
#include<stdio.h> main() { int a = 50; int b = (a++, ++a); printf("%d", b); }
Output
52
- Related Articles
- Comma operator in C/C++
- A comma operator question in C/C++ ?
- What is Comma operator in C++?
- How does the Comma Operator work in C++
- Why do we use comma operator in C#?
- Parsing a comma-delimited std::string in C++
- C++ Program to take out integer from comma separated string
- How to create a comma separated string from a list of string in C#?
- C# program to convert several strings into a single comma-delimited string
- What is Comma Operator (,) in JavaScript?
- Split String with Comma (,) in Java
- What is Trailing Comma in PHP?
- Foreach in C++ and C#
- Loops in C and C++
- Remove comma from a string in PHP?
