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

Aniruddh said:   1 decade ago
Not only 3 but all 4 macro definitions are working properly with dev cpp compiler.

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.

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.

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.

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.

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.

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.

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)

Vemula Durgarao said:   8 years ago
Why not A?

Why its answer is c?
(1)

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


Post your comments here:

Your comments will be displayed after verification.