const problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • santosh

    #16
    Re: const problem

    pereges wrote:
    I have always found that using const is only a choice rather than a
    necessity.
    Yes. Most things in life are just choices.
    But this is just my opinion, I'm sure the experts here will
    differ.
    The point of const (in C) is to help the compiler to do more checks on
    your source. Consider this trivial program:

    #include <stdio.h>

    int main(void) {

    long double pi = 3.1415926535897 932384626433832 795029L;
    long double r;

    puts("Enter radius of circle:");
    scanf("%Lf", &r);
    printf("Area of the circle is: %Lf\n", pi * (r * r));
    return 0;
    }

    Now imagine this as a part of a much larger program or function.
    Obviously 'pi' holds the value of a constant and should not be changed.
    However if some other code in your program happens to modify 'pi' your
    compiler will silently accept the modification and your program will
    start giving you wrong or inaccurate results. This is where
    qualifying 'pi' with const will start paying off. Now anywhere a direct
    change is made to 'pi' the compiler will give you a diagnostic warning
    you of it, so you can correct the code and prevent undefined behaviour.

    As someone else said, const may appear rather meritless for small,
    one-man programs, but it will start paying dividends when the program
    grows larger and is developed by a team, even in spite the quirkiness
    of C's const.

    PS. You can still attempt to write to a const object and evade compiler
    warnings by writing through a pointer, but that's far less likely to
    happen in real code than direct in-advertant modification.

    Comment

    • David Thompson

      #17
      Re: const problem

      On Mon, 28 Apr 2008 15:52:17 +1200, Ian Collins <ian-news@hotmail.co m>
      wrote:
      David Thompson wrote:
      On Mon, 14 Apr 2008 09:04:49 -0700, Keith Thompson <kst-u@mib.org>
      wrote:
      <snip>
      I don't think pointers would be a problem. For example, C++ did
      change the semantics of const. In essence, if an object is declared
      including a 'static' (classwide) data member
      "const" and its initializer is a constant expression, then any
      and of an integer or enumeration type (in C++ enum types are distinct,
      not just aliases for integer types as they are in C)
      reference to the object is a constant expression. This allows <snip>
      But most of the places C (or C++) requires a constant expression do
      require integer (constant expression), including the example you gave
      of a case label, and this feature is enough for those.
      Which feature?
      The feature of C++ that Keith described and I slightly corrected, that
      a const variable(!) of integer or enum type initialized by a constant
      expression is acceptable in a constant expression. (And the which, or
      something like it, the upthread discussion suggested for C.)

      - formerly david.thompson1 || achar(64) || worldnet.att.ne t

      Comment

      Working...