The reason why this code gives you forward referencing error is because here you are trying to print value of x. Which is violating the rule of declaration before read. For forward referencing to work you can using the variable as the destination but not as a source. What I mean is:
int y=0; y = x = 5; //This expression evaluates as (y = (x = 5)) int x; Above code will work fine because here x is used on left hand side as source for inner expression. Where as same code will give error if you do
int y=0; y=x; // x is used as source int x; In your code you were using x as source so you were getting the error. I hope this clears the point.