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

后端 未结 17 1033
囚心锁ツ
囚心锁ツ 2020-11-28 09:34

What is the purpose of anonymous { } blocks in C style languages (C, C++, C#)

Example -



void function()
{

  {
    int i = 0;
    i = i + 1;
  }

          


        
17条回答
  •  清酒与你
    2020-11-28 09:54

    You are doing two things.

    1. You are forcing a scope restriction on the variables in that block.
    2. You are enabling sibling code blocks to use the same variable names.

提交回复
热议问题