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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:47:36+00:00 2026-06-12T00:47:36+00:00

There are three files: source1.c source2.c header.h The two source-files includes the header. This

  • 0

There are three files:
source1.c
source2.c
header.h

The two source-files includes the header.

This is the code of the header:

struct
{
    int a;
    int b
} x;

What happens now is that the struct becomes global and the two source-files now shares the struct called x. Why does this happen?

I know that if you write the following code it will make two global variables. One for each of the source-files. (they don’t share the globals)

int x = 0;

The last piece of code makes sense to me but i really don’t understand the one with the struct..

EDIT:
Hmm everybody here think I should get linker errors. My current code is for an embedded system (nxtOSEK). I’ll try to convert it to a regular C program later.

EDITEDIT:
I’m back with examples in regular C. As you can see it is not only possible with structs but also with regular variables.

source1.c

#include "header.h"

int main(void)
{
    f();
    x = 1;
    f();
}

source2.c

#include "header.h"

void f()
{
    printf("source2: %i\n", x);
}

header.h

#include <stdio.h>

int x;

Output

source2: 0
source2: 1

Note that x must not be declared for it to work or it gives a linker error like everyone here said. (I don’t know why it work with the embeded system..)

It also looks like I misread Eric Postpischil’s answer which looks correct.

  • 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-12T00:47:38+00:00Added an answer on June 12, 2026 at 12:47 am

    An external declaration of an object identifier at file scope that has an initializer is a definition. The declaration int x = 0; is a definition because x is initialized.

    An external declaration of an object identifier at file scope that does not have an initializer is a tentative definition. The declaration struct {…} x; is a tentative definition because x is not initialized.

    Multiple definitions at link time cause an error.

    Multiple tentative definitions at link time may be coalesced to a single definition, which is initialized with zero. This was traditional Unix behavior and was the default behavior with GCC prior to version 10; GCC marked tentative definitions as “common” symbols by default. With GCC version 10 and later, multiple-definition errors may result. The old behavior can be selected with the -fcommon command-line switch to GCC.

    If you change int x = 0; to int x;, you will not a get a link error while using build tools that treat tentative definitions as common symbols. If you change struct {…} x; to struct {…} x = {0};, you will get a link error.

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

Sidebar

Related Questions

In Qt Source files, there are two versions of header files, such as: qxmlstream.h
Have there been incompatibilities between Java releases where Java source code/Java class files targeting
Now I have two directory, all of header files *.h are included in directory
I'm trying to create a struct used in two .c source files to make
I have a struct defined in a header file with three other files that
I have three source files in a folder. I simply want to compile them
I have a Util package with source files in three seperate directories, defined like
Is there a way to export files/folders from Source Safe? (i.e. getting rid of
I have java source code in a text file. There has to be entered
Main article there is a header file and a source file . After copying

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.