Is it something like "for (i = 1; i <= 10; printf ("% d \ n "; i), i ++) is valid and UB-free in C?

Are the next two code blocks exactly the same and achieve the same? It displays the same when I run the program, but I would appreciate some strict explanations.

for(i=1;i<=10;i++)
{
printf("%d\n",i);
}

and

for(i=1;i<=10;printf("%d\n",i),i++);

The loop forexpects valid C statements as arguments, right? But even if I checked in StackOverflow that expressions such as x+=4,y=x*2;are safe, since the comma acts like sequence points here, is the same true for the statement printf("%d\n",i),i++)passed as an argument in the loop forabove?

And if so, please take the trouble to answer the secondary question that arises from it:

  • comma

    :

    printf("Enter number\n"),scanf("%d",&number),printf("You entered %d",number);

+5
2

, . , . .

.

:

6.5.17 Comma operator

2:

void; E . ; .114)

+3

6.8.5.3 C:

1774

    for ( clause-1 ; expression-2 ; expression-3 ) statement

:

1775 > -2 , .

1776 -3 void .

1777 -1 , , , , ;

1778 .

1779 -1 , expression.134)

1780 -1 -3 .

1781 -2 .

, printf, i. printf i expression-3, , , .

+1

All Articles