什么是 C 中的空语句?

什么是 C 中的空语句?

问题描述:

我想确切地知道,什么是 C 编程语言中的空语句?并解释它的典型用途.

I want to know exactly, what is a null statement in C programming language? And explain a typical use of it.

我找到了以下代码段.

for (j=6; j>0; j++)
;

for (j=6; j>0; j++)

来自 msdn 页面:

空语句"是缺少表达式的表达式语句.当语言的语法需要语句但不需要表达式求值时,它很有用.它由一个分号组成.

The "null statement" is an expression statement with the expression missing. It is useful when the syntax of the language calls for a statement but no expression evaluation. It consists of a semicolon.

空语句通常用作迭代语句中的占位符或用作在复合语句或函数末尾放置标签的语句.

Null statements are commonly used as placeholders in iteration statements or as statements on which to place labels at the end of compound statements or functions.

了解更多:https://msdn.microsoft.com/en-us/library/1zea45ac.aspx

并解释它的典型用途.

当你想找到字符串中某个字符第一次出现的索引时

When you want to find the index of first occurrence of a certain character in a string

int a[50] = "lord of the rings";
int i;

for(i = 0; a[i] != 't'; i++)
    ;//null statement
//as no operation is required