gpt4 book ai didi

c - Stephen Prata 的 C Primer Plus 中有一句关于 "statement"我无法理解的句子

转载 作者:行者123 更新时间:2023-11-30 16:58:40 25 4
gpt4 key购买 nike

Stephen Prata 的 C Primer Plus 中有一句关于“陈述”的句子我无法理解。

第5章中有一节解释了语句和表达式之间的区别。作者对此声明解释如下:

Statements

Statements are the primary building blocks of a program. A program is a series of statements with some necessary punctuation. A statement is a complete instruction to the computer. In C, statements are indicated by a semicolon at the end. Therefore,

legs = 4 is just an expression (which could be part of a larger expression), but legs = 4;

is a statement.

然后作者给出了一个例子:

Although a statement (or, at least, a sensible statement) is a complete instruction, not all complete instructions are statements. Consider the following statement:

x = 6 + (y = 5);

In it, the subexpression y = 5 is a complete instruction, but it is only part of the statement. Because a complete instruction is not necessarily a statement, a semicolon is needed to identify instructions that truly are statements.

作者说“y=5”是一个完整的指令,但正如她上面提到的,这不是只是一个表达式,而不是一个完整的陈述吗?

最佳答案

每个 C 程序都由语句组成。程序逐条执行。每个语句通常依次包含一些表达式。并且表达式还可以包括子表达式。例如:

/* diff_statements.c */
#include <stdio.h>
#define SIZE 10 /* SIZE symbolic constant */

int main(void)
{
int index, sum; /* declaration statement */
int boys[SIZE]; /* array declaration */

printf("user enter no of boys in 10 classes...\n");
/* function statement */

for (index = 0; index < SIZE; index++)
scanf("%d", &boys[index]);

/* for statement with single statement */

/* IT'S A GOOD PRACTICE TO REVIEW IF U ENTERED CORRECT VALUES */

printf("No. of boys you entered for 10 classes are as:\n");
for (index = 0; index < SIZE; index++)
printf("%d\t", boys[index]);
printf("\n");

printf("Summing up & Displaying boys in all classes...\n");

/* for statement with block of statements */
for (index = 0; index < SIZE; index++) {
sum += boys[index];
; /* Null Statement; this does nothing here */
}

printf("Total boys in %d classes is %d\n", SIZE, sum);
return 0;
}

每个 C 语句都以分号“;”结尾。虽然表达式没有。例如:

x + y;    /* x + y is an exp but x + y; is a statement. */
5 + 7; /* similarly as above */

while (5 < 10) {
/* 5 < 10 is a relational exp, not a statement. */
/* used as a while test condition */

/* other statements in while loop */
}

result = (float) (x * y) / (u + v);
okey = (x * y) / (u + w) * (z = a + b + c + d);

/* (z = a + b + c + d) is a sub-expression in the above */
/* expression. */

表达式是人类逻辑中的完整指令,但根据C的观点,完整的指令是一条语句,即以分号结尾的表达式的有效组合。

关于c - Stephen Prata 的 C Primer Plus 中有一句关于 "statement"我无法理解的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38633511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com