Sujet : Re: int a = a (Was: Bart's Language)
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 18. Mar 2025, 19:36:16
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrcef2$33076$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 18.03.2025 19:04, Kenny McCormack wrote:
In article <vrc75b$2r4lt$1@dont-email.me>,
David Brown <david.brown@hesbynett.no> wrote:
...
gcc won't warn until you say '-Wextra', and then only for:
>
int a = a + 1;
>
People would not normally write "int a = a;". It is used as a common
idiom meaning "I know it is not clear to the compiler that the variable
is always initialised before use, but /I/ know it is - so disable the
use-without-initialisation warnings for this variable".
Wow! - It would never have occurred to me that "int a = a;" being
considered an idiom, let alone a "common idiom".
I certainly never saw it in any C code nor have seen it mentioned
as idiomatic code [to prevent compiler warning messages]. - This is
IMO just sick; in all respects. Compilers not warning about it (you
may have just missed to complete the RHS expression), programmers
working around a compiler deficiency by code that just rises more
questions (than solving any issue).
So it makes perfect sense for the compiler not to warn about it!
(Obviously mileages on that vary.)
Wouldn't it just be easier and clearer to write: int a = 0;
and be done with it?
Indeed! - More generally, there's a [general, non-language specific]
coding _idiom_ to ("always", sort of) initialize your variables, and
this is certainly part of many coding guidelines and recommendations
I've seen.
"int a = a + 1;", on the other hand, clearly attempts to read the value
of "a" before it is initialised, and a warning is issued if
"-Wuninitialized" is enabled. This warning is part of "-Wall".
How is: int a = a + 1;
conceptually different from: int a = a;
Both are expressions involving 'a'.
Isn't 'a' being used un-initialised in both cases?
(You have to know the value of 'a' in order to evaluate the expression: a)
Yes. (Unless it's really an "idiom"; then we can justify every idea
we like by that classification.)
Janis