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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:03:49+00:00 2026-05-29T10:03:49+00:00

Consider the sample code below : File1.cpp #include <iostream> static int x = 6;

  • 0

Consider the sample code below :

File1.cpp

#include <iostream>

static int x = 6;                          // line 3

int main()
{
   int x = 10;                             // line 7
   {
      extern int x;                        // line 9
      x = x + 5;
      std::cout << "x = " << x << "\n";
   }
}

The external declaration in line 9 causes x in the following lines to refer to the static x (defined in line 3) instead of the automatic x (defined in line 7). But I get the following compiler warning:

File1.cpp:3: warning: ‘x’ defined but not used

Even with this warning, I get the expected output which is x = 11, which is 5 added to the value of the static variable x.

Why is the compiler giving the above warning? I am using GCC version g++ (GCC) 3.4.6.

  • 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-29T10:03:50+00:00Added an answer on May 29, 2026 at 10:03 am

    Note: It would have been easier if you had numbered all lines.

    You are are using an old version of GCC that doesn’t handle extern correctly. If you use a modern version, you’ll see three issues with your code.

    First, the local x in main() is not used (warning). Second, the static global x is not used (warning). Third, the external x is not defined(error).

    Either you should not declare the global x static, or you should use ::x with no extern specification.

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

Sidebar

Related Questions

Consider the following sample code below: #include <iostream> using namespace std; class base {
Consider the sample code below: #include <iostream> using namespace std; class A { private:
Consider the sample code below: #include <iostream> using namespace std; class core { public:
Consider the sample program below: #include <iostream> using namespace std; class test { public:
Conside the following sample code below: #include <iostream> using namespace std; class base {
Consider the following sample code: #include <iostream> using namespace std; class base { public:
First consider this sample C++ code: std::string input1, input2, input3; std::cout << Enter Input
My questions are divided into three parts Question 1 Consider the below code, #include
Consider, static void Main(string[] args) { Console.WriteLine(fun()); } static int fun() { int i
Consider my C++ code below: int _tmain(int argc, _TCHAR* argv[]) { int by =

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.