C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Point Out Correct Statements (Q.No. 2)
2.
Which of the following are correctly formed #define statements in C?
#define CUBE (X) (X*X*X);
#define CUBE(x) (X*X*X)
#define CUBE(X)(X*X*X)
#define CUBE(X) {X*X*X}
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
31 comments Page 2 of 4.

NRV said:   1 decade ago
Option 'A' wont work because of the semicolon...
Option 'B' wont work because 'X' is of lowercase and also the spaces cant be allowed btwn them..
Option 'D' wont work because curly braces cannot be used.

And hence d Option "C "

Rajan said:   1 decade ago
'c' is correct one because of it has neither semicolon nor lower case.

Annanya said:   1 decade ago
Space is must in preprocessor declaration then how came the answer c? can anybody explains?

Hriday Kumar Gupta said:   1 decade ago
Before Explanation of all the options, need to know that, in Macro definition(#define CUBE(X) (X*X*X)), there are Macro Expansion and Macro Templates.

In given Question, #define CUBE(X) (X*X*X) is Macro definition, CUBE(X) is Macro-Templates and (X*X*X)is Macro-Expansion.

A. Macro-Expansion must not followed by semicolon(;)
B. In Macro-Expansion and Macro-Templates using different variables(i.e x and X)
C. Correct Answer according to Macro definition.
D. Wrong, because only parenthesis i.e () is allowed in Macro-Expansion.

Thank You.
(1)

Aarti said:   1 decade ago
a)option will not work as there is space between cube(x) (x*x*x).
if we write cube(x)(x*x*x),then it will compile.there is no effect of semicolon.
b)will not work because macro is defined cube(x)(X*X*X)
this will give error "undefined symbol X".
c)option will work

Nimesh said:   1 decade ago
The mai error in option A is because space between
CUBE and (X)...check out CUBE (X)

Ketan said:   1 decade ago
After the #define their is spac important for defining process and than no semicolon after that.

Sai kumar said:   1 decade ago
Here OPTION A is correct in some cases but other options are absolutely wrong.....given below is example for the answer.
According to statements written in main.
for example
1. #define CUBE(X) (X*X*X);
this will be correct when we have statement like
main
{
int a;
a=CUBE(a) //this will be replaced by (a*a*a); along with semicolon // which is syntactically correct
}

BUT C IS NOT CORRECT AS SPACE IS NOT GIVEN.

Suhas said:   1 decade ago
Semi colon does not matter in macros.
It depends on compiler you are using like Turbo C (windows) and GCC in linux.

Rajesh.T.K. said:   1 decade ago
In GCC compiler, semicolon and space doesn't matter. But what happens with Turbo C compiler? can anyone explain me?


Post your comments here:

Your comments will be displayed after verification.