@@@ is this valid syntax @@@

In order to create temporary levels of scope my C++ book told me that it is valid to do this:

1
2
3
4
5
6
7
{
  short x = 5;
}

{
  short x = 2;
}


though, I get an error:

1
2
unqualified-id before '{' token
expected `,' or `;' before '{' token


Im using Dev-C++
Last edited on
Sorry... It seems this can't be done in global scope:

1
2
3
4
5
6
7
8
9
10
11
// wrong

{
  short x = 5;
}

{
  short x = 2;
}

int main() { ... }


1
2
3
4
5
6
7
8
9
10
11
// correct

int main() {
  {
    short x = 5;
  }

  {
    short x = 2;
  }
}
If you think about it, it wouldn't make sense in global scope.
You may think of using namespaces for those things
hmm, I dont even get what you're doing... maybe if i saw the whole context

what do your functions even do... just set dif values to same variable.

When you make a variable in global, global variables are always set to zero. And wherever you change them, (does'nt matter which function) it affects the same variable; when made local you can have two variables the same name but diffrent values. They have to be in different functions though.

Idk if i helped you.
Last edited on
lol... umm what you're doing isn't even relevant...

making 1statement block statements in main... when you make them in global it needs to be a function... why would you use them? how would you call them? they need to have types and they def need to have names...
@brokenbot. Why are you talking about functions. The OP was asking about block scope. What he was doing was attempting to define scope for a global variable, which defeats the purpose of a global variable anyway as helios pointed out.
Topic archived. No new replies allowed.