Sujet : Re: int a = a (Was: Bart's Language)
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 20. Mar 2025, 09:39:30
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrgk82$2qpu6$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 19/03/2025 22:29, Chris M. Thomasson wrote:
The scares me a bit:
_____________________
#include <stdio.h>
int main() {
int a = 666;
{
// Humm... Odd to me...
int a = a;
printf("%d", a);
}
return 0;
}
Yes, that is an unfortunate consequence of the scoping in C - on the line "int a = a;", the new "a" is initialised with its own unspecified value, not with the value of the outer "a".
If the scope of the new variable did not start until after the initialisation, then it would have allowed a number of possibilities that would be a lot more useful than the "disable warnings about uninitialised variables" effect or initialisers which refer to their own address. For example :
int a = 123;
{
int a = a;
// local copy that does not affect the outer one
...
int a = 123;
{
long long int a = a;
// Local copy that with expanded size
...
for (int a = 0; a < 100; a++) {
const int a = a;
// "Lock" the loop index to catch errors
As it is, you need to do something like :
for (int a = 0; a < 100; a++) {
const int _a = a;
const int a = _a;
// "Lock" the loop index to catch errors