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)
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.
Ben Bacarisse wrote:
fir <fir@grunge.pl> writes:i want it in classic c
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.
- my example work on up to 4 lellets/digits/signs
but i dont know how to make it work for 8
as even old c has 64 bit unsigned i guess it should work, but dont know how to make it work
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");
Am 29.08.2024 um 14:25 schrieb fir:
if(tag=='very old york') printf("\nvery old york");
Isn't standard and I don't know what your compiler does with this
since the string is beyond eight bytes. Maybe it makes a unsigned
__int128 from that.
fir <fir@grunge.pl> writes:
Ben Bacarisse wrote:
fir <fir@grunge.pl> writes:i want it in classic c
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.
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.
On 2024-08-29, fir <fir@grunge.pl> wrote:
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");
#define EIGHTCC(A, B, C, D, E, F, G, H) \
((uint64_t) (A) << 56 | \
(uint64_t) (B) << 48 | \
(uint64_t) (C) << 40 | \
(uint64_t) (D) << 32 | \
(uint64_t) (E) << 24 | \
(uint64_t) (F) << 16 | \
(uint64_t) (G) << 8 | \
(uint64_t) (H))
#define WARSAW EIGHTCC(' ', 'w', 'a', 'r', 's', 'a', 'w')
#define PARIS EIGHTCC(' ', ' ', 'p', 'a', 'r', 'i', 's')
if(tag=='very old york') printf("\nvery old york");
This one won't fit; it has 13 characters. The EIGHTCC macro
requires exactly eight arguments, so you won't define anything
like this by accident.
The binary code coul use an abbreviation, while the constant
symbol has a longer name:
#define VERY_OLD_YORK EIGHTCC('v', 'o', 'l', 'd', 'y', 'r', 'k')
If you care about easily reading these strings in memory dumps, you can adjust the calculation in EIGHTCC according to the endianness of the
machine.
I have it the wrong way around for the predominant little endian;
we want to go from H to A.
...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");
- my example work on up to 4 lellets/digits/signs
but i dont know how to make it work for 8
as even old c has 64 bit unsigned i guess it should work, but dont know how to make it work
fir <fir@grunge.pl> writes:
...
...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");
- my example work on up to 4 lellets/digits/signs
but i dont know how to make it work for 8
"An integer character constant has type int. ... The
value of an integer character constant containing more than one
character, ... is implementation-defined."
The implementation you're using apparently defines a different value for
each possible combination of 4 characters, which is the maximum number allowed if sizeof(int)==4. Keep in mind that, since the mapping is implementation-defined, such code is non-portable. For instance, it's entirely possible that an implementation sets the value of a
multi-character constant solely by looking at it's first or last
characters. On some implementation, you might have 'new york' == 'k' and
'old york' == 'k'.
as even old c has 64 bit unsigned i guess it should work, but dont know how >> to make it work
So, by "old c" you mean C99? Before C99, no integer type was guaranteed
to have more than 32 bits.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 146:40:32 |
Calls: | 10,383 |
Calls today: | 8 |
Files: | 14,054 |
D/L today: |
2 files (1,861K bytes) |
Messages: | 6,417,712 |