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 9188427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:00:18+00:00 2026-06-17T20:00:18+00:00

At the top of some file in my program, outside all functions, I have

  • 0

At the top of some file in my program, outside all functions, I have these variables:

namespace {
  int foo = foo_func();
}

int bar = bar_func();

As you know, foo is a variable local only to that file, but bar is accessible to every file.

…but question: When are the functions foo_func() and bar_func() actually run? Does this happen before main() runs, or possibly sometime later (say just before those values are actually needed)?

  • 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-17T20:00:20+00:00Added an answer on June 17, 2026 at 8:00 pm

    The language specification states that the initializing functions will be executed before any function from that translation unit is called or any object defined in that translation unit is accessed. So, in general case it really depends on how your definitions are spread across translation units.

    The initialization is carried out in top-to-bottom order, so, given your order of definitions, bar_func should see already initialized foo, but foo_func should see “uninitialized” (i.e. zero-initialized) bar.

    Note, that if your main resides in another translation unit, it means that the initialization does not have to happen before main(). Yet, if you attempt to access foo or bar from main (or from anywhere else), that should guarantee that the initialization process is triggered for the entire translation unit that defined these variables.

    Also, if your initializers are constant expressions (constexpr functions), then the whole initialization can be performed statically, which typically means that the variables will begin their lifetimes in already initialized (compile-time initialized) states.

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

Sidebar

Related Questions

I have a page with elements, with relative position. style=position: relative; Some have top/left
I have added some backgrounds on stage and then on top of that adding
If I put main on the top of the source file and invoke some
I have a Perl script (standalone program) which contains some subs I'd like to
I wanna build a program to crawl some websites, check for the style.css file,
I am having some problems with writing a CSV file. The program, I made
I have a program that stores it's information in an XML file. This file
i have been trying to create a little java program that all it does
I want to add at the top of my xml file some notes for
I have a program extracting some information from a lot of large html pages.

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.