Comma Operator in C
Comma Operator :
The comma operators is essentially a binary operator that operates on the first operand that is available, discards the result that is obtained from it, evaluates the operands that are present after this, and then returns the result/value in accordance with those evaluations.
Examples of Comma as an operator :
Let,s see some example for better understanding :
Run
#include<stdio.h> int main() { int val1 = 20; int val2 = (10, 20, 30, 40, 50); printf("val1 : %d\n",val1); printf("val2 : %d", val2); return 0; }
Output :
val1 : 20 val2 : 50
What Happened Above?
In the program, comma is used as the operator in the val2 , and right most value is taken as the highest precedence.
Examples of Comma as an separator :
Let,s see some example for better understanding :
Run
#include<stdio.h> int *help(){ int val = 20; return &val; } int main() { int *ptr = help(); printf("%d", *ptr); return 0; }
Output :
Segmentation fault
What Happened Above?
In the program, comma is used as the separator in the printf function.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment