Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #387991

Re: how to make it work

From Ben Bacarisse <ben@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: how to make it work
Date 2024-08-29 16:09 +0100
Organization A noiseless patient Spider
Message-ID <877cbzwelc.fsf@bsb.me.uk> (permalink)
References <877194bd8a95b9acb03db317ee000e94cce2834d@i2pn2.org> <87cylrwkdf.fsf@bsb.me.uk> <be85fd1fb842b5dc7851d36d535869973cc325cf@i2pn2.org>

Show all headers | View raw


fir <fir@grunge.pl> writes:

> Ben Bacarisse wrote:
>> fir <fir@grunge.pl> writes:
>>
>>> see such code
>>>
>>>
>>> long long unsigned tag ;
>>>
>>> void foo(long long unsigned tag)
>>> {
>>>   if(tag=='warsaw') printf("\nwarsaw");
>>>   if(tag=='paris')  printf("\nparis");
>>>   if(tag=='new york') printf("\nnew york");
>>>   if(tag=='old york') printf("\nold york");
>>>   if(tag=='very old york') printf("\nvery old york");
>>>
>>>
>>> }
>>>
>>> int main(void)
>>> {
>>>   foo('warsaw');
>>>   foo('paris');
>>>   foo('new york');
>>>   foo('old york');
>>>   foo('very old york');
>>>
>>>   return 'bye';
>>> }
>>>
>>> and maybe guess the result (or how it should be)
>>> (later i may tell you)
>>>
>>> the problem is how to make it work i want to use that kind of
>>> 'tags' and would like to use it assuming at least 8 characters
>>> work okay i mean the code above would "catch" only on proper tag and
>>> each one would be printed one time
>>>
>>> right now it dont - is thsi a way to make it work
>>> (maybe except the last one as i understand
>>>   if(tag=='old york') printf("\nold york");
>>>   if(tag=='very old york') printf("\nvery old york");
>>> may be treated as the same
>>>
>>> (i need it to work on 32 bit old mingw/gcc)
>>
>> That's unfortunate because this can be done in a portable and relatively
>> convenient way in modern C:
>>
>> #include <stdint.h>
>> #include <stdio.h>
>>
>> typedef union {
>>       unsigned char bytes[sizeof (uint64_t)];
>>       uint64_t tag;
>> } Tag;
>>
>> void foo(Tag t)
>> {
>>       if (t.tag == (Tag){"warsaw"}.tag)
>>            printf("warsaw\n");
>> }
>>
>> int main(void)
>> {
>>       foo((Tag){"warsaw"});
>> }
>>
>> With a little bit more work, you can get this to work in older C without
>> compound literals.
>>
>> But depending on what your ultimate goal is, you might want to look at
>> how Lisp implementations "intern" their symbols.  That can avoid the
>> obvious length limitations while making for very efficient equality
>> comparisons.
>>
> i want it in classic c

Why?  Compound literals are a quarter of a century old.

> - my example work on up to 4 lellets/digits/signs
> but i dont know how to make it work for 8

"Classic" C's character constants are of type int and classic C's ints
are 16 or 32 bits wide.  There is no getting round that.

A macro like this (with your own choice of old type in the casts)

#define TAG(s) ((uint64_t)(s[0])                       | \
                (uint64_t)(s"\0"[1])             <<  8 | \
                (uint64_t)(s"\0\0"[2])           << 16 | \
                (uint64_t)(s"\0\0\0"[3])         << 24 | \
                (uint64_t)(s"\0\0\0\0"[4])       << 32 | \
                (uint64_t)(s"\0\0\0\0\0"[5])     << 40 | \
                (uint64_t)(s"\0\0\0\0\0\0"[6])   << 48 | \
                (uint64_t)(s"\0\0\0\0\0\0\0"[7]) << 56)

might just about do, but you can't get 64-bit integers using '...'
character constants.

> as even old c has 64 bit unsigned i guess it should work, but dont know how
> to make it work

Some (but not all) old C compilers support 64 bit integer types, but
'...' constants are always of type int.

-- 
Ben.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

how to make it work fir <fir@grunge.pl> - 2024-08-29 14:25 +0200
  Re: how to make it work Ben Bacarisse <ben@bsb.me.uk> - 2024-08-29 14:04 +0100
    Re: how to make it work fir <fir@grunge.pl> - 2024-08-29 15:13 +0200
      Re: how to make it work Ben Bacarisse <ben@bsb.me.uk> - 2024-08-29 16:09 +0100
        Re: how to make it work fir <fir@grunge.pl> - 2024-08-29 18:14 +0200
      Re: how to make it work James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-29 14:16 -0400
        Re: how to make it work fir <fir@grunge.pl> - 2024-08-29 20:24 +0200
  Re: how to make it work Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-29 15:22 +0000
    Re: how to make it work fir <fir@grunge.pl> - 2024-08-29 18:24 +0200
  Re: how to make it work Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-29 17:54 +0200
    Re: how to make it work fir <fir@grunge.pl> - 2024-08-29 18:12 +0200
      Re: how to make it work Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-31 20:38 +0200

csiph-web