Using brackets ({ }) to create scopes?

Hmmmm I'm programming here and a "stupid" idea grow in my head...

I can use brackets to create scopes on my program? Eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
...
int a,b,c;
a=0,b=10,c=20;
printf("%d",a+b);

{
    int a=5;
    printf("%d",a);
}

printf("%d",a+c);
...



It is recomended? Its safe (ANSI?) ? or just CraZY???
Thanks for All =D


Hapy New Year!!!!!!
Yes, the variables declared inside will have their own scope. It is absolutely safe. I highly recommend using it. The reason is because under the C/C++ parsing rules a statement and a block are interchangeable.
hmmm =D i will kill all low life variables with that ... i can?


1
2
3
4
5
6
7
8
...
{
    int a = myFuncionValue();
    if(a<0){
        ...
    }
}
...
You can, but be sure to get mixed up with which variables you want to keep and which you don't, only create a scope when you know you won't use a variable anymore afterwards (or if it can be set to a variable with a larger scope).
A so far unmentioned benefit is that C++ destructors are called at the end of a block, so you can use these to manage the lifetime of objects you create within a function.
Topic archived. No new replies allowed.