Why does defining a typedef with the same name as a function give an error when the typedef is global but not local?
When I compile the first code I don't get any error
#include <stdio.h>
void bool_t(void)
{
printf("This is a test\n ");
}
int main()
{
typedef enum bool_t
{
false=0,true=1
} bool_t;
bool_t x = true;
return 0;
}
But when I compile the second code, I get the error
#include <stdio.h>
void bool_t(void)
{
printf("la valeur est ");
}
typedef enum bool_t
{
false=0,true=1
} bool_t;
int main()
{
bool_t x = true;
return 0;
}
Mistake
error: ‘bool_t’ redeclared as different kind of symbol
I would like to really understand what exactly happened in the first and second code in order to better understand the behavior of the two implementations as I consider myself a newbie to C!
source to share
In the first case, it is bool_t
declared inside the function scope, so it masks the declaration from the outside scope. This is useful because it means that adding independent characters to the outer area will not cause your function to malfunction.
In the second case, you are declaring bool_t
twice in the same scope - the global scope. This is not permitted.
source to share
This is due to the resolution of the area they caused. In the first code, you used two of the same name, but in different scopes, so they are treated as different and the local variable is preferred. In the second case, the scope is the same global, which declares two identical named variables with different data types that the compiler did not allow.
source to share