title: Why can I change a local const variable through pointer casts but not a global one in C?
body: <p>I wanted to change value of a constant by using pointers.</p>
  
  <p>Consider the following code</p>
  
  <pre><code>int main()
  {
      const int const_val = 10;
      int *ptr_to_const = &amp;const_val;
  
      printf("Value of constant is %d",const_val);
      *ptr_to_const = 20;
      printf("Value of constant is %d",const_val);
      return 0;
  }
  </code></pre>
  
  <p>As expected the value of constant is modified.</p>
  
  <p>but when I tried the same code with a global constant, I am getting following run time error.
  The Windows crash reporter is opening. The executable is halting after printing the first printf statement in this statement "*ptr_to_const = 20;"</p>
  
  <p>Consider the following code</p>
  
  <pre><code>const int const_val = 10;
  int main()
  {
      int *ptr_to_const = &amp;const_val;
      printf("Value of constant is %d",const_val);
      *ptr_to_const = 20;
      printf("Value of constant is %d",const_val);
      return 0;
  }
  </code></pre>
  
  <p>This program is compiled in mingw environment with codeblocks IDE.</p>
  
  <p>Can anyone explain what is going on?</p>