I'm confused by Const

S

shawn

As far as i know, keyword "const" is invented to replace "define" identifier.
But ...

test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

why test2.c can be compiled and test1.c not?
And what's the difference between c const and c++ const?
Thanks.
 
R

Richard Bos

As far as i know, keyword "const" is invented to replace "define" identifier.

It isn't. const is used to make an actual, in-memory object read-only,
while #define is used to create a compile-time, source code only, text
replacement.
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
why test2.c can be compiled and test1.c not?

Because enum constants are compile-time constants, whereas const objects
are read-only, but not, strictly speaking, constant. The name of the
qualifier is confusing, it's true.
And what's the difference between c const and c++ const?

C++ consts _are_ compile-time constant, AFAIK, but I'm not a C++ expert
so ICBW.

Richard
 
H

Horst Kraemer

As far as i know, keyword "const" is invented to replace "define" identifier.
No.

But ...

test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

why test2.c can be compiled and test1.c not?

Because in C89 a "const int" doesn't qualify as an expression for
array dimensioning while an enum does. An enum is an alias for some
int value known to the compiler from the definition of this enum while
a const int is not - even if the compiler "sees" the value to which it
is initialized.
And what's the difference between c const and c++ const?

One of the differences is that in C++ a const int may be used for
array dimensioning (if its value is "visible" to the compiler
according to a definition of "visible" given by the C++ Standard).
Specifically

const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";

would be OK in C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top