there's no error or warning for redefining a same global variable

Bug #1650165 reported by realmz
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Invalid
Undecided
Unassigned

Bug Description

there's no error or warning when redefine a global varialbe (int a).

attached the test project.

# cat main.c

#include "MK64F12.h"

static int i = 0;

int a;
int a;

int main(void)
{

    /* Write your code here */

    /* This for loop should be replaced. By default this loop allows a single stepping. */
    for (;;) {
        i++;
    }
    /* Never leave main */
    return 0;
}

Revision history for this message
realmz (realmz6) wrote :
Revision history for this message
David Brown (davidbrown) wrote :

"int a;" is a "tentative definition You can have as many "tentative definitions" of an identifier as you want in a file. If no full defintion (such as with an initialiser "int a = 1;") or extern declaration is made for the identifier, then the compiler treats it as a definition.

So "int a; int a;" at file scope is just the same as "int a;" - it is fine to declare "a" twice.

But you'll get an error for "int a = 1; int a = 2;" because that would be two conflicting definitions.

If you want, you can use the warning flag "-Wredundant-decls" to give a warning on "int a; int a;" for redundant declarations. Personally, I use that flag - I would not intentionally declare "a" twice like this and so the flag might spot a typo or other mistake in my code.

Revision history for this message
realmz (realmz6) wrote :

Thanks for the explanation. The the warning flag "-Wredundant-decls" works.

Changed in gcc-arm-embedded:
status: New → Confirmed
status: Confirmed → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.