Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dice generator problems

11 views
Skip to first unread message

Bill Cunningham

unread,
Dec 9, 2009, 5:48:25 PM12/9/09
to
I had one error in this code until I tried to use error checking with
strtol. Now I've really goofed it. This is the compilation I used.

gcc -g di.c -o a -ansi -pedantic -Wall

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
if (argc > 4 || argc == 0) {
fputs("Dice generator usage error\n", stderr);
exit(EXIT_FAILURE);
}
int x, y;
if (argv[1][0] == '-' && argv[1][1] == 'a') {
if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
fputs("strtol error\n", stderr);
return 1;
}
if ((y = strtol(argv[2], NULL, 10)) == LONG_MIN || LONG_MAX) {
fputs("strtol error\n", stderr);
return 1;
}
printf("%i\n", x + y);
exit(1);
}
srand(time(NULL));
printf("%i\n", rand(void) %argv[1]);
return 0;
}

di.c: In function `main':
di.c:11: warning: ISO C90 forbids mixed declarations and code
di.c:13: error: `LONG_MIN' undeclared (first use in this function)
di.c:13: error: (Each undeclared identifier is reported only once
di.c:13: error: for each function it appears in.)
di.c:13: error: `LONG_MAX' undeclared (first use in this function)
di.c:25: error: syntax error before "void"

Are those return values for strtol right? That must be my biggest
problem. I checked the man page for strtol.

Bill

--

Casts are seldom if ever needed in C.

--Richard Heathfield


Bill Cunningham

unread,
Dec 9, 2009, 6:00:37 PM12/9/09
to

"Bill Cunningham" <nos...@nspam.invalid> wrote in message
news:4b202935$0$5348$bbae...@news.suddenlink.net...
Wait a minute I didn't balance those parenthesis. Well I think this code
has alot more wrong with it than that.

Seebs

unread,
Dec 9, 2009, 6:04:26 PM12/9/09
to
On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

Uh.

x == 1 || 2

is not a test for whether x is equal to either 1 or 2.

> di.c:13: error: `LONG_MIN' undeclared (first use in this function)

Maybe LONG_MIN is declared in a specific header?

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Bill Cunningham

unread,
Dec 9, 2009, 6:13:10 PM12/9/09
to

"Seebs" <usenet...@seebs.net> wrote in message
news:slrnhi0b84.72o...@guild.seebs.net...

> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>
> Uh.
>
> x == 1 || 2
>
> is not a test for whether x is equal to either 1 or 2.

I don't quite understand here what you are trying to say.

>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
>
> Maybe LONG_MIN is declared in a specific header?

stdlib.h

Seebs

unread,
Dec 9, 2009, 6:40:32 PM12/9/09
to
On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
>
> "Seebs" <usenet...@seebs.net> wrote in message
> news:slrnhi0b84.72o...@guild.seebs.net...
>> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

>> Uh.

>> x == 1 || 2

>> is not a test for whether x is equal to either 1 or 2.

> I don't quite understand here what you are trying to say.

I'm saying something really, really, simple. In English.

You have written:


if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

Let's simplify this a bit:
x = strtol(argv[1], NULL, 10);
if (x == LONG_MIN || LONG_MAX) { ...

Now, there are two possibilities.

One is that you think this will tell you whether x is either of LONG_MIN
or LONG_MAX.

The other is that you want to test whether x is 1.

The thing is: You have written something that might be read out loud
as "if x is equal to long_min or long_max". And that suggests, given
your general history, that you have somehow decided that this is a
good way to test whether x has either of these two values.

However, in C, the expression "LONG_MIN || LONG_MAX" turns out to have
the value 1, because at least one of them has a non-zero value. So what
you've actually done is "if x is equal to the boolean value of the
expression long_min or long_max, which is one". So it's equivalent to
"if (x == 1)".

>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)

>> Maybe LONG_MIN is declared in a specific header?

> stdlib.h

#include <stdlib.h>
int main(void) {
int x = LONG_MIN;
}

gcc says:
t.c: In function "main":
t.c:3: error: "LONG_MIN" undeclared (first use in this function)
t.c:3: error: (Each undeclared identifier is reported only once
t.c:3: error: for each function it appears in.)

So I guess no, it must be some other header. Man, sure is a shame it's
impossible to open the documentation and look, or we'd have this one nailed.

Keith Thompson

unread,
Dec 9, 2009, 6:44:13 PM12/9/09
to
"Bill Cunningham" <nos...@nspam.invalid> writes:
> "Seebs" <usenet...@seebs.net> wrote in message
> news:slrnhi0b84.72o...@guild.seebs.net...
>> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>>
>> Uh.
>>
>> x == 1 || 2
>>
>> is not a test for whether x is equal to either 1 or 2.
>
> I don't quite understand here what you are trying to say.

Read it again until you do.

>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
>>
>> Maybe LONG_MIN is declared in a specific header?
>
> stdlib.h

Don't guess.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham

unread,
Dec 9, 2009, 6:53:05 PM12/9/09
to

"Seebs" <usenet...@seebs.net> wrote in message
news:slrnhi0dbp.q1j...@guild.seebs.net...

OK. It's been awhile since I tried error checking What I want if if
(x=y) I need the comparison equals and not the assignment operator.

>>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
>
>>> Maybe LONG_MIN is declared in a specific header?
>
>> stdlib.h
>
> #include <stdlib.h>
> int main(void) {
> int x = LONG_MIN;
> }
>
> gcc says:
> t.c: In function "main":
> t.c:3: error: "LONG_MIN" undeclared (first use in this function)
> t.c:3: error: (Each undeclared identifier is reported only once
> t.c:3: error: for each function it appears in.)
>
> So I guess no, it must be some other header. Man, sure is a shame it's
> impossible to open the documentation and look, or we'd have this one
> nailed.

I got that from man 3 strtol. It said that strtol was defined in
stdlib.h. It didn't say that LONG_MAX and LONG_MIN were declared else where
but as you have pointed out the compiler says they are somewhere else.

Bill Cunningham

unread,
Dec 9, 2009, 6:55:12 PM12/9/09
to

"Keith Thompson" <ks...@mib.org> wrote in message
news:ln7hsvr...@nuthaus.mib.org...

> "Bill Cunningham" <nos...@nspam.invalid> writes:
>> "Seebs" <usenet...@seebs.net> wrote in message
>> news:slrnhi0b84.72o...@guild.seebs.net...
>>> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
>>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>>>
>>> Uh.
>>>
>>> x == 1 || 2
>>>
>>> is not a test for whether x is equal to either 1 or 2.
>>
>> I don't quite understand here what you are trying to say.
>
> Read it again until you do.

I should have used = instead of ==

>>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
>>>
>>> Maybe LONG_MIN is declared in a specific header?
>>
>> stdlib.h
>
> Don't guess.

man 3 strtol said strtol was declared in stdlib.h. But the compiler says
otherwise.

Ben Bacarisse

unread,
Dec 9, 2009, 7:06:11 PM12/9/09
to
"Bill Cunningham" <nos...@nspam.invalid> writes:

> I had one error in this code until I tried to use error checking with
> strtol. Now I've really goofed it. This is the compilation I used.
>
> gcc -g di.c -o a -ansi -pedantic -Wall
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
> if (argc > 4 || argc == 0) {
> fputs("Dice generator usage error\n", stderr);
> exit(EXIT_FAILURE);
> }
> int x, y;

Why int when you set the using strtol? Surely you mean long here, no?

> if (argv[1][0] == '-' && argv[1][1] == 'a') {
> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

This is wrong but it not (currently) a problem because errors prevent
compilation. You want x == LONG MAX at the end, not just LONG_MAX.
LONG_MAX on its own will be taken as true so the whole || will always
be true.

> fputs("strtol error\n", stderr);
> return 1;
> }
> if ((y = strtol(argv[2], NULL, 10)) == LONG_MIN || LONG_MAX) {
> fputs("strtol error\n", stderr);
> return 1;
> }
> printf("%i\n", x + y);
> exit(1);
> }
> srand(time(NULL));
> printf("%i\n", rand(void) %argv[1]);

One error message is telling you that rand(void) is wrong. That is
not ho you call a function that takes no arguments. Once you fix
that, there will be a complaint about % argv[1] as well. argv[1] is
of type char * but % requires integer operands.

> return 0;
> }
>
> di.c: In function `main':
> di.c:11: warning: ISO C90 forbids mixed declarations and code
> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
> di.c:13: error: (Each undeclared identifier is reported only once
> di.c:13: error: for each function it appears in.)
> di.c:13: error: `LONG_MAX' undeclared (first use in this function)
> di.c:25: error: syntax error before "void"
>
> Are those return values for strtol right? That must be my biggest
> problem. I checked the man page for strtol.

There are problems with the return values you use (not least trying to
put a long int into and int) but the basic problem is exactly what the
error messages say: you are using names that are not declared.
LONG_MIN and LONG_MAX are defined in limits.h.

--
Ben.

Keith Thompson

unread,
Dec 9, 2009, 7:10:05 PM12/9/09
to
Seebs <usenet...@seebs.net> writes:
[...]

> Let's simplify this a bit:
> x = strtol(argv[1], NULL, 10);
> if (x == LONG_MIN || LONG_MAX) { ...
[...]

> However, in C, the expression "LONG_MIN || LONG_MAX" turns out to have
> the value 1, because at least one of them has a non-zero value. So what
> you've actually done is "if x is equal to the boolean value of the
> expression long_min or long_max, which is one". So it's equivalent to
> "if (x == 1)".
[...]

Not quite. Remember that "==" binds more tightly than "||".
(x == (LONG_MIN || LONG_MAX))
has the meaning you describe, but
(x == LONG_MIN || LONG_MAX)
is equivalent to
((x == LONG_MIN) || LONG_MAX)
which evaluates to 1, regardless of the value of x (assuming, of
course, that LONG_MIN and LONG_MAX are declared by #including the
proper header).

Of course this doesn't change the basic point, which is that


if (x == LONG_MIN || LONG_MAX)

is the wrong way to test whether x is equal either to LONG_MIN or to
LONG_MAX.

Ben Pfaff

unread,
Dec 9, 2009, 7:15:39 PM12/9/09
to
Ben Bacarisse <ben.u...@bsb.me.uk> writes:

> "Bill Cunningham" <nos...@nspam.invalid> writes:
>
>> if (argv[1][0] == '-' && argv[1][1] == 'a') {
>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>
> This is wrong but it not (currently) a problem because errors prevent
> compilation. You want x == LONG MAX at the end, not just LONG_MAX.

"x == LONG MAX"?
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman

Keith Thompson

unread,
Dec 9, 2009, 7:14:51 PM12/9/09
to
"Bill Cunningham" <nos...@nspam.invalid> writes:
> "Seebs" <usenet...@seebs.net> wrote in message
> news:slrnhi0dbp.q1j...@guild.seebs.net...
[...]

>> However, in C, the expression "LONG_MIN || LONG_MAX" turns out to have
>> the value 1, because at least one of them has a non-zero value. So what
>> you've actually done is "if x is equal to the boolean value of the
>> expression long_min or long_max, which is one". So it's equivalent to
>> "if (x == 1)".

The above isn't quite correct, but for reasons that aren't relevant to
what you're trying to do.

> OK. It's been awhile since I tried error checking What I want if if
> (x=y) I need the comparison equals and not the assignment operator.

You have completely missed the point. You were already using the "=="
comparison operator; there wasn't an assignment operator in the
condition.

If you want to know whether x is either equal to LONG_MIN or equal to
LONG_MAX, you need to write

if (x == LONG_MIN || x == LONG_MAX)

Got it?

[...]


>> So I guess no, it must be some other header. Man, sure is a shame it's
>> impossible to open the documentation and look, or we'd have this one
>> nailed.
>
> I got that from man 3 strtol. It said that strtol was defined in
> stdlib.h. It didn't say that LONG_MAX and LONG_MIN were declared else where
> but as you have pointed out the compiler says they are somewhere else.

So find out where they're declared and add a #include directive for
that header. (Yes, I know which header it is. No, I'm not going to
tell you.)

RTFM.

Keith Thompson

unread,
Dec 9, 2009, 7:15:47 PM12/9/09
to
"Bill Cunningham" <nos...@nspam.invalid> writes:
> "Keith Thompson" <ks...@mib.org> wrote in message
> news:ln7hsvr...@nuthaus.mib.org...
>> "Bill Cunningham" <nos...@nspam.invalid> writes:
>>> "Seebs" <usenet...@seebs.net> wrote in message
>>> news:slrnhi0b84.72o...@guild.seebs.net...
>>>> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
>>>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>>>>
>>>> Uh.
>>>>
>>>> x == 1 || 2
>>>>
>>>> is not a test for whether x is equal to either 1 or 2.
>>>
>>> I don't quite understand here what you are trying to say.
>>
>> Read it again until you do.
>
> I should have used = instead of ==

Wrong. Don't guess.

>>>>> di.c:13: error: `LONG_MIN' undeclared (first use in this function)
>>>>
>>>> Maybe LONG_MIN is declared in a specific header?
>>>
>>> stdlib.h
>>
>> Don't guess.
>
> man 3 strtol said strtol was declared in stdlib.h. But the compiler says
> otherwise.

Wrong again. strtol is declared in stdlib.h. We're talking about
where LONG_MIN is declared.

Look. It. Up.

Peter Nilsson

unread,
Dec 9, 2009, 7:34:31 PM12/9/09
to
On Dec 10, 10:40 am, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
> > [snip]

> You have written:
>          if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN ||
> LONG_MAX) {
>
> Let's simplify this a bit:
>         x = strtol(argv[1], NULL, 10);
>         if (x == LONG_MIN || LONG_MAX) { ...
>
> The thing is:  You have written something that might be
> read out loud as "if x is equal to long_min or long_max".
> ...

> However, in C, the expression "LONG_MIN || LONG_MAX" turns
> out to have the value 1

Quite so, but the condition is parsed as...

(x == LONG_MIN) || LONG_MAX

...not...

x == (LONG_MIN || LONG_MAX)

--
Peter

Gordon Burditt

unread,
Dec 9, 2009, 7:50:07 PM12/9/09
to
> I had one error in this code until I tried to use error checking with
>strtol. Now I've really goofed it. This is the compilation I used.
>
>gcc -g di.c -o a -ansi -pedantic -Wall
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <time.h>
>
>int main(int argc, char *argv[])
>{
> if (argc > 4 || argc == 0) {
> fputs("Dice generator usage error\n", stderr);

It is conventional to explain how to use the program, not just say
that you blew it. In this case you might explain that the arguments
are how many dice to throw, how many sides they have, and which
ones are loaded.

> exit(EXIT_FAILURE);
> }
> int x, y;
> if (argv[1][0] == '-' && argv[1][1] == 'a') {
> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

If you get to the point of strtol(), you are passing it the value
of argv[1], which is a string that starts with "-a", as determined
by the line above it. Is this really what you want?
what is strtol("-a105", NULL, 10) supposed to return?

LONG_MIN and LONG_MAX are declared in a standard header other than
the ones you have included above.

> fputs("strtol error\n", stderr);

Don't you think it would be appropriate to include the invalid input
in the error message, or at least explain which argument had the
error?

> return 1;
Why are you sometimes "return"ing from main(), and sometimes
"exit()"ing? Wouldn't your program be more understandable if you
were consistent about this?

> }
> if ((y = strtol(argv[2], NULL, 10)) == LONG_MIN || LONG_MAX) {

This is not how you compare the result of a function to two different
values.

> fputs("strtol error\n", stderr);
> return 1;
> }
> printf("%i\n", x + y);
> exit(1);

You used EXIT_FAILURE above. Don't you think it would be appropriate
to use it here, also?


> }
> srand(time(NULL));
> printf("%i\n", rand(void) %argv[1]);
> return 0;
>}
>
>di.c: In function `main':
>di.c:11: warning: ISO C90 forbids mixed declarations and code

Where do you think the declaration "int x,y;" should go in C90?

>di.c:13: error: `LONG_MIN' undeclared (first use in this function)
>di.c:13: error: (Each undeclared identifier is reported only once
>di.c:13: error: for each function it appears in.)
>di.c:13: error: `LONG_MAX' undeclared (first use in this function)
>di.c:25: error: syntax error before "void"
>
> Are those return values for strtol right? That must be my biggest

LONG_MIN and LONG_MAX are declared in a standard header that ISN'T
one of the ones you included above. How many standard headers does
that leave to try? Do you think it might help to read the manual?

>problem. I checked the man page for strtol.

The manual page for strtol() probably does not mention the header
where LONG_MIN and LONG_MAX are declared.

Bill Cunningham

unread,
Dec 9, 2009, 7:54:37 PM12/9/09
to

"Gordon Burditt" <gordon...@burditt.org> wrote in message
news:ZumdnXcY5ewi2L3W...@posted.internetamerica...

> The manual page for strtol() probably does not mention the header
> where LONG_MIN and LONG_MAX are declared.

It sounds like that's not my major problem here I thought strtol was
equal to atoi if the 3rd parameter was passed a base 10 number. Hence 10 in
the 3rd parameter. Maybe I'm wrong. The man page I read said the return
value on failure would be offerflow or underflow hence LONG_MAX and
LONG_MIN.

Gordon Burditt

unread,
Dec 9, 2009, 7:58:01 PM12/9/09
to
>> The manual page for strtol() probably does not mention the header
>> where LONG_MIN and LONG_MAX are declared.
>
> It sounds like that's not my major problem here I thought strtol was
>equal to atoi if the 3rd parameter was passed a base 10 number. Hence 10 in
>the 3rd parameter. Maybe I'm wrong.

Try reading the manual and find out.

>The man page I read said the return
>value on failure would be offerflow or underflow hence LONG_MAX and
>LONG_MIN.

If you're going to use LONG_MIN and LONG_MAX, you need to include
the standard header which declares them. Fix it.

Kenny McCormack

unread,
Dec 9, 2009, 7:58:51 PM12/9/09
to
In article <lnzl5rp...@nuthaus.mib.org>,
Keith Thompson <ks...@mib.org> wrote:
...

>Of course this doesn't change the basic point, which is that
> if (x == LONG_MIN || LONG_MAX)
>is the wrong way to test whether x is equal either to LONG_MIN or to
>LONG_MAX.

Of course. Everyone knows that the right way is:

if ((x-LONG_MIN)*(x-LONG_MAX) == 0)

Seebs

unread,
Dec 9, 2009, 8:20:07 PM12/9/09
to
On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
> OK. It's been awhile since I tried error checking What I want if if
> (x=y) I need the comparison equals and not the assignment operator.

You need to slow down, and possibly stop drinking while posting.

None of this has anything to do with the distinction between comparison
and assignment.

I really don't understand why you're still doing this. If you can't
understand the explanation you were just given, there is no *point*.
Do you have alexia or something?

> I got that from man 3 strtol. It said that strtol was defined in
> stdlib.h.

But that's not the question.

> It didn't say that LONG_MAX and LONG_MIN were declared else where
> but as you have pointed out the compiler says they are somewhere else.

Right. Which is documented, too, if you would read the standard, or a C
book, or pretty much anything else.

Bill Cunningham

unread,
Dec 9, 2009, 8:21:12 PM12/9/09
to

"Gordon Burditt" <gor...@hammy.burditt.org> wrote in message
news:ZumdnXYY5ewE2r3W...@posted.internetamerica...

Well from what I make of this
http://www.manpagez.com/man/3/strtol/

That header limits.h declares strtol. My man page on linux said it was
dclared in stdlib.h. The two return values on failure are LONG_MAX and
LONG_MIN.

"The strtol(), strtoll(), strtoimax(), and strtoq() functions return the
result of the conversion, unless the value would underflow or overflow.
If no conversion could be performed, 0 is returned and the global vari-
able errno is set to EINVAL (the last feature is not portable across
all
platforms). If an overflow or underflow occurs, errno is set to ERANGE
and the function return value is clamped according to the following ta-
ble."

Seebs

unread,
Dec 9, 2009, 8:20:56 PM12/9/09
to
On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
> I should have used = instead of ==

No.

And while I can't say whether you are genuinely incapable of reading
simple English, or simply not bothered to try, I give up.

*plonk*

Keith Thompson

unread,
Dec 9, 2009, 8:34:36 PM12/9/09
to
"Bill Cunningham" <nos...@nspam.invalid> writes:
> "Gordon Burditt" <gordon...@burditt.org> wrote in message
> news:ZumdnXcY5ewi2L3W...@posted.internetamerica...
>
>> The manual page for strtol() probably does not mention the header
>> where LONG_MIN and LONG_MAX are declared.
>
> It sounds like that's not my major problem here I thought strtol was
> equal to atoi if the 3rd parameter was passed a base 10 number. Hence 10 in
> the 3rd parameter. Maybe I'm wrong. The man page I read said the return
> value on failure would be offerflow or underflow hence LONG_MAX and
> LONG_MIN.

Your major problem at the moment is that your code doesn't compile.

Your code will not compile until you add a #include directive for the
header that declares LONG_MIN and LONG_MAX.

Note that your code still won't compile even after you've fixed that
problem, because it has other problems as well. You can try to fix
those later.

But the LONG_MIN and LONG_MAX problem is a very simple one. If you're
not able to fix that given the hints we've already given you, there's
just no point in continuing.

Find out which header declares LONG_MIN and LONG_MAX. (Do not guess.
Look it up.) Add a #include directive for that header. Recompile
your program. You'll still have error messages, but fewer than you
have now; in particular, these error messages:

di.c:13: error: `LONG_MIN' undeclared (first use in this function)
di.c:13: error: (Each undeclared identifier is reported only once
di.c:13: error: for each function it appears in.)
di.c:13: error: `LONG_MAX' undeclared (first use in this function)

will go away.

After you've done that, you can worry about fixing the other errors.

Focus on one thing at a time.

Seebs

unread,
Dec 9, 2009, 8:44:24 PM12/9/09
to
On 2009-12-10, Keith Thompson <ks...@mib.org> wrote:
> Not quite. Remember that "==" binds more tightly than "||".

D'oh!

I even wrote a test program, which did not test what I thought it did.

This is what comes of spending a bunch of time in languages in which "x || y"
is idiomatic for roughly "x ? x : y".

> Of course this doesn't change the basic point, which is that
> if (x == LONG_MIN || LONG_MAX)
> is the wrong way to test whether x is equal either to LONG_MIN or to
> LONG_MAX.

Right.

Interesting trivia point: I have written substantial code in a language
where

if (x == 1 or 2 && y == 3 or 4)

actually checks whether it is the case both that:
1. x has either the value 1 or the value 2
2. y has either the value 3 or the value 4

It's actually pretty handy as syntactic sugar goes.

Gordon Burditt

unread,
Dec 9, 2009, 8:48:07 PM12/9/09
to
>>>> The manual page for strtol() probably does not mention the header
>>>> where LONG_MIN and LONG_MAX are declared.
>>>
>>> It sounds like that's not my major problem here I thought strtol was
>>>equal to atoi if the 3rd parameter was passed a base 10 number. Hence 10
>>>in
>>>the 3rd parameter. Maybe I'm wrong.
>>
>> Try reading the manual and find out.
>>
>>>The man page I read said the return
>>>value on failure would be offerflow or underflow hence LONG_MAX and
>>>LONG_MIN.
>>
>> If you're going to use LONG_MIN and LONG_MAX, you need to include
>> the standard header which declares them. Fix it.
>
> Well from what I make of this
>http://www.manpagez.com/man/3/strtol/
>
>That header limits.h declares strtol.

That is not what that manual page says. It's not true that limits.h
declares strtol. That is also not the question. The question is


where LONG_MIN and LONG_MAX are declared.

>My man page on linux said it was
>dclared in stdlib.h.

What was declared in stdlib.h? Not LONG_MIN and LONG_MAX.

>The two return values on failure are LONG_MAX and
>LONG_MIN.
>
>"The strtol(), strtoll(), strtoimax(), and strtoq() functions return the
> result of the conversion, unless the value would underflow or overflow.
> If no conversion could be performed, 0 is returned and the global vari-
> able errno is set to EINVAL (the last feature is not portable across
>all
> platforms). If an overflow or underflow occurs, errno is set to ERANGE
> and the function return value is clamped according to the following ta-
> ble."

That doesn't say anything about where LONG_MIN and LONG_MAX are declared.

Ben Bacarisse

unread,
Dec 9, 2009, 9:17:34 PM12/9/09
to
Ben Pfaff <b...@cs.stanford.edu> writes:

> Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>
>> "Bill Cunningham" <nos...@nspam.invalid> writes:
>>
>>> if (argv[1][0] == '-' && argv[1][1] == 'a') {
>>> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>>
>> This is wrong but it not (currently) a problem because errors prevent
>> compilation. You want x == LONG MAX at the end, not just LONG_MAX.
>
> "x == LONG MAX"?

saved by the "just", I think :-)

--
Ben.

Keith Thompson

unread,
Dec 9, 2009, 9:22:34 PM12/9/09
to
Seebs <usenet...@seebs.net> writes:
> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
>> OK. It's been awhile since I tried error checking What I want if if
>> (x=y) I need the comparison equals and not the assignment operator.
>
> You need to slow down, and possibly stop drinking while posting.
>
> None of this has anything to do with the distinction between comparison
> and assignment.
>
> I really don't understand why you're still doing this. If you can't
> understand the explanation you were just given, there is no *point*.
> Do you have alexia or something?

He's said he's suffering from the side effects of some medication he's
taking; I don't remember the details.

It's also been suggested that he's a deliberate troll. If so, he's
quite skilled at it. (This does not imply any kind of admiration on
my part; if he's deliberately trolling, it's pathetic, not admirable.)

In any case, he's been trying to learn C programming for many years.

>> I got that from man 3 strtol. It said that strtol was defined in
>> stdlib.h.

No, it didn't.

> But that's not the question.
>
>> It didn't say that LONG_MAX and LONG_MIN were declared else where
>> but as you have pointed out the compiler says they are somewhere else.
>
> Right. Which is documented, too, if you would read the standard, or a C
> book, or pretty much anything else.

To be fair, I just checked the index of K&R2 for LONG_MAX and
LONG_MIN. It points to page 252, in the standard library reference,
which contains the description of strtol. It mentions that strtol
returns LONG_MIN or LONG_MAX on overflow, but it doesn't say where
they're defined.

That index entry should really point to page 257. (Bill, I think you
have a copy of K&R2, so that's a big hint.)

The online man page Bill cited is also a bit misleading. It says:

LEGACY SYNOPSIS

#include <stdlib.h>
#include <limits.h>

<limits.h> is necessary for the strtol() and strtoll() functions.

(Note that if Bill had followed this advice he wouldn't be having this
problem, though he still wouldn't know where strtol, LONG_MIN, and
LONG_MAX are actually declared).

Bill Cunningham

unread,
Dec 9, 2009, 9:43:53 PM12/9/09
to

"Gordon Burditt" <gordon...@burditt.org> wrote in message
news:tZOdnQmwSu_Kzr3W...@posted.internetamerica...

> That doesn't say anything about where LONG_MIN and LONG_MAX are declared.

This does.
http://en.wikipedia.org/wiki/Limits.h

limits.h

Nick Keighley

unread,
Dec 10, 2009, 3:19:43 AM12/10/09
to
On 9 Dec, 23:55, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> "Keith Thompson" <ks...@mib.org> wrote in message
> news:ln7hsvr...@nuthaus.mib.org...
> > "Bill Cunningham" <nos...@nspam.invalid> writes:
> >> "Seebs" <usenet-nos...@seebs.net> wrote in message

> >>news:slrnhi0b84.72o...@guild.seebs.net...
> >>> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:

> >>>>         if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {
>

> >>> x == 1 || 2
>
> >>> is not a test for whether x is equal to either 1 or 2.

Mr Seebach is giving an example of code that is simpler than yours BUT
ALSO WRONG IN THE SAME WAY.


> >>     I don't quite understand here what you are trying to say.

which bit didn't you understand?

x == 1 || 2
does not test if x is either 1 or 2 does it? Write a test program if
you have difficulty understanding.

>     I should have used = instead of ==

what does the operator == do? what does the operator = do? Which one
would you use if you wanted to test if two things were equal.

I think I'd be better off teaching my cat to program in C.

<snip>

Squeamizh

unread,
Dec 10, 2009, 3:59:35 AM12/10/09
to
On Dec 9, 5:44 pm, Seebs <usenet-nos...@seebs.net> wrote:
> Interesting trivia point:  I have written substantial code in a language
> where

Your trivia point would be more interesting if you named the
language. I've seen you and others do this kind of thing quite a bit,
and I have never understood it. "There is at least one platform on
which your code wouldn't run correctly," "There is at least one
popular compiler that doesn't implement feature <x> that way," and so
on. The information might be somewhat interesting, but it's useless
without specifics. You took the time to describe something, but you
left out the most important detail!

Nick Keighley

unread,
Dec 10, 2009, 5:09:52 AM12/10/09
to
On 10 Dec, 08:59, Squeamizh <sque...@hotmail.com> wrote:
> On Dec 9, 5:44 pm, Seebs <usenet-nos...@seebs.net> wrote:

> > Interesting trivia point:  I have written substantial code in a language
> > where
>
> Your trivia point would be more interesting if you named the
> language.  

only slightly. in what way does it relate to C programming? Why not
just on comp.programming and ask "what language has a syntax of X"?

> I've seen you and others do this kind of thing quite a bit,
> and I have never understood it.  "There is at least one platform on
> which your code wouldn't run correctly," "There is at least one
> popular compiler that doesn't implement feature <x> that way,"

it's a math thing. Once you've produced one counter-example you've
destroyed the hypothesis/claim.

Why does it matter if the platform is an IBM mainframe or a DSP chip
(which both do rather odd things, or so we "all the worlds a VAX"
people think)?

I think its usually good enough that there /exists/ a a counter
example.

"A pointer won't fit in an int" "it doesn't have to be 2s complement"
"a pointer isn't always a simple number" "char* might not be the same
size as int*"

> and so
> on.  The information might be somewhat interesting, but it's useless
> without specifics.  You took the time to describe something, but you
> left out the most important detail!

opinions vary.

Richard Heathfield

unread,
Dec 10, 2009, 5:34:33 AM12/10/09
to
In
<287dd9ab-b1ab-4178...@m26g2000yqb.googlegroups.com>,
Nick Keighley wrote:

> On 10 Dec, 08:59, Squeamizh <sque...@hotmail.com> wrote:

<snip>



>> I've seen you and others do this kind of thing quite a bit,
>> and I have never understood it. "There is at least one platform on
>> which your code wouldn't run correctly," "There is at least one
>> popular compiler that doesn't implement feature <x> that way,"
>
> it's a math thing. Once you've produced one counter-example you've
> destroyed the hypothesis/claim.

But in mathematics, saying "I have a counter-example" is not
sufficient. You have to produce the counter-example. The point here
is not to prove that a given construct is not portable by producing a
counter-example, because we don't need one - we have the Standard for
that. No, the point - and this only works if the point-maker is
someone you trust not to lie to you - is to point out the danger of
using the construct. Naming a platform where the technique does not
work would most likely draw a reaction of "but I don't suppose my
code will ever run on that platform", which would be fair enough
except for the fact that the program /may/ one day have to run on
some other platform that shares the same issue. If that definitely
isn't a concern, fine, go ahead and use the construct - portability
is only one of several conflicting factors that we face with every
non-trivial program we write.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Frank

unread,
Dec 10, 2009, 5:47:27 AM12/10/09
to

Well, Richard, I trust you to be well ahead of the curve on C issues while
completely unrealistic about platform and portability isues.

My best first dice generator was in fortran.
--
frank

Kenny McCormack

unread,
Dec 10, 2009, 7:56:12 AM12/10/09
to
In article <10a6caac-370a-478e...@a39g2000pre.googlegroups.com>,

Welcome to CLC! We hope you enjoy your stay!

You have now been exposed to the CLC way - which is never to say
anything specific - that is to say, testable. Because if you do,
someone will call you on it, and show that you are wrong. The rules of
CLC are such that anything can be sub-divided and looked at in such a
way as to be wrong.

So, smart people avoid saying anything specific. It is all vague
references, such as you quote above.

Again, we hope you enjoy your stay!

Richard

unread,
Dec 10, 2009, 8:05:38 AM12/10/09
to
gaz...@shell.xmission.com (Kenny McCormack) writes:

Of more interest would be to ask the regs for pointers to their code so
we can verify that it is indeed compiled and used on more than their in
house MS Windows system for example.

And I mean the clique regs : not the obvious posters who do develop for
multiple platforms simultaneously.

"Flash Gordon" is at the top of the "systems you can only dream" league
of referrers IMO.

--
"Avoid hyperbole at all costs, its the most destructive argument on
the planet" - Mark McIntyre in comp.lang.c

Barry Schwarz

unread,
Dec 10, 2009, 8:49:38 AM12/10/09
to
On Wed, 09 Dec 2009 18:22:34 -0800, Keith Thompson <ks...@mib.org>
wrote:

>Seebs <usenet...@seebs.net> writes:
>> On 2009-12-09, Bill Cunningham <nos...@nspam.invalid> wrote:
>>> OK. It's been awhile since I tried error checking What I want if if
>>> (x=y) I need the comparison equals and not the assignment operator.
>>
>> You need to slow down, and possibly stop drinking while posting.
>>
>> None of this has anything to do with the distinction between comparison
>> and assignment.
>>
>> I really don't understand why you're still doing this. If you can't
>> understand the explanation you were just given, there is no *point*.
>> Do you have alexia or something?
>
>He's said he's suffering from the side effects of some medication he's
>taking; I don't remember the details.
>
>It's also been suggested that he's a deliberate troll. If so, he's
>quite skilled at it. (This does not imply any kind of admiration on
>my part; if he's deliberately trolling, it's pathetic, not admirable.)
>
>In any case, he's been trying to learn C programming for many years.
>

It's kind of strange that he doesn't seem to have these problems in
other newsgroups he posts to when he is really interested in solving
the problem.

--
Remove del for email

Bill Cunningham

unread,
Dec 10, 2009, 10:48:52 AM12/10/09
to

"Keith Thompson" <ks...@mib.org> wrote in message
news:lnfx7jp...@nuthaus.mib.org...

> To be fair, I just checked the index of K&R2 for LONG_MAX and
> LONG_MIN. It points to page 252, in the standard library reference,
> which contains the description of strtol. It mentions that strtol
> returns LONG_MIN or LONG_MAX on overflow, but it doesn't say where
> they're defined.
>
> That index entry should really point to page 257. (Bill, I think you
> have a copy of K&R2, so that's a big hint.)
>
> The online man page Bill cited is also a bit misleading. It says:
>
> LEGACY SYNOPSIS
>
> #include <stdlib.h>
> #include <limits.h>
>
> <limits.h> is necessary for the strtol() and strtoll() functions.
>
> (Note that if Bill had followed this advice he wouldn't be having this
> problem, though he still wouldn't know where strtol, LONG_MIN, and
> LONG_MAX are actually declared).

I have used strtol before Keith and I've never used limits.h myself. And
I've never heard of the LONG... macros until I actually looked at my man
page. Could I use 0 to report an error? I might just use atoi instead.

Ben Bacarisse

unread,
Dec 10, 2009, 11:33:47 AM12/10/09
to
Squeamizh <squ...@hotmail.com> writes:

> On Dec 9, 5:44 pm, Seebs <usenet-nos...@seebs.net> wrote:
>> Interesting trivia point:  I have written substantial code in a language
>> where
>
> Your trivia point would be more interesting if you named the
> language.

Unlikely to be what Seebs is talking about, but icon has this pattern
though not the exact syntax quoted. In icon you can say

i == (1 | 2)

to ask is i is 1 or 2 (the parentheses are needed). You can even say:

(x | y | z) == (1 | 2)

to find out is any of x, y or z are either 1 or 2.

<snip>
--
Ben.

Keith Thompson

unread,
Dec 10, 2009, 11:31:42 AM12/10/09
to
"Bill Cunningham" <nos...@nspam.invalid> writes:
[...]

> I have used strtol before Keith and I've never used limits.h
> myself. And I've never heard of the LONG... macros until I actually
> looked at my man page. Could I use 0 to report an error? I might
> just use atoi instead.

Ok, you've never used limits.h before. So you can use it now.
Just add "#include <limits.h>" to the top of your program.
It's trivial.

You were trying to use LONG_MAX and LONG_MIN. Several of us assumed
that it would be very easy to find out where they're defined,
but it turned out to be more difficult than we thought (the man
page you cited wasn't clear, and the index of K&R2 points to the
wrong place). You probably could have checked some other reference
or done a Google search, but whatever.

You now know that LONG_MAX and LONG_MIN are defined in <limits.h>
and the strtol function is declared in <stdlib.h>. Your question
has been answered.

Could you use 0 to report an error? I suppose so, but why on Earth
would you want to? 0 is typically a valid value.

You could use atoi if you wanted to, but you'd lose the possibility
of any real error checking, and you'd be throwing away all the
work you've done trying to use strtol. In a real-world program,
strtol is the right function to use here. If you give up now,
everything you've done so far will have been a collosal waste of
time, yours as well as ours. (It may have been anyway.)

Have you even *tried* to fix the errors in your program, or are you
just going to drop it and come back later with some other problem?

Fix your code or stop wasting our time.

Kenny McCormack

unread,
Dec 10, 2009, 11:48:03 AM12/10/09
to
In article <lnzl5qo...@nuthaus.mib.org>,

Keith Thompson <ks...@mib.org> wrote:
...
>Fix your code or stop wasting our time.

Oh, come on. You love it!

If Bill ever stops wasting your time, you'll have to actually look into
this thing, we, what do they call it, oh yeah, I remember now: a life!

Kenny McCormack

unread,
Dec 10, 2009, 11:49:34 AM12/10/09
to
In article <0.c585e3aa522550c207b1.2009...@bsb.me.uk>,

Many languages (though not low-level ones like C) had an "in" operator.
For example, SQL:

WHERE fld in (a,b,c)

Peter Nilsson

unread,
Dec 10, 2009, 4:32:42 PM12/10/09
to
Richard Heathfield <r...@see.sig.invalid> wrote:

> Nick Keighley wrote:
> > it's a math thing. Once you've produced one counter-example
> > you've destroyed the hypothesis/claim.
>
> But in mathematics, saying "I have a counter-example" is not
> sufficient. You have to produce the counter-example.

Or use the axiom of choice to show one exists, without
necessarily producing it.

--
Peter

Nick

unread,
Dec 10, 2009, 5:04:08 PM12/10/09
to
"Bill Cunningham" <nos...@nspam.invalid> writes:

> I had one error in this code until I tried to use error checking with
> strtol. Now I've really goofed it. This is the compilation I used.
>
> gcc -g di.c -o a -ansi -pedantic -Wall
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
> if (argc > 4 || argc == 0) {
> fputs("Dice generator usage error\n", stderr);
> exit(EXIT_FAILURE);
> }
> int x, y;


> if (argv[1][0] == '-' && argv[1][1] == 'a') {

> if ((x = strtol(argv[1], NULL, 10)) == LONG_MIN || LONG_MAX) {

> fputs("strtol error\n", stderr);
> return 1;
> }
> if ((y = strtol(argv[2], NULL, 10)) == LONG_MIN || LONG_MAX) {
> fputs("strtol error\n", stderr);
> return 1;
> }
> printf("%i\n", x + y);
> exit(1);
> }
> srand(time(NULL));
> printf("%i\n", rand(void) %argv[1]);
> return 0;
> }

Even when the bugs others have talked about have been shaken out, that's
not the most useful error checking you are doing there.

You'll only get LONG_MAX or LONG_MIN if you put a valid number into
strtol that is greater than the range of a long. Yet you end up putting
the result into an int - so there will be a great range of numbers out
of range that (assuming longs are bigger than ints on your platform)
that pass the test but don't fit into x or y.

Secondly, the LONG_xxx tests won't pick up the most common input error.
If the string is, for example, "seven" it will be accepted as valid, but
x will be set to 0. You need to use the middle parameter of strtol for
this.

In passing, for others, when I'm assigning strtol to an int is one of
the few times I use a not-entirely-necessary cast. It shows the reader
that I'm aware that I'm doing it (because I "know" the input will be in
range of an integer - for example I've assembled two hex digits in a
buffer).
--
Online waterways route planner: http://canalplan.org.uk
development version: http://canalplan.eu

Ike Naar

unread,
Dec 10, 2009, 5:16:27 PM12/10/09
to
In article <87zl5qh...@temporary-address.org.uk>,

Nick <3-no...@temporary-address.org.uk> wrote:
>You'll only get LONG_MAX or LONG_MIN if you put a valid number into
>strtol that is greater than the range of a long.

You'll also get LONG_MAX or LONG_MIN if you put LONG_MAX,
resp. LONG_MIN into strtol.

Keith Thompson

unread,
Dec 10, 2009, 5:26:30 PM12/10/09
to

Right. To distinguish whether LONG_MIN or LONG_MAX indicates an
overflow, you need to set errno to 0 before the call and check its
value after the call. (If you don't set it to 0 before the call,
you risk the possibility that errno had already been set to ERANGE
by something else; standard functions don't set errno to 0.)

Nick

unread,
Dec 11, 2009, 1:37:02 AM12/11/09
to
i...@localhost.claranet.nl (Ike Naar) writes:

True, thanks. As has been said many times before, catching all errors
is complicated, even with strtol to help.

Practically, I think that's the least of the problems
here: and limiting the number of dice that generated to LONG_MAX-1 isn't
going to reduce the utility of the program that much.

Nick Keighley

unread,
Dec 11, 2009, 4:18:18 AM12/11/09
to
On 10 Dec, 13:05, Richard <rgrd...@gmail.com> wrote:
> gaze...@shell.xmission.com (Kenny McCormack) writes:

<snip>

> > You have now been exposed to the CLC way - which is never to say
> > anything specific - that is to say, testable.   Because if you do,
> > someone will call you on it, and show that you are wrong.  The rules of
> > CLC are such that anything can be sub-divided and looked at in such a
> > way as to be wrong.
>
> > So, smart people avoid saying anything specific.  It is all vague
> > references, such as you quote above.
>
> > Again, we hope you enjoy your stay!
>
> Of more interest would be to ask the regs for pointers to their code so
> we can verify that it is indeed compiled and used on more than their in
> house MS Windows system for example.
>
> And I mean the clique regs : not the obvious posters who do develop for
> multiple platforms simultaneously.

I'm not sure which category I fall in but I maintain a C++ application
(which I can't post) that has been ported from HPUX to MS Windows.

I am aware of an application that broke when moved from one Sun
machine to another (endianess changed!).

I've weird embedded systesm where a fair amount of testing for it was
done on MS Windows some on a VAX (ok, this was a while ago!). The
weird embedded system was neither of these.

I routinely expect code to run on both Windows and Linux.

> "Flash Gordon" is at the top of the "systems you can only dream" league
> of referrers IMO.

--
Nick Keighley
1,3,7-trimethylxanthine -- a basic ingredient in quality software.

Ben Bacarisse

unread,
Dec 11, 2009, 8:30:17 AM12/11/09
to
Nick <3-no...@temporary-address.org.uk> writes:

> i...@localhost.claranet.nl (Ike Naar) writes:
>
>> In article <87zl5qh...@temporary-address.org.uk>,
>> Nick <3-no...@temporary-address.org.uk> wrote:
>>>You'll only get LONG_MAX or LONG_MIN if you put a valid number into
>>>strtol that is greater than the range of a long.
>>
>> You'll also get LONG_MAX or LONG_MIN if you put LONG_MAX,
>> resp. LONG_MIN into strtol.
>
> True, thanks. As has been said many times before, catching all errors
> is complicated, even with strtol to help.
>
> Practically, I think that's the least of the problems
> here: and limiting the number of dice that generated to LONG_MAX-1 isn't
> going to reduce the utility of the program that much.

I agree, but that is not what is happening (yet). strtol is used only
to find two numbers to add (truly bizarre, but I don't want to try to
find out why this is done).

If, later, the program were to use strtol to find the upper limit of
the dice roll, then a bunch of other problems spring up, not least the
fact the RAND_MAX may be a lot smaller than LONG_MAX.

--
Ben.

Richard Bos

unread,
Dec 11, 2009, 5:10:25 PM12/11/09
to
Nick Keighley <nick_keigh...@hotmail.com> wrote:

> On 10 Dec, 08:59, Squeamizh <sque...@hotmail.com> wrote:

> > On Dec 9, 5:44=A0pm, Seebs <usenet-nos...@seebs.net> wrote:
>
> > > Interesting trivia point: =A0I have written substantial code in a

> > > language where
> >
> > Your trivia point would be more interesting if you named the
> > language.
>
> only slightly. in what way does it relate to C programming? Why not
> just on comp.programming and ask "what language has a syntax of X"?

'cause they'd probably not know which language it is, unless certain
people read that group.

It's Inform 6.

Richard

Seebs

unread,
Dec 11, 2009, 6:29:13 PM12/11/09
to
On 2009-12-11, Richard Bos <ral...@xs4all.nl> wrote:
> It's Inform 6.

Ayup.

Didn't think the specific language was relevant, just thought it was
neat that the syntax did exist. And I'd forgotten about Icon, but I've
used that too.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Richard Bos

unread,
Dec 12, 2009, 1:33:56 PM12/12/09
to
gaz...@shell.xmission.com (Kenny McCormack) wrote:

> Ben Bacarisse <ben.u...@bsb.me.uk> wrote:
> >Unlikely to be what Seebs is talking about, but icon has this pattern
> >though not the exact syntax quoted. In icon you can say
> >
> > i == (1 | 2)
> >
> >to ask is i is 1 or 2 (the parentheses are needed). You can even say:
> >
> > (x | y | z) == (1 | 2)
> >
> >to find out is any of x, y or z are either 1 or 2.
>
> Many languages (though not low-level ones like C) had an "in" operator.
> For example, SQL:
>
> WHERE fld in (a,b,c)

Yah. But unlike those languages, Icon does actual, backtracking,
pattern-matching.

Yes, this is beautiful.

Yes, this is horrendous.

Richard

0 new messages