1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

const qualifier

Discussion in 'C' started by sharmila, Apr 4, 2006.

  1. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    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.
     
    Last edited: Apr 4, 2006
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    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.
     
  3. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    what is a compile time constant.
     
  4. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    Compile time constant are constants which are defined at compile time and substituted also at the compile time. Something like macros.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Compile time, as opposed to runtime, is the time when a compiler compiles the code.
     
  6. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    In the link
    http://www.eskimo.com/~scs/c-faq.com/cpp/constdefine2.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?
     
  7. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    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.
     
  8. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    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.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    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:
    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:
    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
     
  10. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    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++.
     
  11. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    In C Macros are compile time constants.
     
  12. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    I mean using const qualifier.i.e compile-time and run-time concept for 'const' variables.
     
  13. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    I guess we need to be testing this on GCC compilers rather than on VC and then we can conclude anything on const in C
     
  14. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    ok.Coderzone.Thank you.I will also try for that.If I got any doubt I will get back to you.
    Thank you once again.
     
  15. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi,
    I tested using gcc compiler.it is not giving any errors.It is executing normally.But in VC with .c extension it is giving errors.
     
  16. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    So probably its the flaw of the VC in case of const in C. Both are compile time constants const / Macros.
     
  17. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0

Share This Page