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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:24:51+00:00 2026-05-20T10:24:51+00:00

As far as I know, in C++ one can declare the same name multiple

  • 0

As far as I know, in C++ one can declare the same name multiple times, as far as it has the same type in all these declarations. To declare an object of type int, but NOT define it, the extern keyword is used. So the following should be correct and compile without errors:

extern int x;
extern int x; // OK, still declares the same object with the same type.
int x = 5;    // Definition (with initialization) and declaration in the same
              // time, because every definition is also a declaration.

But once I moved this to the inside of a function, the compiler (GCC 4.3.4) complains that I am redeclaring x and that it’s illegal. The error message is the following:

test.cc:9: error: declaration of 'int x'
test.cc:8: error: conflicts with previous declaration 'int x'

where int x = 5; is in line 9, and extern int x is in line 8.

My question is:
If multiple declarations are not supposed to be errors, then WHY is it an error in this particular case?

  • 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-20T10:24:51+00:00Added an answer on May 20, 2026 at 10:24 am

    An extern declaration declares something to have external linkage (meaning the definition is expected to appear at file scope in some compilation unit, possibly the current one). Local variables cannot have external linkage, so the compiler complains that you’re trying to do something contradictory.

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

Sidebar

Related Questions

As far as I know, Flash has to pass info off to another external
As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...)
I know FireBug with YSlow has to be by far my personal favourite. I
As far as I know, PHP is a dynamic type language. We don't need
First...if no one can answer this question it's ok... because I don't know how
As far as I know, foreign keys (FK) are used to aid the programmer
As far as i know it is not possible to do the following in
As far as I know (not much I'll admit), the currently popular programming paradigms
As far as I know, there's no way to use {% include %} within
The problem I'm running into is that as far as I know the delete

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.