C Programming - C Preprocessor - Discussion

2. 

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: Option C

Explanation:

No answer description available for this question.

Chaitali said: (Jul 4, 2011)  
Why not a or b?
Please explain...

Vishnu said: (Jul 8, 2011)  
Both a and b will work.

Saritha said: (Jul 15, 2011)  
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.

Aniruddha said: (Aug 14, 2011)  
Hey nice observation @saritha.

Arjun said: (Aug 23, 2011)  
Is it not necessary to have a space in C?

Kamal said: (Oct 1, 2011)  
How could option C be correct? There is no space between CUBE(X) and (X*X*X). Please someone explain it.

Singh said: (Oct 17, 2011)  
Option A will not be complile as between CUBE and (X) their is space
and their is no provision for double define value without \.

Khalid said: (Nov 2, 2011)  
Semicolon and spaces between (x)and (x*x*x) makes option A wrong

:-) said: (Nov 5, 2011)  
It is working Properly...

#define CUBE(X) (X*X*X)
#define CUBE(X) (X*X*X);
#define CUBE(X)(X*X*X)

S.Parthasarathy said: (Nov 29, 2011)  
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..)

Nrv said: (Dec 7, 2011)  
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: (Jan 12, 2012)  
'c' is correct one because of it has neither semicolon nor lower case.

Annanya said: (Jan 27, 2012)  
Space is must in preprocessor declaration then how came the answer c? can anybody explains?

Hriday Kumar Gupta said: (Feb 16, 2012)  
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.

Aarti said: (Mar 4, 2012)  
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: (Aug 19, 2012)  
The mai error in option A is because space between
CUBE and (X)...check out CUBE (X)

Ketan said: (Aug 19, 2012)  
After the #define their is spac important for defining process and than no semicolon after that.

Sai Kumar said: (Aug 20, 2012)  
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: (Oct 30, 2012)  
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: (Nov 19, 2012)  
In GCC compiler, semicolon and space doesn't matter. But what happens with Turbo C compiler? can anyone explain me?

Aniruddh said: (Aug 9, 2013)  
Not only 3 but all 4 macro definitions are working properly with dev cpp compiler.

Shankar Meena said: (Sep 2, 2013)  
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.

Neetu said: (Feb 14, 2014)  
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.

Anand Kushwaha said: (Jul 27, 2015)  
A and B are not work because a option is semicolon and option B gives the small latter x.

Eededdasd said: (Jul 22, 2016)  
Depends on the usage in the main method. All are correct except for B which makes no sense as the lower case is used.

Narasimha .Akkisetty said: (Aug 18, 2016)  
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.

Anshul said: (Sep 7, 2016)  
How could option C be correct? There is no space between CUBE(X) and (X*X*X). Please someone explain it.

Neeraj said: (Sep 1, 2017)  
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)

Vemula Durgarao said: (Sep 30, 2017)  
Why not A?

Why its answer is c?

Uday M said: (Feb 4, 2018)  
IN GCC compiler, both B, C answer is correct.

Hellyeah said: (Mar 22, 2020)  
Yes, @Neeraj is right.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.