Is if(TRUE) a good idea in C?

后端 未结 17 3459
忘了有多久
忘了有多久 2021-02-19 01:19

In the C programming language, it is my understanding that variables can only be defined at the beginning of a code block, and the variable will have the scope of the block it w

17条回答
  •  抹茶落季
    2021-02-19 01:42

    As so many answers already said, you don't need the "if" stuff. Just create the bare block. But i want to get at another point. In C, you can create variable declarations anywhere in a block, not only at the start. In C89, you had that restriction. Starting with C99 (that's 10 years now), you don't have that restriction anymore, although some compilers will moan anyway. GCC won't, though, if you tell it to use the most "recent" C Standard with the -std=c99 option.

    Because there are still compilers in existence that moan by default, i would not prefer mixing declarations and code though. I would keep putting declarations at the start of blocks for compatibility reasons.

提交回复
热议问题