What is the purpose of anonymous { } blocks in C style languages?

It limits the scope of variables to the block inside the { }.


Brackets designate an area of scope - anything declared within the brackets is invisible outside of them.

Furthermore, in C++ an object allocated on the stack (e.g. without the use of 'new') will be destructed when it goes out of scope.

In some cases it can also be a way to highlight a particular piece of a function that the author feels is worthy of attention for people looking at the source. Whether this is a good use or not is debatable, but I have seen it done.

Tags:

C#

C++

C