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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:16:19+00:00 2026-05-28T15:16:19+00:00

I came across some code today that surprised me. A variable was defined (outside

  • 0

I came across some code today that surprised me. A variable was defined (outside of a function) in the .c file as being static. However, in the .h file it was declared as being extern. Here is a similar example of the code:

Structure definitions and declaration in .h:

typedef struct
{
    unsigned char counter;
    unsigned char some_num;
} One_Struct;

typedef struct
{
    unsigned char counter;
    unsigned char some_num;
    const unsigned char * p_something;
} Another_Struct;

typedef struct
{
    One_Struct * const p_one_struct;
    Another_Struct * const p_another_struct;
} One_Useful_Struct;

extern One_Useful_Struct * const p_my_useful_struct[];

Definition and initialization in .c:

static One_Useful_Struct * const p_my_useful_struct[MAX_USEFUL_STRUCTS] =
{
    &p_my_useful_struct_regarding_x,
    &p_my_useful_struct_regarding_y,
};

Question:
So my question is, why didn’t I receive a compiler error or warning?

The code has been successfully running in other projects for some time now. I did note that the pointer is never used outside of the .c file in which it is defined and was properly defined as static (I removed the external declaration). The only reason that I found it was because I ran Lint on the project and Lint picked it up.

  • 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-28T15:16:20+00:00Added an answer on May 28, 2026 at 3:16 pm

    It’s certianly not standard C. GCC and clang both detect and give an error on this case:

    $ gcc example.c
    example.c:4: error: static declaration of ‘x’ follows non-static declaration
    example.c:3: error: previous declaration of ‘x’ was here
    $ clang example.c
    example.c:4:12: error: static declaration of 'x' follows non-static declaration
    static int x;
               ^
    example.c:3:12: note: previous definition is here
    extern int x;
               ^
    1 error generated.
    

    You must be using a pretty permissive compiler – maybe Visual Studio? I just checked on my Windows machine and VS2003 accepts my example program silently. Adding /Wall does give a warning:

    > cl /nologo /Wall example.c
    example.c
    example.c(4) : warning C4211: nonstandard extension used : redefined extern to static
    

    Looks to me like you’re using an extension of whatever compiler it is that you’re using.

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

Sidebar

Related Questions

I came across some legacy code that contains a function like this: LPCTSTR returnString()
I came across some code written in C that looks like this: if (file
Today at work we came across the following code (some of you might recognize
Whilst refactoring some code I came across some getter methods that returns a std::string.
A while ago, I came across some code that marked a data member of
An interesting issue came up recently. We came across some code that is using
I came across this line in some code and can't find the syntax defined
I came across some code recently that used a custom error handler to turn
i have been going through some code and came across a statement that somehow
I was playing around with some code today, and I came across the idea

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.