C# Programming - Datatypes - Discussion

Discussion Forum : Datatypes - General Questions (Q.No. 12)
12.
Which of the following is the correct ways to set a value 3.14 in a variable pi such that it cannot be modified?
float pi = 3.14F;
#define pi 3.14F;
const float pi = 3.14F;
const float pi; pi = 3.14F;
pi = 3.14F;
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
5 comments Page 1 of 1.

Dheeraj said:   1 decade ago
C and D both are answer. Because const float pi = 3.14F; it is correct. And const float pi; pi = 3.14F;

suppose I don't take value of pi it is garbage collected and I feed value of pi that is occurred.
(1)

Sanjay bisht said:   1 decade ago
# define pi 3.14 defines "pi" as replaceable with 3.14 meaning 3.14 can be replaced with the word "pi". The question wants to allocate "pi"(variable) the value 3.14. hence #define is substitution.

Abhijit said:   1 decade ago
Const keyword is used for declaring a Constant variable,where Constant variable must be declare at the time of declaration only.

Syntax for declaring Constant is :
[const]<type><var><=value>

VISHWANATH said:   1 decade ago
The same is observed with #define pi 3. 14.
Can any one please tell why can't we use this ?

Gopesh said:   1 decade ago
Since the question is we have to set a value 3.14 in a variable pi such that it cannot be modified, so we have to use 'const' keyword. But we can't write (const float pi; pi = 3.14F;) because here we are modifying the value of pi.

Post your comments here:

Your comments will be displayed after verification.