7
What is the difference between a global variable and a static global variable? Example:
#include <stdio.h>
int numero = 5;
static int static_numero = 5;
int main(void)
{
printf("numero: %d \n", numero);
printf("static_numero: %d \n", static_numero);
return 0;
}
Is there a difference between the two variables? If there is no difference then what would be the reason a global variable cannot be initialized in a file .h
when two files .c
do the include
?
/* Isso em um arquivo .h */
int a = 5; // Da erro
static int b = 5; // Nao da erro
/* Arquivo .c 1*/
#include "arquivo.h"
/* Arquivo .c 2 */
#include "arquivo.h"
The static
of the above code for some reason is mandatory when two files .c
do the include
, but it wouldn’t be redundant to use the static
since the lifetime of these variables will not change?