4

K&R c 2nd edition(section 2.3) mentions

A constant expression is an expression that involves only constants. Such expressions may be evaluated at during compilation rather than run-time, and accordingly may be used in any place that a constant can occur

however, I have several doubts regarding it:

  1. Will this expression be considered as constant expression?

    const int x=5;
    const int y=6;
    int z=x+y;
    

    i.e using const keyword is considered constant expression or not?

  2. Is there any technique by which we can check whether an expression was evaluated during compilation or during run-time?

  3. Are there any cases where compile time evaluation produces different result than run-time evaluation?

  4. Should I even care about it? (maybe I use it to optimize my programs)

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Raman
  • 2,735
  • 1
  • 26
  • 46

4 Answers4

2
  1. Perhaps. A compiler can add more forms of constant expressions, so if it can prove to itself that the variable references are constant enough it can compute the expression at compile-time.
  2. You can (of course) disassemble the code and see what the compiler did.
  3. Not if the compiler is standards-compliant, no. The standard says "The semantic rules for the evaluation of a constant expression are the same as for nonconstant expressions" (§6.6 11 in the C11 draft).
  4. Not very much, no. :) But do use const for code like that anyway!
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Sir, May I ask about answer 3? `NULL`-dereference will produce different result, if I'm not wrong. Please enlight. – Sourav Ghosh Jul 08 '15 at 14:23
  • @SouravGhosh You can't dereference pointers in a constant expression, as far as I know? You can use pointers (including `NULL`) with `sizeof`, but you can't do a standalone dereferencing. – unwind Jul 08 '15 at 14:26
  • Yes, I was thinking about that only, right. I'll update my answer accordingly. Thanks. – Sourav Ghosh Jul 08 '15 at 14:31
1

using const keyword is considered constant expression or not?

>> No, it is not a constant. The variable using const is called const qualified, but not a compile time constant.

Is there any technique by which we can check whether an expression was evaluated during compilation or during run-time?

>> (as mentioned in Mr. Unwind's answer) Disassemble the code.

Are there any cases where compile time evaluation produces different result than run-time evaluation?

>> No, it will not. refer to Chapter §6.6 11, C11 standard.

FWIW, in case of usage with sizeof operator (compile time, though not constant expression), NULL pointer dereference will be ok. Compile time NULL pointer dereference invokes undefined behaviour.

Should I even care about it? (maybe I use it to optimize my programs)

>> Opinion-based, so won't answer.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0
  1. x and y are const, z is not. compiller probably will substitute x and y , but will not substitute z. but probably compiller will calc 5 + 6 as well and will assign to z directly.

  2. not sure you can check generated assembler code, but I do not know how this can be done.

  3. not. compile time means expression is already calculated in run time.

  4. I care :) but it appies only when you need fast execution.

Nick
  • 9,962
  • 4
  • 42
  • 80
0
  1. In C, the const qualifier is just a guarantee given by the programmer to the compiler that he will not change the object. Otherwise it does not have special meanings as in C++. The initializer for such objects with file- or global scope has to be a constant expression.

  2. As an extension, gcc has a builtin function (int __builtin_constant_p (exp)) to determine if a value is constant.

  3. No, it shall not - unless you exploit implementation defined or undefined behaviour and compiler and target behave differently. [1]

  4. As constant expressions are evaluated at compile-time, they safe processing time and often code space and possibly data space. Also, in some places (e.g. global initializers), only constant expressions are allowed. See the standard.


[1]: One example is right shifting a signed negative integer constant, e.g. -1 >> 24. As that is implementation defined, the compiler might yield a different result from a program run using a variable which holds the same value:

int i = -1;
(-1 >> 24) == (i >> 24)
^             ^--- run-time evaluated by target
+--- compile-time evaluated by compiler

The comparison might fail.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52