Spa*_*kzz -3 c declaration function function-prototypes
为什么我们int add(int,int)在第二行语句后使用分号.
#include<stdio.h>
int add(int,int);
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c=add(a,b);
printf("The sum of the 2 numbers is %d",c);
return 0;
}
int add(int x,int y)
{
int sum;
sum=x+y;
return sum;
}
Run Code Online (Sandbox Code Playgroud)
在C语法中,声明的定义方式如下
declaration:
declaration-specifiers init-declarator-listopt ;
^^^
Run Code Online (Sandbox Code Playgroud)
如您所见,分号是必需的.
还有这个
int add(int,int);
Run Code Online (Sandbox Code Playgroud)
是一个函数声明.因此,您必须在声明的末尾放置一个分号.
比较两个程序
int main( void )
{
int add( int x, int y )
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
和
int main( void )
{
int add( int x, int y );
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
第一个程序无效,因为编译器会认为函数add是在函数内定义的main.
第二个程序有效.其中有一个函数声明和一个代码块main.
因此需要使用分号来解除其他程序结构的声明.