What's wrong about const?

S

shawn

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);
}

Test1 can not be compiled.
And test2.c can.
Why?
 
Z

Zoran Cutura

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);
}

Test1 can not be compiled.
And test2.c can.
Why?

Although one would think that using the keyword const makes the object
a constant that is not essentially true. In an array declaration
(definition) you must use a constant that can be evaluated at compile
time, but not at run time. An enumeration value is such a constant, but
const is just qualifying the object such that it cannot (should not) be
changed directly or indirectly, but there will be an object. The value
of an object cannot be evaluated at runtime.

As a rule, if any of the operands of an expression can be pointed to
with a pointer (or the address can be evaluated) the expression cannot
be constant.
 
R

Ramaraj M Bijur

Enum const is compilation time entity where us const variable is runtime
entity

I mean const variable value can take at the time of code is running not at
compilation and array size is calculated at the time of compilation

but in ENUM const compiler can get the value at compilation time

I hope it may help
 
A

Anupam

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);
}

Test1 can not be compiled.
And test2.c can.
Why?

const in C does not create a compile time const.
Don't confuse this with C++. This is one of the
many areas in which they vary.
 
J

Jack Klein

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);
}

Test1 can not be compiled.
And test2.c can.
Why?

What's wrong with you reading the answers to the same question when
you asked it yesterday. The first program can't be compiled because
the C language standard says it can't.
 
Z

Zoran Cutura

Zoran Cutura said:
changed directly or indirectly, but there will be an object. The value
of an object cannot be evaluated at runtime.

That should be "compiletime"
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top