我希望知道在C编程语言中,什么是空语句?并且解释一下它的典型用法。
我找到了以下这段代码。
for (j=6; j>0; j++)
;
而且
for (j=6; j>0; j++)
来自msdn页面:
"null语句"是一个表达式语句,其中表达式缺失。当语言的语法需要语句但不需要表达式计算时,它非常有用。它由分号组成。
空语句通常用作迭代语句中的占位符或作为复合语句或函数末尾放置标签的语句。
了解更多:https://msdn.microsoft.com/en-us/library/1zea45ac.aspx
int a[50] = "lord of the rings";
int i;
for(i = 0; a[i] != 't'; i++)
;//null statement
//as no operation is required
空语句是一种不执行任何操作但出现在语法上的语句。
while ((*s++ = *t++))
; /* null statement */
if (condition1)
if (condition2)
dosomething();
else
; /* null statement */
else
dosomethingelse();
一个空语句(只包含分号)不执行任何操作。
标准还提供了它的一些常见用法:
EXAMPLE 2 In the program fragment
char *s; /* ... */ while (*s++ != '\0') ;
a null statement is used to supply an empty loop body to the iteration statement.
6 EXAMPLE 3 A null statement may also be used to carry a label just before the closing } of a compound statement.
while (loop1) { /* ... */ while (loop2) { /* ... */ if (want_out) goto end_loop1; /* ... */ } /* ... */ end_loop1: ; }
int i, j, k;
i = something;
for(j = 1; (2 * j) <= i; j *= 2);
计算小于i的最大2的整数次幂。对于大的i来说不是很高效,但如果您不能信任Log函数返回一个精确的整数值,则非常有用。
我的C++编译器不接受空语句作为函数体,但接受“空语句块”“{}”。
一个空语句并不会执行任何操作,因此只有一个 ';'
所以......在C/C++中,这个代码块将不会执行10次
for(int i = 0; i < 10; i++){//execute the following 10 times
;//nothing aka "null statement"
}//end for
while (1 == 1) ;
,它会一直循环不做任何事情。例如,在一个线程中使用它,你不想终止该线程,该线程可能会继续处理接收到的消息、处理中断或由其他进程调用其回调函数。 - Mawg says reinstate Monica