0

There is a sentence about "statement" that I can't understand in C Primer Plus by Stephen Prata.

In Chapter 5, there is a section which explains difference between statement and expression. The author explains statement as follows:

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.

And then the author gives an example:

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.

The authors says that "y=5"is a complete instruction but as she mentioned above isn't this just an expression, not a complete statement?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Jin
  • 1,902
  • 3
  • 15
  • 26
  • 1
    "complete instruction", not "complete statement". At what point did you get the impression that "complete instruction" and "expression" were mutually exclusive? – Cubic Jul 28 '16 at 10:19
  • http://stackoverflow.com/questions/3846727/why-is-the-difference-between-an-expression-and-a-statement – Mohit Jain Jul 28 '16 at 10:20
  • `y=5` is an assignment. Assignments in C are also expressions and they evaluate to the right hand expression. So `y=5` is both a complete instruction (assign 5 to `y`) and an expression (the value is `5`). – Klas Lindbäck Jul 28 '16 at 10:27

1 Answers1

0

Every C program comprises of Statements. Program executes statement by statement. Each statement generally, in turn, comprises of some expression. And an expression may further comprise of sub-expressions. For 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 */

    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;
}

Every C statement ends with a semicolon ‘;’. While expressions don’t. For example:

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. */

Expressions are complete instruction in human logic, but not according to C. From point of view of C, a complete instruction is a Statement, that is a valid combination of expressions ended with a semi-colon.

K.H.A.J.A.S
  • 514
  • 4
  • 19