Search code examples
ccomma-operator

Comma operator in condition of loop in C


#include <stdio.h>

main()
{
    int i;
    for(i=0; i<0, 5; i++)
        printf("%d\n", i);
}

I am unable to understand the i<0, 5 part in the condition of the for loop.

Even if I make it i>0, 5, there's no change in output.

How does this work?


Solution

  • Comma operator evaluates i<0 Or i>0 and ignores. Hence, it's always the 5 that's present in the condition.

    So it's equivalent to:

    for(i=0;5;i++)