Does (int i = 0; i < n; i++;) used as a block instead of {int i = 0; i < n; i++;} make any sense at all in C?

StackOverflow https://stackoverflow.com/questions/16304443

Question

Don't know if it's a really dumb thing to ask as I feel it goes against C syntax.But I am not sure.I stumbled across that in a question posted few minutes back.The OP uses something like (int i = 0; i < n; i++), ie without even a ; after i++.

Fibonacci Series in C - The series of numbers up to a given number

But though the OP's line is obviously wrong, I am tempted to ask something I just don't know- What does the following mean in C :

(int i = 0; i < n; i++;)   // Three `;` terminated statements enclosed in ()

as the following simply means a block of statements in C:

{int i = 0; i < n; i++;}

I mean, what does (int i = 0,n=3; i = n; i++;) mean in the following dummy program:

#include<stdio.h>

int main(void)
{
(int i = 0,n=3; i = n; i++;) 
}

Edit Even that single line sourced from that original question is ridden with errors.So let me ask this independently : What does it do if we enclose multiple ; terminated statements within a pair of ()? If we enclose within {} it becomes a block,but what about ()?

Was it helpful?

Solution

Nothing. The parentheses are used in certain situations such as boolean expressions and for loop comprehensions. You'll get a bunch of syntax errors.

OTHER TIPS

Common for loop construction:

for (int i = 0; i < 10; i++){
   //code here
}

The code

{int i = 0; i < 10; i++;}

doesn't really do much except set i to 0 and increment it to 1. I'm not even sure if saying i < 10 is valid outside a condition

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top