In C programming, the comma operator (,) is used to separate expressions in various contexts. It evaluates each of its operands (from left to right) and returns the value of the rightmost operand. The comma operator has the lowest precedence among C operators.
#include <stdio.h> int main() { int a = 1, b = 2, c = 3; printf("Value of a: %d\n", a); printf("Value of b: %d\n", b); printf("Value of c: %d\n", c); return 0; }
Value of a: 1 Value of b: 2 Value of c: 3
#include <stdio.h> int main() { int a = 5, b = 10, c = 15; int result; result = (a + b, b + c); // Evaluates a + b and then b + c, returns b + c printf("%d\n", result); // Output will be 25 (b + c) return 0; }
25
#include <stdio.h> int main() { int i, sum = 0; for (i = 0; i < 5; i++, sum += i) { printf("i: %d, sum: %d\n", i, sum); } return 0; }
i: 0, sum: 0 i: 1, sum: 1 i: 2, sum: 3 i: 3, sum: 6 i: 4, sum: 10
#include <stdio.h> int add(int x, int y) { return (x++, y++, x + y); } int main() { int result = add(5, 10); printf("Result: %d\n", result); // Output will be 16 (5 + 11) return 0; }
Result: 16
#include <stdio.h> #define MAX(a, b) ((a > b) ? a : (b)) int main() { int x = 5, y = 10; int max_value = MAX(x, y); printf("Maximum value: %d\n", max_value); // Output will be 10 return 0; }
Maximum value: 10