Re: What does a sleep(-1) do?

Barry Margolin <barmar@xxxxxxxxxxxx> writes:
In article <44b37343$0$31648$e4fe514c@xxxxxxxxxxxxxx>,
Casper H.S. Dik <Casper.Dik@xxxxxxx> wrote:
Barry Margolin <barmar@xxxxxxxxxxxx> writes:
In article <44b219f9$0$31637$e4fe514c@xxxxxxxxxxxxxx>,
Casper H.S. Dik <Casper.Dik@xxxxxxx> wrote:
jeroendecock@xxxxxxxxx writes:
Can someone explain what the sleep function with a negative value does?
You can't call sleep with a negative argument:

unsigned int sleep(unsigned int seconds);

If you pass it "-1" the implementation will most likely see this
as the highest possible unsigned number.

"most likely"? As long as the proper header file has been used so that
the prototype has been seen, it's REQUIRED to treat it this way (as all
the previous responses pointed out).

Oh, is a two's complement implementation a requirement for POSIX

Not POSIX, C. C says that when a negative number is assigned to an
unsigned int, it's converted modulo UINT_MAX+1.

Right. On a two's complement implementation, the conversion can be
done just by copying the representation. On a ones' complement or
sign and magnitude implementation, the compiler will have to do a
little extra work do to the conversion correctly.

(The C99 standard requires one of those three representations for
signed integer types; the C90 standard is less specific, but has the
same requirements for conversions.)

Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <>
San Diego Supercomputer Center <*> <>
We must do something. This is something. Therefore, we must do this.

Relevant Pages

  • Re: if(a);
    ... The type of the octal signed int literal 0 will be compared to the ... 'a' has type unsigned int, in which case 0 is converted to ... is still 0 after undergoing conversion, ...
  • Re: print hex value of char
    ... The %x format is for printing unsigned int. ... do cause an actual conversion, but then the actual promoted type to ... If any argument is not the correct type for the corresponding ... suggests that it is undefined behaviour. ...
  • Re: Questions, please
    ... a negative number to an `unsigned int', ... Of course not: the *conversion* from `double' ... > reason it allows it is to avoid breaking hundreds of pre-standard programs. ... on these machines the cast ...
  • Re: Promoting unsigned long int to long int
    ... Maybe there are more unsigned integer types in the standard, ... if (errno == ERANGE) ... Why force conversion to double when 0 will do ... you should cast a to unsigned int. ...
  • Re: Newbie - itoa implementation redux
    ... after which conversion to `unsigned int' will give ... is not guaranteed, though, and I have used machines that trapped ... shows how to do the conversion by using non-positive numbers. ... leaving a time bomb for subsequent callers. ...