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.

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)

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)

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.

Shankar meena said:   1 decade ago
My answer is for @Chaitali and @Vishnu.

[A]. #define CUBE (X) (X*X*X);
[B]. #define CUBE(x) (X*X*X)

For ans [A]. #define is a preprocessor directive. It is not a statement so we are not semicolon.

For ans [B]. #define is used for declare a symbolic constant. And in the constant we are used all letter is capital.

So, the answer A and B is incorrect and C is correct.

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

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 "

Neetu said:   1 decade ago
Option (A) is not right because there is a space btw CUBE and (X) , if we remove the space, it'll be correct.

And also it is not necessary to place semicolon at the end of macro, so option (C) is also correct.

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.

Saritha said:   1 decade ago
A and be will not work because option a has semicolon and preprocessor doesn't have semicolon.

In option b the x is a lowercase argument and used as uppercase.

S.Parthasarathy said:   1 decade ago
Option B is the correct one... In preprocessor we can use lower cases also to define a function.. It is not compulsory to use higher cases..)


Post your comments here:

Your comments will be displayed after verification.