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

  • Home
  • SEARCH
  • 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 3440498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:26:20+00:00 2026-05-18T08:26:20+00:00

I declared a variable i in temp2.h extern i; which contains just one above

  • 0

I declared a variable i in temp2.h
extern i; which contains just one above line
and made another file
temp3.c

 #include<stdio.h>
#include<temp2.h>
int main ()
{
extern i;
i=6;
printf("The i is %d",i);
}

When I compiled above as
cc -I ./ temp3.c I got following errors

 /tmp/ccJcwZyy.o: In function `main':
temp3.c:(.text+0x6): undefined reference to `i'
temp3.c:(.text+0x10): undefined reference to `i'
collect2: ld returned 1 exit status

I had declared extern in temp3.c above as K R page 33 says as I mentioned in above post.
I tried another way for temp3.c with same header file temp2.h

 #include<stdio.h>
#include<temp2.h>
int main ()
{

i=6;
printf("The i is %d",i);
}

and compiled it cc -I ./ temp3.c and got following error

/tmp/ccZZyGsL.o: In function `main':
temp3.c:(.text+0x6): undefined reference to `i'
temp3.c:(.text+0x10): undefined reference to `i'
collect2: ld returned 1 exit status

I also tried

 #include<stdio.h>
#include<temp2.h>
int main ()
{
extern i=6;
printf("The i is %d",i);
} 

compiled this one

 cc -I ./ temp3.c

got same error as in post 1

 temp3.c: In function ‘main’:
temp3.c:5: error: ‘i’ has both ‘extern’ and initializer

So I have tried at least 3 different ways to use extern but non of them worked.

  • 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-18T08:26:21+00:00Added an answer on May 18, 2026 at 8:26 am

    When you declare a variable using extern , you are telling the compiler that the variable was defined elsewhere and the definition will be provided at the time of linking. Inclusion is a different thing altogether.

    extern

    An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it; this states the type of the variable. The declaration may be an explicit extern statement or may be implicit from context.

    -The C Programming Language

    A variable must be defined once in one of the modules(in one of the Translation Units) of the program. If there is no definition or more than one, an error is produced, possibly in the linking stage (as in example 1 and 2).

    Try something like the following

    a.c

    int i =10; //definition
    

    b.c

    extern int i; //declaration
    int main()
    {
        printf("%d",i);
    }
    

    Compile, link and create an executable using

    gcc a.c b.c -o executable_name
    

    or


    gcc -c a.c // creates a.o
    gcc -c b.c // creates b.o
    

    Now link the object files and create an executable

    gcc a.o b.o -o executable_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I declared global variable in js file which is filled after jquery.ajax call. Everything
I have a static variable declared in a file: static char *msgToUser[] = {
I have a boolean variable declared at the top of a class and when
What are the benefits of having a member variable declared as read only? Is
If a variable is declared as static in a function's scope it is only
I declared a private variable vector<SomeClass> theVector; someplace inside my SomeClass class. Why can't
How to know the size of a declared variable in GMP??or how can we
So I've got some C code: #include <stdio.h> #include <string.h> /* putting one of
Having trouble setting the value of a declared variable in MySQL. If I run
What is the difference between a variable declared as dynamic and a variable declared

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.