我继承了一些代码,它在调用时有一个这样的格式.要清楚一些函数定义在这样的地方:
void some_function(int a, int b, int c){
printf("hi there\n");
}
Run Code Online (Sandbox Code Playgroud)
然后它在主代码中被调用如下:
some_function(2, 3, 4);
{
x = 1200;
another_function(&x);
}
Run Code Online (Sandbox Code Playgroud)
以上只是简短的说法:
if(some_function(2,3,4) == 1)
{
//then execute code found here?
}
Run Code Online (Sandbox Code Playgroud)
我之前从未见过那个,我觉得因为some_function是void而且永远不会返回一个值,它永远不会运行括号中的代码?
甲复合语句是一个{,随后的零点或多个声明和声明,后跟一个}.复合语句被视为单个语句.
典型地,复合语句被用作通过控制语句if,while,for等:
if (condition)
{
blah;
blah;
}
Run Code Online (Sandbox Code Playgroud)
但它可以在任何可以使用单个语句的地方使用.(复合语句有时称为块 ;严格来说复合语句是一种块.)
它可以作为定义局部变量的一种方法:
blah;
{
int n = 42;
/* ... */
}
/* n is not visible here */
Run Code Online (Sandbox Code Playgroud)
在你的例子中:
some_function(2, 3, 4);
{
x = 1200;
another_function(&x);
}
Run Code Online (Sandbox Code Playgroud)
额外的{,}合法的,但没用; 它们可以省略而不改变程序的含义.(作者可能打算将这些陈述标记为逻辑分组.)
复合语句在逻辑上与函数调用无关; 它碰巧跟着它.
如果代码是这样的话,在这里使用复合语句会更有意义:
some_function(2, 3, 4);
{
int x = 1200;
another_function(&x);
}
Run Code Online (Sandbox Code Playgroud)
因为它将为变量提供局部范围x.