Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extern declaration and global scope

I'm wondering why is this code compiles and run. I thought that if a variable is declared as static (in global scope) it will be accessible only within the file it is declared.

functions.h

static int x = 10;

main.c

#include <stdio.h>
#include "functions.h"

extern int x;

int main()
{
   printf("%d", x);
}
like image 975
DRON Avatar asked Apr 01 '26 03:04

DRON


1 Answers

Technically, it is indeed declared within the main.c, as this includes the functions.h. If it was a sparate compilation module, you'd be right.

But I'd have suspected that within the same compilation unit extern and staticwould collide with each other. At least it would be worth a warning.

like image 153
glglgl Avatar answered Apr 02 '26 20:04

glglgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!