Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6813119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:31:58+00:00 2026-05-26T20:31:58+00:00

Is accessing a non- const object through a const declaration allowed by the C

  • 0

Is accessing a non-const object through a const declaration allowed by the C standard?
E.g. is the following code guaranteed to compile and output 23 and 42 on a standard-conforming platform?

translation unit A:

int a = 23;
void foo(void) { a = 42; }    

translation unit B:

#include <stdio.h>

extern volatile const int a;
void foo(void);

int main(void) {
    printf("%i\n", a);
    foo();
    printf("%i\n", a);
    return 0;
}

In the ISO/IEC 9899:1999, I just found (6.7.3, paragraph 5):

If an attempt is made to modify an object defined with a const-qualified type through use
of an lvalue with non-const-qualified type, the behavior is undefined.

But in the case above, the object is not defined as const (but just declared).

UPDATE

I finally found it in ISO/IEC 9899:1999.

6.2.7, 2

All declarations that refer to the same object or function shall have compatible type;
otherwise, the behavior is undefined.

6.7.3, 9

For two qualified types to be compatible, both shall have the identically qualified
version of a compatible type; […]

So, it is undefined behaviour.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:31:59+00:00Added an answer on May 26, 2026 at 8:31 pm

    TU A contains the (only) definition of a. So a really is a non-const object, and it can be accessed as such from a function in A with no problems.

    I’m pretty sure that TU B invokes undefined behavior, since its declaration of a doesn’t agree with the definition. Best quote I’ve found so far to support that this is UB is 6.7.5/2:

    Each declarator declares one identifier, and asserts that when an
    operand of the same form as the declarator appears in an expression,
    it designates a function or object with the scope, storage duration,
    and type indicated by the declaration specifiers.

    [Edit: the questioner has since found the proper reference in the standard, see the question.]

    Here, the declaration in B asserts that a has type volatile const int. In fact the object does not have (qualified) type volatile const int, it has (qualified) type int. Violation of semantics is UB.

    In practice what will happen is that TU A will be compiled as if a is non-const. TU B will be compiled as if a were a volatile const int, which means it won’t cache the value of a at all. Thus, I’d expect it to work provided the linker doesn’t notice and object to the mismatched types, because I don’t immediately see how TU B could possibly emit code that goes wrong. However, my lack of imagination is not the same as guaranteed behavior.

    AFAIK, there’s nothing in the standard to say that volatile objects at file scope can’t be stored in a completely different memory bank from other objects, that provides different instructions to read them. The implementation would still have to be capable of reading a normal object through, say, a volatile pointer, so suppose for example that the “normal” load instruction works on “special” objects, and it uses that when reading through a pointer to a volatile-qualified type. But if (as an optimization) the implementation emitted the special instruction for special objects, and the special instruction didn’t work on normal objects, then boom. And I think that’s the programmer’s fault, although I confess I only invented this implementation 2 minutes ago so I can’t be entirely confident that it conforms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is accessing const variables faster than non- const variable? I'm wondering if it is
I want to block non-browser clients from accessing certain pages / successfully making a
When accessing an object in a DataTable retrieved from a database, are there any
When accessing member that doesn't exist, automatically creates the object. $obj = new ClassName();
when accessing foo() of base using derived class's object. #include <iostream> class base {
I'm dealing with a 2D array with the following characteristics: const int cols =
I'm writing a NON-GUI app which I want to be cross platform between OS
I'm accessing a data context object that is auto-generated by using LINQ to SQL.
I want to block all non-US users from accessing my website. I'm using nginx
Some time ago I was confused by the following behavior of some code when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.