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

  • Home
  • SEARCH
  • 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 7711057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:16:48+00:00 2026-06-01T01:16:48+00:00

I had this code: int foo(void){ return 1; } int main(void){ static const int

  • 0

I had this code:

int foo(void){
    return 1;
}

int main(void){
    static const int x = foo();

    //do stuff
    return 0;
}

But I got an error about initializing a static variable with a non-const value. I thought it had something to do with the const specifier, but it didn’t. I ended dropping the const keyword and doing this:

int foo(void){
    return 1;
}

int main(void){
    static int x = 0;
    if (x == 0) x = foo();

    //do stuff
    return 0;
}

Now, why can’t the compiler just delay the initialization of the static int x variable until it’s used, and more importantly, why can’t it just put it in a read-write section, and just enforce that it’s not written to in compile time? I’d like to use the const AND static keyword for improved semantics in my code, but I don’t really care how the compiler handles this, just let it work.

Is my understanding of the C standard wrong? Or is my compiler sucking? It’s MSVC 9.0.

  • 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-06-01T01:16:50+00:00Added an answer on June 1, 2026 at 1:16 am

    C requires it.

    From the C Standard:

    (C99, 6.7.8p4) “All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.”

    Note that the const qualifier does not mean constant but rather read-only. A const object is not a constant in C.

    The reason a static object cannot be initialized by a non constant value is related to the fact that the initialization of a static object is done “prior to program startup” (C99, 6.2.4p3).

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

Sidebar

Related Questions

so I had this code: #include <list> void j(){ list<int> first; } but then
I had a question regarding a C code. #include <stdio.h> void foo(void){ int a;
A co-worker asked about some code like this that originally had templates in it.
How does the following work? #include <limits> int main() { const int* const foo
Consider the following code: int SomeField; void Foo([Pure] Func<int, object> getData) { Contract.Requires(getData !=
I was looking at an authentication system which had this code: # prevents a
I had posted a question in regards to this code. I found that JTextArea
So basically this code was working fine before. I had some computer issues and
I've been having problems with this code I had spent the last 3 hours
Suppose you had code like this: _READERS = None _WRITERS = None def Init(num_readers,

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.