When can I omit curly braces in C?

后端 未结 9 1935
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 09:14

I\'m almost certain this has been asked before, but I can\'t find it being answered anywhere.

When can I omit curly braces in C? I\'ve seen brace-less return

9条回答
  •  春和景丽
    2020-11-29 10:10

    An example:

    int a[2][2] = {{1, 2}, {3, 4}};
    

    you can use the valid equivalent form:

    int a[2][2] = {1, 2, 3, 4};
    

    Some verbose compilers may warn, but it is valid.

    Note that for the if statement (same for the while, the for statement, ...) the {} are not omitted. They are just not requested. The if statement has this syntax:

    if (expression) statement

    If you want a multiple statement instead of a single statement you can use a compound statement which is surrounded {}.

提交回复
热议问题