What does ((time_t)-1) mean?

C

Charlie Gordon

Kenneth Brody said:
What part of C99 would raise a signal if time_t is an unsigned long
or larger? Is it the "loss of precision" cause by casting it to a
(signed) long?

Also, going way back to the OP, note that 7.23.2.4p3 refers to
"(time_t)(-1)", and not "((time_t)-1)". (Note the parens around "-1",
making it unambiguous that "(time_t)" is a cast, and not a variable
name.)

Ambiguity remains, but time_t would need to be a (locally defined) function
pointer.
 
K

Kenny McCormack

Kenneth Brody said:
Also, going way back to the OP, note that 7.23.2.4p3 refers to
"(time_t)(-1)", and not "((time_t)-1)". (Note the parens around "-1",
making it unambiguous that "(time_t)" is a cast, and not a variable
name.)

OK, so I change my original source code to:

#include <stdio.h>
int time_t(int x) { return x + 6; }
int main(void)
{
printf("The result is %d\n",((time_t)(-1)));
return 0;
}
 
K

Keith Thompson

Kenneth Brody said:
What part of C99 would raise a signal if time_t is an unsigned long
or larger? Is it the "loss of precision" cause by casting it to a
(signed) long?
[...]

Yes.

C99 6.3.1.3p3:

Otherwise, the new type is signed and the value cannot be
represented in it; either the result is implementation-defined or
an implementation-defined signal is raised.

The conversion of -1 to time_t can't cause a problem, but the
conversion to long potentially can.
 
A

Army1987

time_t is required by the C standard to be an arithmetic type. It
could be a signed integer type, an unsigned integer type, or a
floating-point type.
<nitpick>
Where on earth does the standard forbid it from being "plain"
char?
</nitpick>
(I don't think this applies to any real implementation, but...)
 
K

Keith Thompson

Army1987 said:
<nitpick>
Where on earth does the standard forbid it from being "plain"
char?
</nitpick>
(I don't think this applies to any real implementation, but...)

Nothing (other than good taste). If it made sense for time_t to be a
character type, I'd use either signed char or unsigned char. My first
statement above is correct; my second statement above is incomplete.

The fact that plain char is an integer type, but is neither a signed
integer type nor an unsigned integer type, is just an oddity. I
suppose it's a necessary one; the alternative would be for the
classification of plain char to be implementation-defined.

It's almost an interesting question whether time_t could be _Bool. It
has to be "capable of representing times"; _Bool could represent only
two distinct times, one of which ((_Bool)(-1), which is 1) would be an
error indication for the time() function. (_Bool is an unsigned
integer type.)

time_t could also theoretically be a complex type. I'm not sure even
the DS9K would do this; perhaps the DS10K would.
 
C

Charlie Gordon

Keith Thompson said:
Nothing (other than good taste). If it made sense for time_t to be a
character type, I'd use either signed char or unsigned char. My first
statement above is correct; my second statement above is incomplete.

The fact that plain char is an integer type, but is neither a signed
integer type nor an unsigned integer type, is just an oddity. I
suppose it's a necessary one; the alternative would be for the
classification of plain char to be implementation-defined.

It's almost an interesting question whether time_t could be _Bool. It
has to be "capable of representing times"; _Bool could represent only
two distinct times, one of which ((_Bool)(-1), which is 1) would be an
error indication for the time() function. (_Bool is an unsigned
integer type.)

time_t could also theoretically be a complex type. I'm not sure even
the DS9K would do this; perhaps the DS10K would.

How so? I always thought the DS9K ran on imaginary time.
 
D

David Thompson

...
Right, except you should say scope rather than block. Every block is a
scope, but file scope (and FWLIW prototype scope) is not a block.
And in C99 several statements are scopes even without using blocks.
(struct or union body is also a scope and not a block, but in C only
for members said:
Here is my complete source file:

==========
#include <stdio.h>

typedef int my_int_t;
That's at file scope.
int main(void)
{
my_int_t my_int_t = 5;
That's in a block scope, namely the body of main.

<snip: fine with particular gcc and MSVC>

As he said, in different scopes it's legal. Confusing; I think bad
style, and I bet many agree with me; but legal.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top