Question: Is it me, or do both GCC and Clang have not quite correct error messages when assessing particular global char declarations in C?
---And a particular note regarding a similar question is that I'm looking for clarification regarding why the char declaration is getting this reaction. There are related questions, yes, but all I saw there was int declarations.
$ gcc --version gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
$ clang --version clang version 10.0.0-4ubuntu1
Consider the following C code, able.c:
#include <stdio.h>
char able;
able = 'X';
int main(void)
{
printf("%c", able);
}
A first note is that yes, combining the declaration and the initialization of able is much more efficient. However, when run through GCC and Clang, the error messages that turn up seem to me to be, basically, incorrect messages:
$ clang -Weverything able.c
able.c:5:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
able = 'X';
^
able.c:5:1: error: redefinition of 'able' with a different type: 'int' vs 'char'
able.c:3:6: note: previous definition is here
char able;
^
able.c:3:6: warning: no previous extern declaration for non-static variable 'able' [-Wmissing-variable-declarations]
char able;
^
able.c:3:1: note: declare 'static' if the variable is not intended to be used outside of this translation unit
char able;
^
2 warnings and 1 error generated.
$ gcc -Wall -Wextra -Wpedantic able.c
able.c:5:1: warning: data definition has no type or storage class
5 | able = 'X';
| ^~~~
able.c:5:1: warning: type defaults to ‘int’ in declaration of ‘able’ [-Wimplicit-int]
able.c:5:1: error: conflicting types for ‘able’
able.c:3:6: note: previous declaration of ‘able’ was here
3 | char able;
| ^~~~
Both sets of messages complain about a missing type specifier, except that the type specifier---char---is indeed right there. When the declaration and initialization messages are combined in that spot, above/before the main function, the program compiles. When the pair of messages are placed in the main function, even without being combined, the program also compiles.
So the char able; statement is perfectly fine, so why those error messages?