Instead of define i was hoping I could use const to declare global constants. When I remove my #defines and replace them with const int however, I get compiler errors.
Can someone explain why?
Instead of define i was hoping I could use const to declare global constants. When I remove my #defines and replace them with const int however, I get compiler errors.
Can someone explain why?
Well perhaps if you posted actual error messages....
For one thing, this works
#define SIZE 10
int array[SIZE];
Whereas this doesn't (but it does in C++)
const int size = 10;
int array[size];
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Well the actual error messages aren't all that exciting:
Code://#define INPUTMAX 255 compiles fine. const int INPUTMAX=255;
assign1b.c:79: error: variable-sized object may not be initialized
assign1b.c:79: warning: excess elements in array initializer
So const variable's are fine as long as they aren't an array initializer?
So are either of those lines actually line 79 of assign1b.c ?
My guess is that you'll find an array at that point in the code.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Yeah definitely. But why is it that C doesn't support the use of constants in that context? And are there any other exceptions I should know about?
I liked the typesafing of const in C++, but since it seems C's constants are poorly implemented I am just better off enlisting the preprocessor with #define?
Yes, although some recommend type-safe methods, since you could really define INPUTMAX as anything, integer, string etc.
Such methods the "enum hack"
And I think there are a couple of other ways... anyway see http://cboard.cprogramming.com/showt...ht=define+enumCode:enum whatever_t { INPUTMAX = 255 };
Last edited by zacs7; 09-14-2007 at 08:20 PM.
You may think that, keira, but you're wrong.
Salem's response reflected the fact that the error messages you're getting are typically associated with a messed up declaration of an array. They have no meaning for declarations of constants, such as the code you showed.
The reason is historical. When C was first created, #define's were the normally accepted ways to introduce compile time constants. The notion of const (as a keyword, or as a concept) did not appear until C was standardised (more accurately, it appeared while the standard was being developed, but in practical terms the main point of appearance of const was the standard. The actual meaning of const in C has a few meanings different from what was introduced in C++.
I'm sure there are, particularly if you expect C to behave like C++ (which is what I suspect you're doing). There are also some incompatibilities between C standards, and between the latest C standard and C++.
There are a vast number of differences between C and C++
http://david.tribble.com/text/cdiffs.htm
In C, const just means "tell me if I try to modify this memory location". The compiler doesn't regard the assigned value as special in any way. For example, trying to use it as an array size.
The symbol with the const qualifier ALWAYS occupies a slot in memory.
In C++, const is a lot closer to the idea of #define, and the compiler always knows what that value is. As such, you can use it for array sizes and any other place where the size must be known at compile time.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
That link is a good summary of the differences between C99 and C++. It does not really consider the incompatibilities between C++ and C89/90 - the definitive description of those incompatibilities is supplied in an annex of the C++ standard.
While I know what you mean, the eyes of the C++ standards committee would be spinning if they literally interpreted that characterisation of const. LOL! There is no involvement of the preprocessor (for C or C++) with const.
A more formal description is that const symbols;
1) have internal linkage in C++ (unless they also explicitly declared extern) but they have external linkage in C; and
2) can be treated by the compiler as compile-time constants in C++, but not in C.
The first means that, if a const is declared in a header file, it can be initialised (given a value) within a header file in C++, but must be defined only once (eg in a single source file) in C.
The second is the reason that a symbol which is a const integer cannot be used at compile time as an array size.
Another consequential incompatibility between C and C++ is that a const object in C++ must be initialised in every source file (unless is it also declared extern) but that is not required in C.