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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:59:04+00:00 2026-06-01T08:59:04+00:00

I am unable to understand the weird behaviour of the program. It throws a

  • 0

I am unable to understand the weird behaviour of the program. It throws a warning but the output is surprising. I have 3 files, file1.c, file2c, file1.h

file1.c :

#include "file1.h"

extern void func(void);

 int main(void)
{
func();
printf("%d\n",var);
return 0;
}

file2.c:

int var;

 void func(void)
{
var = 1;
}

and file1.h is :

#include<stdio.h>

extern double var;

When I compile and run, it shows a warning as expected but on printing prints 1. But how is it possible when I am changing the var declared in file2.c

  • 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-01T08:59:05+00:00Added an answer on June 1, 2026 at 8:59 am

    Just setting the type on an extern definition doesn’t change the value stored in memory – it only changes how the value is interpreted. So a value of 0x0001 is what is stored in memory no matter how try to interpret it.

    Once you’ve called func() you’ve stored the value 0x0001 in the location where a is stored. Then in main when a is passed to printf the value stored at location a plus the next 4 bytes (assuming 32bits) is pushed onto the stack. That is the value 0x0001xxxx is pushed onto the stack. But the %d treats the value passed in as an int and will read the first 4 bytes on the stack, which is 0x0001 so it will print 1.

    Try changing your code as follows:

    file1.c :

    #include "file1.h"
    
    extern void func(void);
    
    int main(void)
    {
        func();
        printf("%d %d\n",var);
        return 0;
    }
    

    file2.c:

    int var;
    int var2;
    
    void func(void)
    {
        var = 1;
        var2 = 2;
    }
    

    and file1.h is :

    #include<stdio.h>
    
    extern double var;
    

    You will probably get the output 1 2 (although this depends on how the compiler stores the variables, but it’s likely it will store them in adjacent locations)

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

Sidebar

Related Questions

I am unable to understand the weird behavior of this program. I have 2
Recently I saw this piece of JavaScript code, but have been unable to understand
I have read the link below but I am unable to understand the exact
I have read article at https://developer.mozilla.org/en/DOM/element.addEventListener but unable to understand useCapture attribute. Definition there
I am having following code but unable to understand as to why no match
I have some questions about basic CSS that I was unable to understand or
I might be missing something here but am unable to understand the true purpose
I've done some searching, but I've been unable to fully understand how this is
I have written a small program , to understand how futures work in c++0x.
This might be a dumb question but I'm unable to understand: When we create

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.