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

Discussion

Which of the following are correctly formed #define statements in C?

  • A.#define CUBE (X) (X*X*X);
  • B.#define CUBE(x) (X*X*X)
  • C.#define CUBE(X)(X*X*X)
  • D.#define CUBE(X) {X*X*X}

Answer: C

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

The others each have a fault:

  • #define CUBE (X) (X*X*X); - the space after the name makes it an object-like macro whose body is the whole rest of the line, and the trailing semicolon travels with every use.
  • #define CUBE(x) (X*X*X) - the parameter is x but the body uses X, so the argument is never substituted.
  • #define CUBE(X) {X*X*X} - braces make it a block, which cannot appear where a value is wanted.

Only the third is well formed - though ((X)*(X)*(X)) would be safer still.

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

Post your comment

Please log in or register to post a comment.