How many styles of writing functions are there in C?

后端 未结 4 2034
-上瘾入骨i
-上瘾入骨i 2021-01-13 00:03

So far, I know two styles:

/* 1st style */
int foo(int a) {
return a;
}

/* 2nd style */
int foo(a)
int a;
{
return a;
}

(I saw someone wri

4条回答
  •  滥情空心
    2021-01-13 00:30

    There are (at least) two disadvantages when using the 2nd style:

    1. If the function prototype is also missing, the compiler will not check for proper argument type. And, if I remember correctly, the number of arguments will also not be checked.
    2. This (K&R, 1st Ed) style is valid only for backward compatibility ... someday, the C standard will (perhaps) disallow this style and your programs will halt.

    Also, for better readability, you can put function's return type on its own line (especially useful with long-winded return types like unsigned long int and headers):

    int 
    foo(int a) 
    {
        return a;
    }
    

提交回复
热议问题