
- 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
A comma operator question in C/C++ ?
Comma Operator in C/C++ programming language has two contexts −
As a Separator −
As an operator − The comma operator { , } is a binary operator that discards the first expression (after evaluation) and then use the value of the second expression. This operator has the least precedence.
Consider the following codes and guess the output −
Example
#include <stdio.h> int main(void) { char ch = 'a', 'b', 'c'; printf("%c", ch); return 0; }
Output
It gives an error because the works as a separator.
prog.c: In function ‘main’: prog.c:5:20: error: expected identifier or ‘(’ before 'b' char ch = 'a', 'b', 'c'; ^~~
Example
#include <stdio.h> int main(void) { char ch; ch = 'a','b','c'; printf("%c", ch); return 0; }
Output
It gives a as output as it works because the ‘,’ works as operator but it precedence is below assignment operator hence the output is a.
a
Example
#include <stdio.h> int f1() { return 43; } int f2() { return 123 ; } int main(void) { int a; a = (f1() , f2()); printf("%d", a); return 0; }
Output
It gives 123 as output as the ‘, ’ works as operator and being in braces it works and evaluates the second expression and gives output.
123
- Related Articles
- Comma operator 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#?
- What is Comma Operator (,) in JavaScript?
- When is the comma operator useful in JavaScript?
- Comma separated argument applicable for IN operator in MySQL?
- What does double question mark (??) operator mean in PHP ?
- A matrix probability question in C?
- A Boolean Matrix Question in C++?
- Comma in C and C++
- Parsing a comma-delimited std::string in C++
- What is a Ternary operator/conditional operator in C#?
- An interesting time complexity question in C++
- deque::operator= and deque::operator[] in C++ STL

Advertisements