Deutsch   English   Français   Italiano  
<vrgvue$35029$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: int a = a (Was: Bart's Language)
Date: Thu, 20 Mar 2025 11:59:10 +0000
Organization: A noiseless patient Spider
Lines: 91
Message-ID: <vrgvue$35029$1@dont-email.me>
References: <vracit$178ka$1@dont-email.me> <vrc2d5$1jjrf$1@paganini.bofh.team>
 <vrc4eb$2p28t$1@dont-email.me> <vrc75b$2r4lt$1@dont-email.me>
 <vrccjb$b3m6$1@news.xmission.com> <vrcef2$33076$1@dont-email.me>
 <vrelvn$12ddq$1@dont-email.me> <aqCCP.590465$SZca.348513@fx13.iad>
 <vrfd0i$1n2ke$1@dont-email.me> <vrgk82$2qpu6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 20 Mar 2025 12:59:11 +0100 (CET)
Injection-Info: dont-email.me; posting-host="b23726e8530270bdb8279be50ba9c02e";
	logging-data="3309641"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+1b/7kf8d4oV8NuZFKUVOH"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:9l6apInYUrFc0rrBulgNjSFlJ7s=
In-Reply-To: <vrgk82$2qpu6$1@dont-email.me>
Content-Language: en-GB
Bytes: 4161

On 20/03/2025 08:39, David Brown wrote:
> 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".

So the inner 'a' wasn't supposed to mysteriously inherit the outer a's 
value? (Perhaps due to sharing the same location.) Since the compilers I 
tried just showed zero not 666.

(Since this thread's subject was an alternative language, in that one it 
can be done like this, but with the first 'a' outside the function since 
there are no block scopes; the module is called 'test':

     int a = 666

     proc main =
         int a := test.a
         println a, test.a

The outer 'a' needs that qualifier to access it, in scopes where it 
would be shadowed. Alternatively an alias can be created then that used 
instead:

     int a = 666
     int b @ a

I don't know if any of these zero-run-time overhead methods are possible 
in C. Possibly a union, but that's only if you can define 'a' yourself, 
and is not an import for example. But then you have to permanently use 
the union 'qualifier'.)

> 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

You mean locking the loop index so it can't be modified? On the same 
subject, that other language works like this:

     for a in 0..99 do
         a := 777        # not allowed

Loop variables, if not already defined as variables, are automatically 
as read-only variables.