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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:34:51+00:00 2026-06-05T02:34:51+00:00

Given the following header file, if ‘a’ is defined inside the main body, I

  • 0

Given the following header file, if ‘a’ is defined inside the main body, I get a warning “unused variable ‘a'” and linker error “undefined reference to ‘a’.

header.h:

#ifndef HEADER_H
#define HEADER_H

#include <iostream>

extern int* a;

void f()
{
    std::cout<<*a <<std::endl;
    return;
}

#endif

main.cpp:

#include "header.h"

int main()
{
    int* a = new int(10);

    f();
}

However, if ‘a’ is defined outside of main(), the program links with no errors and f() works as expected (prints 10). Why is this?

Example:

int* a = new int(10);

int main()
{
    f();
}
  • 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-05T02:34:54+00:00Added an answer on June 5, 2026 at 2:34 am

    You need to learn about name binding, which determines how two
    declarations of the same name are related. When you define a variable
    within a function, its name has no linkage; i.e. the entity it refers to
    is distinct from any other entity in the program.

    More generally: a declaration (and in this sense, a definition is also a
    declaration) associates a symbol with an entity—an object (in
    which case, the declaration declares a variable), a function, a
    reference, a type or anything else you can declare in C++. Whether
    different declarations of the same name associate with the same entity
    or not is defined by their linkage. C++ recognizes three
    different types of linkage:

    • external linkage, in which the entity can be referred to by declarations in other transation units,

    • internal linkage, in which the entity can be referred to by other declarations in the same translation unit,

    • and no linkage, in which the entity cannot be referred to by any other declaration.

    Variables declared at block scope (i.e. local variables) have no
    linkage, unless they are explicitly declared extern (and a local
    variable declared extern cannot be a definition). So the int a in
    main declares (and defines) an entity which is independent of any
    other a in the program. Variables declared in namespace scope have
    external linkage, unless they are declared static, in which case they
    have internal linkage; when you define int a at namespace scope, it
    has external linkage, and so refers to the same entity you declared with
    extern int a in the header.

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

Sidebar

Related Questions

Given a postscript file that has the following header %!PS-Adobe-3.0 I would like to
If I declare a global variable in a header file and include it in
Given the following: large project with thousands of C++ source files no common header
Given the following two header files: #ifndef EVENT_HANDLER_H #define EVENT_HANDLER_H #include <SFML/Window.hpp> #include <SFML/Window/Event.hpp>
I'm trying to get actual value of given xpath. I am having the following
I have following PHP code $filename = 'a56.flv'; $file = C:/xampp/htdocs/site/flv/a56.flv; header(Content-Type: application/force-download); header('Content-Type:
In my header file, I'm using Wordpress function bloginfo following three times: <link rel=pingback
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?

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.