473,414 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

What does ((time_t)-1) mean?

Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"

RETURN VALUE
On success, the value of time in seconds since the Epoch is
retu
rned.
On error, ((time_t)-1) is returned, and errno is set
appropriately
..
Could anybody tell me its meaning please?

And, is my way of checking return value correct?

time_t now;

time(&now);
if (!now) {
fprintf(stderr, "Unable to fetch time information: %s
\n",
strerror(errno));
return 1;
}

Thanks for answering!

Oct 24 '07 #1
45 9275
loudking wrote:
Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"

RETURN VALUE
On success, the value of time in seconds since the Epoch is
retu
rned.
On error, ((time_t)-1) is returned, and errno is set
appropriately
.
Could anybody tell me its meaning please?
time's return type is time_t, so the return value in case of error is a
time_t typed value of -1
>
And, is my way of checking return value correct?

time_t now;

time(&now);
if (!now) {
fprintf(stderr, "Unable to fetch time information: %s
\n",
strerror(errno));
return 1;
}

Thanks for answering!
time_t now;
if(time(&now) == -1) {
fprintf(stderr, "Unable to fetch time information: %sn",
strerror(errno));
return 1;
}

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Oct 24 '07 #2
loudking:
On error, ((time_t)-1) is returned, and errno is set
appropriately
.
Could anybody tell me its meaning please?

When a signed integer type expression whose value is negative is
converted to an unsigned integer type, e.g.:

unsigned a = -5;

short unsigned b = -5;

char unsigned c = -5;

then it's the same as:

unsigned a = UINT_MAX - 4;

short unsigned b = USHRT_MAX - 4;

char unsigned c = UCHAR_MAX - 4;

Therefore, when you assign -1 to an unsigned integer type, you're
giving it its maximum value (i.e. MAX - 0)

A more general formula would be:

(uint_type)-x

is the same as:

UINT_TYPE_MAX - (x-1)
The function you're asking about will return the max value for a
time_t.

Martin
Oct 24 '07 #3
time_t now;
if(time(&now) == -1) {
fprintf(stderr, "Unable to fetch time information: %sn",
strerror(errno));
return 1;

}
Then do i have to write it in this way?

if(time(&now) == (time_t)-1) {
.......

Oct 24 '07 #4
loudking:
Then do i have to write it in this way?

if(time_t(&now) == (time_t)-1)

I don't know what function you're working with, but if it returns a
time_t, then you'd want:

if ((time_t)-1 == Func())

(The time_t cast is redundant if time_t is bigger than int, but still
I'd leave it in)

Martin

Oct 24 '07 #5
loudking wrote:
>time_t now;
if(time(&now) == -1) {
fprintf(stderr, "Unable to fetch time information: %sn",
strerror(errno));
return 1;

}

Then do i have to write it in this way?

if(time(&now) == (time_t)-1) {
......
You don't have to. It's done automatically (integer promotion).
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Oct 24 '07 #6
Martin Wells wrote:
loudking:
> On error, ((time_t)-1) is returned, and errno is set
appropriately
.
Could anybody tell me its meaning please?
....
The function you're asking about will return the max value for a
time_t.
It's an expression, not a function. It has a value which is the result
of converting -1 to whatever type time_t is. Your answer is correct only
if time_t is an unsigned type. If it is a signed integer or floating
point type, then that expression will have a value of -1, which is very
definitely not the maximum value for those types.
Oct 24 '07 #7
Pietro Cerutti wrote:
loudking wrote:
>>time_t now;
if(time(&now) == -1) {
fprintf(stderr, "Unable to fetch time information: %sn",
strerror(errno));
return 1;

}
Then do i have to write it in this way?

if(time(&now) == (time_t)-1) {
......

You don't have to. It's done automatically (integer promotion).
You're making assumptions about time_t which might be reasonable, but
which are not required by the standard, which only requires it to be an
arithmetic type. If, for instance, time_t is a 32-bit unsigned short,
and int is 64 bits, then leaving out the cast will result in value of
USHRT_MAX being promoted to an 'int' and then failing when it is
compared with -1.

In real life, systems where time_t is both unsigned and smaller than int
are probably very uncommon, maybe even non-existent; but they would be
legal.
Oct 24 '07 #8
Pietro Cerutti wrote:
loudking wrote:
>>time_t now;
if(time(&now) == -1) {
fprintf(stderr, "Unable to fetch time information: %sn",
strerror(errno));
return 1;

}
Then do i have to write it in this way?

if(time(&now) == (time_t)-1) {
......

You don't have to. It's done automatically (integer promotion).
If (time_t)-1 < INT_MAX, the cast is required. (I've never
seen and may never see a system where this condition holds, but
why take even a small risk if it's unnecessary?)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 24 '07 #9
In article <11*********************@v29g2000prd.googlegroups. com>,
loudking <lo******@gmail.comwrote:
>Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"
This (perfectly legal) program says that the answer to your question is 5:

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

Oct 24 '07 #10
loudking wrote:
Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"
Referencing "man 2" is probably a hint that the question really belongs
in comp.unix.programmer.

However, you've provided more or less enough information for us to
answer the question in terms of what is regarded as "on-topic" in
comp.lang.c

(time_t)-1 is an example of a "cast" operation.

We assume that somewhere (in a header, probably called "time.h") a
typedef has declared an integer type called "time_t". (time_t)-1 is -1
expressed as one of these types.
RETURN VALUE
On success, the value of time in seconds since the Epoch is
returned.
On error, ((time_t)-1) is returned, and errno is set
appropriately
So when you call time(), if it is successful it returns a positive
time_t value but if it is unsuccessful, it returns (time_t)-1;
(The reference to "errno" here is off-topic for comp.lang.c, I think)
And, is my way of checking return value correct?
time_t now;

time(&now);
if (!now) {
The error path would only be taken if "now" was (time_t)0 - wouldn't it?

In addition, the manual page specified that time() _returns (time_t)-1
for an error, not that it sets the time_t passed to it to (time_t)-1.

The natural way of using this function is probably something like :-

time_t now;
if (time(&now) == (time_t)-1) {
fprintf(stderr, "Unable to fetch time information: %s\n",
strerror(errno));
return 1;
}
The cast is, probably, unnecessary - but a) it could conceivably be
necessary (see Eric's comments) and b) it makes things nice and explicit.
Oct 24 '07 #11
loudking <lo******@gmail.comwrites:
Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"

RETURN VALUE
On success, the value of time in seconds since the Epoch is
retu
rned.
On error, ((time_t)-1) is returned, and errno is set
appropriately
.
Could anybody tell me its meaning please?

And, is my way of checking return value correct?

time_t now;

time(&now);
You didn't check the "return value" - however "now" does contain a copy
of the return value....

if (!now) {
"!now" is not the same as checking for a time_t of -1 .....
fprintf(stderr, "Unable to fetch time information: %s
\n",
strerror(errno));
return 1;
}

Thanks for answering!
I would think something like

if(time(&now)==(time_t)-1){
error
}

is better ... as the man page says.
Oct 24 '07 #12
Kenny McCormack wrote:
>
In article <11*********************@v29g2000prd.googlegroups. com>,
loudking <lo******@gmail.comwrote:
Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"

This (perfectly legal) program says that the answer to your question is 5:

#include <stdio.h>
int main(void)
{
int time_t = 6;
printf("The result is %d\n",((time_t)-1));
return 0;
}
Is it legal to declare a variable named "time_t"? True, you have
not included <time.hwhich is why it "works".

Are you allowed to repurpose a type/variable/macro defined by the
standard, as long as you don't use anything related to the way
the standard defines them?

If, for example, defining your own printf() function invokes UB,
even if you never make reference (direct or otherwise) to the
standard's printf() function, doesn't the same hold true for the
time_t typedef?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Oct 24 '07 #13
Kenneth Brody <ke******@spamcop.netwrites:
Kenny McCormack wrote:
>>
In article <11*********************@v29g2000prd.googlegroups. com>,
loudking <lo******@gmail.comwrote:
>Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"

This (perfectly legal) program says that the answer to your question is 5:

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

Is it legal to declare a variable named "time_t"? True, you have
not included <time.hwhich is why it "works".

Are you allowed to repurpose a type/variable/macro defined by the
standard, as long as you don't use anything related to the way
the standard defines them?

If, for example, defining your own printf() function invokes UB,
even if you never make reference (direct or otherwise) to the
standard's printf() function, doesn't the same hold true for the
time_t typedef?
If you don't include the definition then why isn't it perfectly ok?
Stupid, but ok.
Oct 24 '07 #14
On Wed, 24 Oct 2007 16:21:26 +0100,
Mark Bluemel <ma**********@pobox.comwrote:
loudking wrote:
>Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"

Referencing "man 2" is probably a hint that the question really belongs
in comp.unix.programmer.
The type time_t is defined in the C language standard. The fact that
time() on the OP's system is in section 2 of the manual pages does not
change that.

Martien
--
|
Martien Verbruggen | Think of the average person. Half of the
| people out there are dumber.
|
Oct 24 '07 #15
Richard wrote:
Kenneth Brody <ke******@spamcop.netwrites:
>Kenny McCormack wrote:
>>>
In article <11*********************@v29g2000prd.googlegroups. com>,
loudking <lo******@gmail.comwrote:
Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"
This (perfectly legal) program says that the answer to your question
is 5:

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

Is it legal to declare a variable named "time_t"? True, you have
not included <time.hwhich is why it "works".

Are you allowed to repurpose a type/variable/macro defined by the
standard, as long as you don't use anything related to the way
the standard defines them?

If, for example, defining your own printf() function invokes UB,
even if you never make reference (direct or otherwise) to the
standard's printf() function, doesn't the same hold true for the
time_t typedef?

If you don't include the definition then why isn't it perfectly ok?
Stupid, but ok.
I think it's UB to redefine Standard symbols.

Oct 25 '07 #16
On Thu, 25 Oct 2007 09:09:01 +0530, santosh wrote:
I think it's UB to redefine Standard symbols.
Redefining external symbols is a bad idea, but defining your own symbols
which happen to have the same name as standard symbols is allowed.
Oct 25 '07 #17
Kenneth Brody wrote:
Kenny McCormack wrote:
>In article <11*********************@v29g2000prd.googlegroups. com>,
loudking <lo******@gmail.comwrote:
>>Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"
This (perfectly legal) program says that the answer to your question is 5:

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

Is it legal to declare a variable named "time_t"? True, you have
not included <time.hwhich is why it "works".
time_t is not part of any C standard, AFAIK.

The compiler knows nothing about it, unless you include the <time.hheader.

time() (as discussed here) is part of POSIX, not part of C.
Oct 25 '07 #18
Mark Bluemel wrote:
Kenneth Brody wrote:
>Kenny McCormack wrote:
>>In article <11*********************@v29g2000prd.googlegroups. com>,
loudking <lo******@gmail.comwrote:
Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"

This (perfectly legal) program says that the answer to your question
is 5:

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

Is it legal to declare a variable named "time_t"? True, you have
not included <time.hwhich is why it "works".
Please ignore my comments below...
time_t is not part of any C standard, AFAIK.

The compiler knows nothing about it, unless you include the <time.h>
header.

time() (as discussed here) is part of POSIX, not part of C.
Oct 25 '07 #19
Martien Verbruggen wrote:
On Wed, 24 Oct 2007 16:21:26 +0100,
Mark Bluemel <ma**********@pobox.comwrote:
>loudking wrote:
>>Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"
Referencing "man 2" is probably a hint that the question really belongs
in comp.unix.programmer.

The type time_t is defined in the C language standard. The fact that
time() on the OP's system is in section 2 of the manual pages does not
change that.
That will teach me to trust the manual pages rather than reading the
standard...
Oct 25 '07 #20
In article <ff**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
>The type time_t is defined in the C language standard. The fact that
time() on the OP's system is in section 2 of the manual pages does not
change that.
>That will teach me to trust the manual pages rather than reading the
standard...
If you man page said that time() conforms to Posix, it was correct.
It merely omitted to say it also conforms to ISO C. Posix time() is
more rigidly defined that C's version - it specifies that it returns
seconds since Jan 1 1970.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 25 '07 #21
Kenneth Brody wrote:
Kenny McCormack wrote:
>In article <11*********************@v29g2000prd.googlegroups. com>,
loudking <lo******@gmail.comwrote:
>>Hello, all

I don't quite understand what does ((time_t)-1) mean when I execute
"man 2 time"
This (perfectly legal) program says that the answer to your question is 5:

#include <stdio.h>
int main(void)
{
int time_t = 6;
printf("The result is %d\n",((time_t)-1));
return 0;
}
That response was merely a pointless attempt to show how clever Kenny
is, without being in anyway useful to the OP. I'm sure Kenny doesn't
care, but all it did was make him look silly, not clever.
Is it legal to declare a variable named "time_t"? True, you have
not included <time.hwhich is why it "works".

Are you allowed to repurpose a type/variable/macro defined by the
standard, as long as you don't use anything related to the way
the standard defines them?
The standard's time_t typedef has file scope, and is in the name space
of ordinary identifiers. It "... is reserved for use as a macro name and
as an identifier with file scope in the same name space", but only " if
any of its associated headers is included." (7.1.3p1). Therefore, even
if he had #included the header, use in any scope other than file scope
is fine. Since he didn't #include the header, the issue wouldn't even
come up at file scope.
If, for example, defining your own printf() function invokes UB,
even if you never make reference (direct or otherwise) to the
standard's printf() function, doesn't the same hold true for the
time_t typedef?
The printf() function name is an identifier with external linkage; the
rules are different in that case. such identifiers "... are always
reserved for use as identifiers with external linkage ..." (7.1.3p1).
Note that you can still define your own printf identifier, so long as
you declare it 'static', giving it internal linkage, or use it as an
identifier with no linkage (like a typedef). However, if you declare it
static then you cannot #include <stdio.hin the same translation unit,
because then you would have declarations of the same identifier with
internal and external linkage in the same translation unit (6.2.2p7).

Whether either of these cases is a good idea is a different matter - as
a general rule I would recommend avoiding any alternative definition of
names defined in the standard library, even in contexts where it is
legal to do so. But it is legal to use time_t and printf in the manner
described above.
Oct 25 '07 #22
"Eric Sosman" <es*****@ieee-dot-org.invalida écrit dans le message de
news: 25******************************@comcast.com...
Pietro Cerutti wrote:
>loudking wrote:
>>>time_t now;
if(time(&now) == -1) {
fprintf(stderr, "Unable to fetch time information: %sn",
strerror(errno));
return 1;

}
Then do i have to write it in this way?

if(time(&now) == (time_t)-1) {
......

You don't have to. It's done automatically (integer promotion).

If (time_t)-1 < INT_MAX, the cast is required. (I've never
seen and may never see a system where this condition holds, but
why take even a small risk if it's unnecessary?)
if time_t is signed, the condition holds and the cast is not required.

--
Chqrlie.
Oct 25 '07 #23
In article <47**********************@news.free.fr>,
Charlie Gordon <ne**@chqrlie.orgwrote:
> If (time_t)-1 < INT_MAX, the cast is required. (I've never
seen and may never see a system where this condition holds, but
why take even a small risk if it's unnecessary?)
>if time_t is signed, the condition holds and the cast is not required.
Likewise if time_t is a floating-point type.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 25 '07 #24
Harald van D)&k wrote:
>
On Thu, 25 Oct 2007 09:09:01 +0530, santosh wrote:
I think it's UB to redefine Standard symbols.

Redefining external symbols is a bad idea, but defining your own symbols
which happen to have the same name as standard symbols is allowed.
On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.

I'm still not convinced one way or the other. (Though I do know not
to actually use such a construct in the first place in any "real"
code.)

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

void foo(void)
{
time_t time_t = 5;
printf("%ld\n",(long)( (time_t)-1 ) );
}
void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}

int main(void)
{
foo();
bar();
}
==========

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Oct 25 '07 #25
James Kuyper wrote:
>
Kenny McCormack wrote:
[...]
This (perfectly legal) program says that the answer to your question is 5:

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

That response was merely a pointless attempt to show how clever Kenny
is, without being in anyway useful to the OP. I'm sure Kenny doesn't
care, but all it did was make him look silly, not clever.
Well, Kenny's intentions aside, it does bring up a valid question
regarding variables with the same name as a typedef. My compiler
allows a variable named "time_t" even with <time.hincluded. In
such a case, "(time_t)-1" is (to me) ambiguous. My compiler treats
it as "the variable time_t, minus one", but it could (unless the
standard says otherwise) interpret as "cast negative one to the
time_t type".

I'm wondering what, if anything, the standard says about this.

[...]
The standard's time_t typedef has file scope, and is in the name space
of ordinary identifiers. It "... is reserved for use as a macro name and
as an identifier with file scope in the same name space", but only " if
any of its associated headers is included." (7.1.3p1). Therefore, even
if he had #included the header, use in any scope other than file scope
is fine. Since he didn't #include the header, the issue wouldn't even
come up at file scope.
Let's ignore standard-defined typedefs for the moment. What about:

typedef int my_int_t;
my_int_t my_int_t = 4;
...
printf("%d\n", (my_int_t)-1 );

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Oct 25 '07 #26
On Thu, 25 Oct 2007 10:36:53 -0400, Kenneth Brody wrote:
Harald van D)&k wrote:
>>
On Thu, 25 Oct 2007 09:09:01 +0530, santosh wrote:
I think it's UB to redefine Standard symbols.

Redefining external symbols is a bad idea, but defining your own
symbols which happen to have the same name as standard symbols is
allowed.

On the other hand, my compiler gives no warnings (even with warnings at
max) and displays "4" and "-1" for the following code. Of course, as
has been said here numerous times, "works as expected" is a valid
outcome for UB.

I'm still not convinced one way or the other. (Though I do know not to
actually use such a construct in the first place in any "real" code.)

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

void foo(void)
{
time_t time_t = 5;
printf("%ld\n",(long)( (time_t)-1 ) ); }
void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) ); }

int main(void)
{
foo();
bar();
}
==========
This does not redefine any standard symbol. This is a case of defining
your own symbols which happen to have the same name as standard symbols.
An example:

#include <stdio.h>

void time(void) /* error: conflicts with time_t time(time_t *), since
both are external symbols */
{}

static int printf; /* error: conflicts with int printf(const char
*restrict, ...), since both have file scope */

int time_t; /* okay: doesn't conflict with time_t, since <time.h>'s
version isn't external and isn't defined */

int main(int fread, char *fopen[]) /* okay: doesn't conflict with fread
or fopen, since neither is declared as an external symbol */
{
#undef stderr
#define stderr return 0 /* error: redefines the standard symbol stderr,
since <stdio.his included and defines stderr as a macro */
stderr;
}
Oct 25 '07 #27
On Thu, 25 Oct 2007 10:45:53 -0400, Kenneth Brody wrote:
Let's ignore standard-defined typedefs for the moment. What about:

typedef int my_int_t;
my_int_t my_int_t = 4;
...
printf("%d\n", (my_int_t)-1 );

[...]
If the typedef and the object are defined in the same block (or both at
file scope), this is not allowed. Otherwise, it is.
Oct 25 '07 #28
$)CHarald van D)&k wrote:
On Thu, 25 Oct 2007 10:45:53 -0400, Kenneth Brody wrote:
>Let's ignore standard-defined typedefs for the moment. What about:

typedef int my_int_t;
my_int_t my_int_t = 4;
...
printf("%d\n", (my_int_t)-1 );

[...]

If the typedef and the object are defined in the same block (or both
at file scope), this is not allowed. Otherwise, it is.
In that case it's rather surprising that gcc, at least, doesn't produce
any diagnostic even when '-Wall', '-W', '-ansi' and '-pedantic' are
provided.

Oct 25 '07 #29
On Thu, 25 Oct 2007 23:29:47 +0530, santosh wrote:
$)CHarald van D)&k wrote:
>On Thu, 25 Oct 2007 10:45:53 -0400, Kenneth Brody wrote:
>>Let's ignore standard-defined typedefs for the moment. What about:

typedef int my_int_t;
my_int_t my_int_t = 4;
...
printf("%d\n", (my_int_t)-1 );

[...]

If the typedef and the object are defined in the same block (or both at
file scope), this is not allowed. Otherwise, it is.

In that case it's rather surprising that gcc, at least, doesn't produce
any diagnostic even when '-Wall', '-W', '-ansi' and '-pedantic' are
provided.
Do you mean that it doesn't produce any diagnostic in the same block, or
in different blocks? If the former, that's a bug, and I encourage you to
report it. The version of gcc I'm using reports an unconditional error
for this. If the latter, the code is perfectly valid, so why should it
complain? There are enough cases where hiding the outer symbol this way
is intentional to not warn by default, but if you do want the warning,
there's an option just for that (-Wshadow).
Oct 25 '07 #30
On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.

void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}
A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).

Oct 25 '07 #31
"Old Wolf" <ol*****@inspire.net.nza écrit dans le message de news:
11**********************@t8g2000prg.googlegroups.c om...
On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
>On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.

void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}

A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).
'minor' does not begin to describe how minuscule it is in real life.

--
Chqrlie.
Oct 26 '07 #32
Charlie Gordon said:
"Old Wolf" <ol*****@inspire.net.nza écrit dans le message de news:
11**********************@t8g2000prg.googlegroups.c om...
>On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
>>On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.

void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}

A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).

'minor' does not begin to describe how minuscule it is in real life.
By "real life", presumably, you mean "systems that Charlie Gordon has
worked on".

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 26 '07 #33
"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message de news:
9c******************************@bt.com...
Charlie Gordon said:
>"Old Wolf" <ol*****@inspire.net.nza écrit dans le message de news:
11**********************@t8g2000prg.googlegroups.c om...
>>On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.

void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}

A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).

'minor' does not begin to describe how minuscule it is in real life.

By "real life", presumably, you mean "systems that Charlie Gordon has
worked on".
I have probably worked on more different systems than you have.
Can anyone name a real-life system where casting a time_t or whatever
unsigned type to long causes a signal to be raised, or any kind of
surprising behaviour?

--
Chqrlie.
Oct 26 '07 #34
Charlie Gordon said:
"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message de
news: 9c******************************@bt.com...
>Charlie Gordon said:
>>"Old Wolf" <ol*****@inspire.net.nza écrit dans le message de news:
11**********************@t8g2000prg.googlegroups.c om...
On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.
>
void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}

A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).

'minor' does not begin to describe how minuscule it is in real life.

By "real life", presumably, you mean "systems that Charlie Gordon has
worked on".

I have probably worked on more different systems than you have.
Yes, you probably have. Er, so what? It is clear that you have never worked
on a system where the conversion of a time_t to a long int causes a
problem. Otherwise, you surely would not have answered as you did.
Can anyone name a real-life system where casting a time_t or whatever
unsigned type to long causes a signal to be raised, or any kind of
surprising behaviour?
Wrong question, or at least it's an incomplete question. What it's missing
is this: "...or could a computer manufacturer, for commercial or
engineering reasons of its own, release a system at some point in the
future that would break the above code with unfortunate consequences
despite the provision and use of a conforming ISO C compiler for that
system?"

And the answer is yes.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 26 '07 #35
"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message de news:
hZ******************************@bt.com...
Charlie Gordon said:
>"Richard Heathfield" <rj*@see.sig.invalida écrit dans le message de
news: 9c******************************@bt.com...
>>Charlie Gordon said:

"Old Wolf" <ol*****@inspire.net.nza écrit dans le message de news:
11**********************@t8g2000prg.googlegroups.c om...
On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
>On the other hand, my compiler gives no warnings (even with warnings
>at max) and displays "4" and "-1" for the following code. Of course,
>as has been said here numerous times, "works as expected" is a valid
>outcome for UB.
>>
>void bar(void)
> {
> printf("%ld\n",(long)( (time_t)-1 ) );
> }
>
A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).

'minor' does not begin to describe how minuscule it is in real life.

By "real life", presumably, you mean "systems that Charlie Gordon has
worked on".

I have probably worked on more different systems than you have.

Yes, you probably have. Er, so what? It is clear that you have never
worked
on a system where the conversion of a time_t to a long int causes a
problem. Otherwise, you surely would not have answered as you did.
Have you ever worked on one?
Your answer below indicates you haven't.
Such systems are either obsolete, hypothetical or doomed.
>Can anyone name a real-life system where casting a time_t or whatever
unsigned type to long causes a signal to be raised, or any kind of
surprising behaviour?

Wrong question, or at least it's an incomplete question. What it's missing
is this: "...or could a computer manufacturer, for commercial or
engineering reasons of its own, release a system at some point in the
future that would break the above code with unfortunate consequences
despite the provision and use of a conforming ISO C compiler for that
system?"

And the answer is yes.
Porting "real-life" programs to this architectural oddity would be a
pointless nightmare.

--
Chqrlie.
Oct 26 '07 #36
Charlie Gordon said:
"Richard Heathfield" a écrit...
>Charlie Gordon said:
>>"Richard Heathfield" a écrit...
Charlie Gordon said:
<snip>
>>>>'minor' does not begin to describe how minuscule it is in real life.

By "real life", presumably, you mean "systems that Charlie Gordon has
worked on".

I have probably worked on more different systems than you have.

Yes, you probably have. Er, so what? It is clear that you have never
worked on a system where the conversion of a time_t to a long int
causes a problem. Otherwise, you surely would not have answered
as you did.

Have you ever worked on one?
Your answer below indicates you haven't.
No, it indicates at most that I have no idea whether or not I have worked
on such a system. It is entirely possible that I have done so without
realising it, and never found out because I didn't take liberties with
time_t.
Such systems are either obsolete, hypothetical or doomed.
Hypothetical systems have a nasty habit, occasionally, of becoming real
systems. Planning for the future is part of portable programming, and it's
one reason we have standards in the first place - so that our code
*doesn't* break on new systems.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 26 '07 #37
$)CHarald van D)&k wrote:
>
On Thu, 25 Oct 2007 23:29:47 +0530, santosh wrote:
$)CHarald van D)&k wrote:
On Thu, 25 Oct 2007 10:45:53 -0400, Kenneth Brody wrote:
Let's ignore standard-defined typedefs for the moment. What about:

typedef int my_int_t;
my_int_t my_int_t = 4;
...
printf("%d\n", (my_int_t)-1 );

[...]

If the typedef and the object are defined in the same block (or both at
file scope), this is not allowed. Otherwise, it is.
In that case it's rather surprising that gcc, at least, doesn't produce
any diagnostic even when '-Wall', '-W', '-ansi' and '-pedantic' are
provided.

Do you mean that it doesn't produce any diagnostic in the same block, or
in different blocks? If the former, that's a bug, and I encourage you to
report it. The version of gcc I'm using reports an unconditional error
for this. If the latter, the code is perfectly valid, so why should it
complain? There are enough cases where hiding the outer symbol this way
is intentional to not warn by default, but if you do want the warning,
there's an option just for that (-Wshadow).
Here is my complete source file:

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

typedef int my_int_t;

int main(void)
{
my_int_t my_int_t = 5;

printf("%d\n",(my_int_t)-1);

return 0;
}
==========

$ gcc --version
egcs-2.91.66

$ gcc -Wall -W -ansi -pedantic foo.c
$ ./a.out
4
$

==========

Using MSVC 6.0, no warnings appear with "/W4", and it also produces
the output "4".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Oct 26 '07 #38
Old Wolf wrote:
>
On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.

void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}

A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).
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.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Oct 26 '07 #39
"Kenneth Brody" <ke******@spamcop.neta écrit dans le message de news:
47***************@spamcop.net...
Old Wolf wrote:
>>
On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.

void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}

A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).

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.

--
Chqrlie.
Oct 26 '07 #40
In article <47***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.netwrote:
....
>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;
}

Oct 26 '07 #41
Kenneth Brody <ke******@spamcop.netwrites:
Old Wolf wrote:
>On Oct 26, 3:36 am, Kenneth Brody <kenbr...@spamcop.netwrote:
On the other hand, my compiler gives no warnings (even with warnings
at max) and displays "4" and "-1" for the following code. Of course,
as has been said here numerous times, "works as expected" is a valid
outcome for UB.

void bar(void)
{
printf("%ld\n",(long)( (time_t)-1 ) );
}

A minor point: this causes implementation-defined
behaviour (or raising a signal, in C99) if time_t
is an unsigned long (or a larger unsigned type).

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 26 '07 #42
On Thu, 25 Oct 2007 22:22:47 -0700, Keith Thompson wrote:
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...)
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 27 '07 #43
Army1987 <ar******@NOSPAM.itwrites:
On Thu, 25 Oct 2007 22:22:47 -0700, Keith Thompson wrote:
>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...)
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 27 '07 #44
"Keith Thompson" <ks***@mib.orga écrit dans le message de news:
ln************@nuthaus.mib.org...
Army1987 <ar******@NOSPAM.itwrites:
>On Thu, 25 Oct 2007 22:22:47 -0700, Keith Thompson wrote:
>>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...)

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.

--
Chqrlie.
Oct 27 '07 #45
...
On Fri, 26 Oct 2007 10:52:19 -0400, Kenneth Brody
<ke******@spamcop.netwrote:
Do you mean that it doesn't produce any diagnostic in the same block, or
in different blocks? If the former, that's a bug, and I encourage you to
report it. The version of gcc I'm using reports an unconditional error
for this. If the latter, the code is perfectly valid, so why should it
complain? There are enough cases where hiding the outer symbol this way
is intentional to not warn by default, but if you do want the warning,
there's an option just for that (-Wshadow).
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, not for types. <OTC++ is different here. </)
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
Nov 12 '07 #46

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrey Mishenin | last post by:
While accessing MySQl data base by means of perl the value of variable $! sometimes equals " Resource temporarily unavailable". At the mean time everything works correct. What does that mean?...
2
by: pembed2003 | last post by:
Hi all, I recently saw a piece of code that looks like: class one{public: one(){} }; class two : public virtual one{public: two(){} }; class three : virtual public one{public: three(){} }; ...
6
by: **Developer** | last post by:
What does this mean: External component has thrown an exception. My program crashes after it leaves a subroutine. What I see during debugging is when I press F11 at the End Sub statement
2
by: tony | last post by:
Hello!! I know what an abstract class is which mean that the one of the derived class must define the abstract methods in the abstract class. So all the abstract methods in the abstarct class...
10
by: tony | last post by:
Hello!! I have some demo programs written in C# and they have this construction "" see below. I haven't seen this before so what does it mean ? public bool ShowDropDownButtons { get {...
9
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you...
0
by: steve | last post by:
What does it mean to set a value or April fools all year long :) http://beyondsql.blogspot.com/2008/03/sql-what-does-it-mean-to-set-value.html
14
by: Tony | last post by:
Hello! It says "Another limitation that you need to be aware of is that using the operator == and != are only permitted when comparing a value of a type supplied to a generic type to null....
1
by: rasmidas | last post by:
Hi, Could anyone please let me know what does it mean by the following statement in solaris shell scripting. $PROMPT " Select an option: " read ans db_option=${ans:=0} I...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.