Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of the following are correctly formed #define statements?

  • A.#define INCH PER FEET 12
  • B.#define SQR(X) (X *X);
  • C.#define SQR(X) X*X
  • D.#define SQR(X) (X*X)

Answer: D

Correct answer: #define SQR(X) (X*X)

Taking the others in turn:

  • #define INCH PER FEET 12 defines the macro INCH as the text PER FEET 12 - a macro name cannot contain spaces.
  • #define SQR(X) (X *X); ends in a semicolon, which travels with every expansion and breaks any use inside a larger expression.
  • #define SQR(X) X*X has no outer parentheses, so 1/SQR(2) becomes 1/2*2.

Only #define SQR(X) (X*X) is well formed - though even it should parenthesise the parameter, as ((X)*(X)), to survive an argument such as a+b.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.