Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different Output

 #include<stdio.h>

int main(void)
{
   static int i=i++, j=j++, k=k++;
   printf("i = %d j = %d k = %d", i, j, k);
   return 0;
}

Output in Turbo C 4.5 :

i = 0 j = 0 k = 0

In gcc I'm getting the error:

Initializer element is not constant

Which one is logically correct ? I'm in bit confusion..

like image 794
Parikshita Avatar asked Dec 08 '22 01:12

Parikshita


1 Answers

Standard says about initialization (6.7.8):

4   All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

(That's from C99, but C89 says almost exactly the same thing.)

So it looks as though GCC is more correct than 15-year old abandonware. (Who'd a thunk it?)

like image 170
Nietzche-jou Avatar answered Jan 04 '23 05:01

Nietzche-jou