char c = number;
short s = number;
long l = number;
long long ll = number;
float f = number;
double d = number;
unsigned int ui = number;
and many others
The compiler won't complain. However, if you declared
const int number = 123;
The compiler would moan about
char c = number;
short s= number;
unsigned int ui = number;
Having said that, if, during a debug session, you wish to change 123 to 321, a change in value of number would hold througout the debug session but you couldn't do this with a #define. However, using #define like this does not demostrate the full power of macros in C/C++.
Depending on what you are doing, a const normally specifies ROM storage. A #define always specifies compile time processing. You can do lots of tricks with #define.
eg
In headerd.h
DECL(red),
DECL(blue),
DECL(green)
In header.h
#define DECL(x) x
enum Colour
{
#include "headerd.h"
};
#undef DECL
This will declare enums in the header file and their corresponding strings in the source file without having to worry about the order. Maintenance is as simple as just adding another declaration to headerd.h.
Or in yet other words:
#define Value_X 123
will substitute the text 'Value_X' with '123' througout the text and THEN compile it.
const int Value_X = 123; allocates a memory location and saves the value 123 in it. Totte
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.