There is a sentence about "statement" that I can't understand in C Primer Plus by Stephen Prata -
this question has answer here:
- expression versus statement 18 answers
there sentence "statement" can't understand in c primer plus stephen prata.
in chapter 5, there section explains difference between statement , expression. author explains statement follows:
statements
statements primary building blocks of program. program series of statements necessary punctuation. statement complete instruction computer. in c, statements indicated semicolon @ end. therefore,
legs = 4
is expression (which part of larger expression), butlegs = 4;
is statement.
and author gives example:
although statement (or, @ least, sensible statement) complete instruction, not complete instructions statements. consider following statement:
x = 6 + (y = 5);
in it, subexpression
y = 5
complete instruction, part of statement. because complete instruction not statement, semicolon needed identify instructions statements.
the authors says "y=5"is complete instruction mentioned above isn't expression, not complete statement?
every c program comprises of statements. program executes statement statement. each statement generally, in turn, comprises of expression. , expression may further comprise of sub-expressions. example:
/* 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 */ (index = 0; index < size; index++) scanf("%d", &boys[index]); /* statement single statement */ /* it's practice review if u entered correct values */ printf("no. of boys entered 10 classes as:\n"); (index = 0; index < size; index++) printf("%d\t", boys[index]); printf("\n"); printf("summing & displaying boys in classes...\n"); /* statement block of statements */ (index = 0; index < size; index++) { sum += boys[index]; ; /* null statement; nothing here */ } printf("total boys in %d classes %d\n", size, sum); return 0; }
every c statement ends semicolon ‘;’. while expressions don’t. example:
x + y; /* x + y exp x + y; statement. */ 5 + 7; /* above */ while (5 < 10) { /* 5 < 10 relational exp, not statement. */ /* used while test condition */ /* other statements in while loop */ } result = (float) (x * y) / (u + v); okey = (x * y) / (u + w) * (z = + b + c + d); /* (z = + b + c + d) sub-expression in above */ /* expression. */
expressions complete instruction in human logic, not according c. point of view of c, complete instruction statement, valid combination of expressions ended semi-colon.
Comments
Post a Comment