Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   const qualifier (http://www.go4expert.com/forums/const-qualifier-t686/)

sharmila 4Apr2006 15:42

const variable
 
Hi all,
I got a doubt about const qualifier.
In a site i read that

The const qualifier really means ``read-only''; an object so qualified is a run-time object which cannot (normally) be assigned to. The value of a const-qualified object is therefore not a constant expression in the full sense of the term, and cannot be used for array dimensions, case labels, and the like. (C is unlike C++ in this regard.) When you need a true compile-time constant, use a preprocessor #define (or perhaps an enum).

Here what is the meaning of run-time object.

coderzone 4Apr2006 19:27

Re: const qualifier
 
You cannot do the following

const int N = 10;

then in some other function you want it as

int arr[N]

This will give error as N is not a compile time constant.

Again if you wish to write

N = 20 then it will also give error because N is a read only calue and cannot be overwritten.

sharmila 4Apr2006 19:30

Re: const qualifier
 
what is a compile time constant.

coderzone 4Apr2006 22:36

Re: const qualifier
 
Compile time constant are constants which are defined at compile time and substituted also at the compile time. Something like macros.

shabbir 4Apr2006 22:45

Re: const qualifier
 
Compile time, as opposed to runtime, is the time when a compiler compiles the code.

sharmila 5Apr2006 09:20

Re: const qualifier
 
In the link
http://www.eskimo.com/~scs/c-faq.com...stdefine2.html
it is written that
When you write

const int num2 = 45;

on the other hand, the main part of the compiler allocates a variable, of type int, and initializes it with the value 45, and makes a note to itself that it should complain if you ever try to modify this variable anywhere else in the source file.

I heard that for const variables memory is allocated in ROM.So,I think at compile time it is allocating memory in ROM and it is storing value there.But it is substituting the value at run time.Am I correct?

coderzone 5Apr2006 10:26

Re: const qualifier
 
Quote:

Originally Posted by sharmila
I heard that for const variables memory is allocated in ROM.So,I think at compile time it is allocating memory in ROM and it is storing value there.But it is substituting the value at run time.Am I correct?

ROM here means the memory is readable and not in the actual ROM of the system. ROM cannot be used to even write the initial value of the const.

sharmila 5Apr2006 11:51

Re: const qualifier
 
Thank you.But my doubt remailns like that.In another site
http://builder.com.com/5100-6370_14-5090819.html
it is written
const int COMPILE_TIME = 5; // set at compile-time; its value is 5 (five)
int read_int() { int val; std::cin >> val; return val; }
const int RUN_TIME= read_int(); // set at runtime; can be any value

and so we can use COMPILE_TIME it in arrays,switch-cases.But we can't use RUN_TIME in arrays and so on.Can we do like that?

one more doubt.For constants memory is allocated in which memory segment.stack,heap or someting else.

shabbir 5Apr2006 19:58

Re: const qualifier
 
I guess everybody is getting confused as to what the constant is all about and here is what I have to say that will clear all the doubts.

const is a type of variable whose value cannot and should not be changed. Now there seems to be a different in the compilation when you have the file extension is .c and .cpp.

Here a sample program
Code: C

const int i = 10;
void main()
{
    char arr[i];
}

The above program will compile successfully in .cpp file extension but not in .c file extension suggesting that the const behaves as compile time constant as well as runtime constant.

Taking other example
Code: C

void main()
{
    int const i=123;
    int *ip;
   
    char const c='c';
    char *cp;
   
    char const *arr="coderzone";
    char **arrp;
   
    // Changing constant integer i.e. const int/int const
   
    printf(" %d %x \n",i,&i);
    ip=&i;
    *ip=456;
    printf(" %d %x %x %d \n\n",i,&i,ip,*ip);
   
    // Changing constant character i.e. const char/char const
   
    printf(" %c %x \n",c,&c);
    cp=&c;
    *cp='a';
    printf(" %c %x %x %c \n\n",c,&c,cp,*cp);
   
    // Changing constant string i.e. const char * /char const
   
    printf(" %s %x \n",arr,&arr);
    arrp=&arr;
    *arrp="go4expert";
    printf(" %s %x %x %s \n\n",arr,&arr,arrp,*arrp);
}

The above sample when compiled in .c file will give 3 warning but will compile and run succesfully giving you an edited value of the constant but will give error in .cpp file and so the gist is in C const is not a compile time constant but in cpp it is.

I hope this clears the mystery of const somewhat

Thanks
Shabbir

sharmila 6Apr2006 12:59

Re: const qualifier
 
Hi shabbir,
Thank you.I tried that one and I understood how we have to use const variables.In C are we having any compile-time constants?or this compile-time and run-time concept is only for C++.


All times are GMT +5.5. The time now is 05:21.