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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:51:14+00:00 2026-05-29T22:51:14+00:00

file1.cpp #include <iostream> extern int u; int i=9; int j=i+9; int main() { std::cout<<u;

  • 0

file1.cpp

#include <iostream>

extern int u;
int i=9;
int j=i+9;
int main()
{
   std::cout<<u;
   return 0;
}

file2.cpp

extern int j;
int u=j+9;

Result is u=9 but not 27

This shows that j is initialized twice – First zero(due to which variable u get value 9) and then with 18

Is it possible ?? My meaning for initialization is destroyed here.

I also try to make variable j constant to see what will happen next

file1.cpp //after changing j to constant

extern int u;
int i=9;
extern const int j=i+9;

This has same output as before.

However if I change int j=i+9;(in file1.cpp) to int j=9;

Surprisingly, I got correct values i.e. u=18;

  • 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-29T22:51:18+00:00Added an answer on May 29, 2026 at 10:51 pm

    The order of initialization of global variables declares across different translation Units is not specified.

    The globals u and i, j reside in different translation units in your code, so the order whether u gets initialized first or i & j get initialized before u is Unspecified.
    This invokes an Undefined Behavior in your code.
    This invokes an Unspecified Behavior in your code

    However, Note that order of initialization of globals in the same translation unit is well-defined.
    i.e: it is well defined that i will be initialized before j in your code.

    What you are seeing is a classic case of Static Initialization Fiasco.

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

Sidebar

Related Questions

I have got a f2.cpp file // f2.cpp #include <iostream> void f2() { std::cout
I have the following source code in main.cpp: #include <iostream> #include <iomanip> int main()
Consider the sample code below : File1.cpp #include <iostream> static int x = 6;
I created the following two C++ files: Stack.cpp #include<iostream> using namespace std; const int
I have the main .cpp file with this: #include stdafx.h #include Form1.h #include <iostream>
I have the following source files: //test1.cpp #include <iostream> using namespace std; inline void
I've this following code in C++: //file0.cpp #include file11.cpp #include file22.cpp #include<iostream> #include<conio.h> using
I have 2 .cpp files a1.cpp #include <iostream> #include <conio.h> using namespace std; using
Consider following source files 1.cpp #include <iostream> using namespace std; struct X { X()
What am I doing wrong here? // file main.cpp #include <iostream> #include <boost/numeric/ublas/vector.hpp> #include

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.