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.

Comma Operator in c

About Comma Operator :

In C language, comma sign is used primarily in two ways :

  • As an operator.
  • As a separator.

The behaviour of comma sign is depend on how we used that in our program.

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

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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription