473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

what is the difference between const and #define ?

whats the Difference between const and #define in C?
we can Use both for the case below...

#define PI 3.1416;

const float pi2 = 3.1416;

for both the case actually what will happen when compiling and running?
why should I go for const here?

And is there any specific use for const here.. If yes give me Examples ?

Thanks & Regards
-Balaji
Mar 13 '11 #1

✓ answered by Ankit Khare

#define is a macro,a preprocessor directive,its simply a textual replacement,so wherever you have written PI in your code will be replaced to 3.1416 before the compiler comes into the picture.
Whereas in the other case you are a declaring a const float variable,by const it means your code cannot change its value anywhere in the code.

One other difference is where ever you have used #defined value,that will be treated as double,whereas in other case you are explicitly declaring it as float

2 8133
#define is a macro,a preprocessor directive,its simply a textual replacement,so wherever you have written PI in your code will be replaced to 3.1416 before the compiler comes into the picture.
Whereas in the other case you are a declaring a const float variable,by const it means your code cannot change its value anywhere in the code.

One other difference is where ever you have used #defined value,that will be treated as double,whereas in other case you are explicitly declaring it as float
Mar 14 '11 #2
Adding to Ankit's reply,

In the extended source code you can see replaced macro value.
Mar 15 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: tggfang | last post by:
Please, anybody can tell me what is the real meaning of this const String& str thanks
2
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
10
by: sandSpiderX | last post by:
Hi, What is meant by where X is struct X { int i; } const X cx={1};
14
by: alexl | last post by:
eg int myfunction () const; I understand myfunction(const thevariable), eg myfunction won't change the value of "thevariable." But what is the significance of placing const after the () in ...
13
by: Vijay Kumar R. Zanvar | last post by:
Hello, I have few questions. They are: 1. Is "const char * const *p;" a valid construct? 2. How do I align a given structure, say, at 32-byte boundary? 3. Then, how do I assert that a given...
6
by: G Patel | last post by:
I've been looking at some code for string functions (certain implementation of them) and the non modified string is usually declared as a const char *s in the parameter list. I was wondering,...
2
by: Jim Langston | last post by:
I'm a little confused. I have a class function declared like: const CItem& operator << (const std::string &sIn); Which I use like this: CItem Item; Item <<...
2
by: nicknamenickname | last post by:
hello, here is an example: in class vector explicit vector(size_type n, const T& val = T(), const A&a = A()):alloc(a) {...} vector I mentioned above is a constructor of a template class:
6
Atran
by: Atran | last post by:
Hello, What is const in C#? And do you can give me example. Thanks.
5
by: bhushanbagul | last post by:
Hi All Please let me know exactly what difference i and g makes in oracle version i.e. when we say oracle 10g or oracle 11i Thanks Bhushan
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.