-2

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 ()?

Community
  • 1
  • 1
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49

2 Answers2

5

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

Dolphiniac
  • 1,632
  • 1
  • 9
  • 9
0

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

shieldgenerator7
  • 1,507
  • 17
  • 22
  • `i < 10` is valid outside a condition, but it returns a value, and it must be assigned to somewhere, else it will give you an error. ie `bool i_less_than_ten = i < 10;` – Goodwine Apr 30 '13 at 16:24
  • @Goodwine I just tried that.Gives no error of that kind.Maybe compiler dependant – Rüppell's Vulture Apr 30 '13 at 16:36
  • @Goowind Even this gives the output `1,0` that I expected `printf("%d,%d",ij)` if `i` is greater than `j`.How would you explain this Goodwine? – Rüppell's Vulture Apr 30 '13 at 16:38
  • @SheerFish Whoa, hold it, I said that if you don't assign it somewhere it will give you an error, in my example I'm assigning it to `i_less_than_ten`, and in your example you are using it as arguments, which falls in "assigned to somewhere" – Goodwine Apr 30 '13 at 19:50
  • It is completely allowed by the standard and not an error at all to have an expression just sit on its own. Any decent compiler will both remove it, and also warn you that you probably made a mistake, but it's perfectly legal code and should not generate an *error*, in the strict sense of the word, in **any** conforming compiler. – Alex Celeste May 31 '13 at 03:28