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

后端 未结 17 1053
囚心锁ツ
囚心锁ツ 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:38

    As far as I understand, they are simply for scoping. They allow you to reuse variable names in the parent/sibling scopes, which can be useful from time to time.

    EDIT: This question has in fact been answered on another Stack Overflow question. Hope that helps.

提交回复
热议问题