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 1 of 4.

Vemula Durgarao said:   8 years ago
Why not A?

Why its answer is c?
(1)

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)

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.

Hellyeah said:   5 years ago
Yes, @Neeraj is right.

Uday M said:   8 years ago
IN GCC compiler, both B, C answer is correct.

NEERAJ said:   8 years ago
BOTH A and C options are correct.

WE CAN USE SEMICOLON IN #DEFINE try it by urself like,

#define mul(x) (x*x*x)
main()
{
int a;
a=mul(5)
}
IN THIS CASE IT WILL RUN WITH NO ERROR AND WITH CORRECT OUTPUT

BUT
#define mul(x) (x*x*x)
main()
{
printf("%d", mul(5));
}
Will give an error as in printf semicolon cn not be used with a variable.

And space or no space it doesn't matter.

#define mul(x) (x*x)
Is same as
#define mul(x)(x*x)

Anshul said:   9 years ago
How could option C be correct? There is no space between CUBE(X) and (X*X*X). Please someone explain it.

Narasimha .akkisetty said:   9 years ago
As per my knowledge;

a) it contain a semicolon.
b) in function it contains at a time lower and upper case letters.
c) it does not contain at least one space.
d) it contains curly braces.

Eededdasd said:   9 years ago
Depends on the usage in the main method. All are correct except for B which makes no sense as the lower case is used.

Anand kushwaha said:   1 decade ago
A and B are not work because a option is semicolon and option B gives the small latter x.


Post your comments here:

Your comments will be displayed after verification.