What is the use of qualifier const

Discussion in 'C Programming' started by istillshine@gmail.com, Apr 12, 2008.

  1. Guest

    It seems I never need to use it. It only made a long line longer, and
    made you type five more characters. I feel so strange why people are
    talking about it.
    , Apr 12, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > It seems I never need to use it. It only made a long line longer, and
    > made you type five more characters. I feel so strange why people are
    > talking about it.


    Don't you ever create read only variables or string literals or pass
    pointers to read only data to functions?

    --
    Ian Collins.
    Ian Collins, Apr 12, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 11, 11:33 pm, Ian Collins <> wrote:

    > Don't you ever create read only variables or string literals or pass
    > pointers to read only data to functions?


    I do. But I never used const. I just took care not to modify them. In
    fact, I never needed to modify them.

    Who will put a qualifier before something and modify this thing
    later? And if he dose not modify this thing later, why dose he put a
    qualifier before it?

    I could not under why people do that. When I saw some old C code, I
    found people never used const. Their programs are still robust enough
    without const.
    , Apr 12, 2008
    #3
  4. Ian Collins Guest

    wrote:
    > On Apr 11, 11:33 pm, Ian Collins <> wrote:
    >
    >> Don't you ever create read only variables or string literals or pass
    >> pointers to read only data to functions?

    >
    > I do. But I never used const. I just took care not to modify them. In
    > fact, I never needed to modify them.
    >

    Then you enjoy skating on thin ice, or never make mistakes.

    > Who will put a qualifier before something and modify this thing
    > later? And if he dose not modify this thing later, why dose he put a
    > qualifier before it?
    >

    So the compiler will tell him if he accidentally does attempt to modify it.

    > I could not under why people do that.


    Because they want to write safe, well behaved programs. They want their
    constant data stored somewhere it can't be modified. Many embedded
    systems have more FLASH than RAM, so it makes sense to store constant
    data in FLASH.

    > When I saw some old C code, I
    > found people never used const. Their programs are still robust enough
    > without const.


    That's because cost was introduced later. It wouldn't have been
    introduced if there wasn't some thing to fix or a perceived need to
    differentiate between constant and volatile data.

    --
    Ian Collins.
    Ian Collins, Apr 12, 2008
    #4
  5. user923005 Guest

    On Apr 11, 8:41 pm, wrote:
    > On Apr 11, 11:33 pm, Ian Collins <> wrote:
    >
    > > Don't you ever create read only variables or string literals or pass
    > > pointers to read only data to functions?

    >
    > I do. But I never used const. I just took care not to modify them.  In
    > fact, I never needed to modify them.


    It's a great way to do it. Just like grenades. Don't bother with
    putting pins in them, just hold the handles down and they are safe.

    > Who will put a qualifier before something and modify this thing
    > later?  


    The second programmer, who never saw the qualifier put there.

    > And if he dose not modify this thing later, why dose he put a
    > qualifier before it?


    To give him an error message to tell him he did something stupid.

    > I could not under why people do that.  When I saw some old C code, I
    > found people never used const.  Their programs are still robust enough
    > without const.


    If you substitute the word "fragile" with "robust" then I can agree
    with you.
    Old code often won't even compile with modern compilers, and the older
    it is the less likely it will compile.
    If it does succeed in compiling, chances are good it won't run.
    If it does run, it is likely to be packed with exploits like gets()
    because old time programmers did not have to deal with a hostile
    audience.
    Old numerical stuff is sometimes villianously bad.
    user923005, Apr 12, 2008
    #5
  6. Guest

    On Apr 12, 9:04 am, Eric Sosman <> wrote:

    > Those old programs were also robust enough without `void'
    > and without function prototypes, so what's your point?


    I think void and function prototypes are useful additions. But for
    const, I rarely use it largely because I don't want to type five
    extra characters,
    'c', 'o', 'n', 's', and 't'. Between pounding five more characters
    and taking care not to modify the constant variable, I prefer the
    latter.
    , Apr 12, 2008
    #6
  7. On 12 Apr, 14:16, wrote:
    > On Apr 12, 9:04 am, Eric Sosman <> wrote:


    > >      Those old programs were also robust enough without `void'
    > > and without function prototypes, so what's your point?

    >
    > I think void and function prototypes are useful additions.  But for
    > const,  I rarely use it largely because I don't want to type five
    > extra characters,
    > 'c', 'o', 'n', 's', and 't'.  Between pounding five more characters
    > and taking care not to modify the constant variable, I prefer the
    > latter.


    http://en.wikipedia.org/wiki/APL_(programming_language)

    --
    Nick Keighley
    Nick Keighley, Apr 14, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sergey Tolstov

    const qualifier and VC6.0

    Sergey Tolstov, Oct 7, 2003, in forum: C++
    Replies:
    8
    Views:
    463
    Howard
    Oct 7, 2003
  2. Mahesh Tomar

    Const Qualifier question

    Mahesh Tomar, Sep 1, 2004, in forum: C++
    Replies:
    4
    Views:
    362
    Old Wolf
    Sep 2, 2004
  3. Patrick Guio

    const qualifier problem

    Patrick Guio, Nov 9, 2004, in forum: C++
    Replies:
    5
    Views:
    325
    Vyacheslav Kononenko
    Nov 9, 2004
  4. Javier
    Replies:
    2
    Views:
    548
    James Kanze
    Sep 4, 2007
  5. paulo
    Replies:
    9
    Views:
    694
    James Kanze
    Mar 6, 2009
Loading...

Share This Page