|
|
|
|
|
by quietbritishjim
2136 days ago
|
|
> Curly braces designate scope in C It's not the curly braces that create the scope as such. It's the block, and the curly braces just happen to create the block. In C++ you can even do this, for example (I was surprised to find it doesn't work in C): int i = 1;
if (true) int i = 2;
std::cout << i << '\n';
// Prints 1
In any case, the semanics of blocks (whether they create scopes) is totally orthogonal to the syntax of blocks. You can just as easily imagine the reverse: the current colon-indent syntax where blocks do create scopes.Another issue, which other replies have alluded to but not quite spelt out. You'd need to add a syntax to distinguish between regular assignment and declaration. In C you can still choose to assign to the outer-scope variable from a nested scope of you wish, so you don't want i=1 in a nested scope to always declare a new variable that shadows a variable in the outer scope. Oh and one more thing. Your snippet (or equivalent) won't work in all C-style braces languages: in C# it is a compilation error to shadow a variable from an outer scope in the same function (presumably because it's a common source of bugs and extremely easy to just choose a different name for one of the variables). |
|