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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:13:49+00:00 2026-06-09T12:13:49+00:00

In principle, a variable defined outside any function (that is, global, namespace, and class

  • 0

In principle, a variable defined outside any function (that is, global, namespace, and class static variables) is initialized before main() is invoked. Such nonlocal variables in a translation unit are initialized in their declaration order

Above are the lines from the class notes given by my lecturer.

#include <iostream>

using namespace std;
int a=99;
int main(int argc, char *argv[]) 
{
  cout<<a<<endl;
  cout<<b<<endl;
  return 0;
}
int b=100;

There is an error while I run this. Isn’t it true that b assigned to 100 before main() is called?

  • 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-09T12:13:51+00:00Added an answer on June 9, 2026 at 12:13 pm

    The problem here is not initialisation order: b is indeed initialised before main starts running.

    The problem is the “visibility” of b. At the point where main is being compiled, there is no b.

    You can fix it by either moving the definition/initialisation of b to before main:

    #include <iostream>
    
    using namespace std;
    int a = 99;
    int b = 100;
    int main (int argc, char *argv[]) {
        cout << a << '\n';
        cout << b << '\n';
        return 0;
    }
    

    or simply indicate that b exists:

    #include <iostream>
    
    using namespace std;
    int a = 99;
    extern int b;
    int main (int argc, char *argv[]) {
        cout << a << '\n';
        cout << b << '\n';
        return 0;
    }
    int b = 100;
    

    Neither of those two solutions change when b is created or initialised at run-time, they simply make b available within main.

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

Sidebar

Related Questions

is any way to return reference at class parameter or global variable? I try
I have a class that handles all of my session variables in my asp.net
The principle behind the CQS architectural pattern is that you separate your queries and
I thought that in principle Haskell's type system would forbid calls to impure functions
I understand that according to pigeonhole principle, if number of items is greater than
I believe that readability and the KISS principle are the most important things in
I'm wondering if there's a specific programming principle (Demeter?) that supports the idea that
I have a custom class called CustomClass. It contains a variable called Name and
I'm working on a .NET application in C# that will generate a variable-length string
Lets say we have this really trivial classes: class A { virtual int Function(int

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.