Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What does ((time_t)-1) mean?

Reply
Thread Tools

What does ((time_t)-1) mean?

 
 
loudking
Guest
Posts: n/a
 
      10-24-2007
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!

 
Reply With Quote
 
 
 
 
Pietro Cerutti
Guest
Posts: n/a
 
      10-24-2007
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
 
Reply With Quote
 
 
 
 
Martin Wells
Guest
Posts: n/a
 
      10-24-2007
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


 
Reply With Quote
 
loudking
Guest
Posts: n/a
 
      10-24-2007
> 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) {
.......

 
Reply With Quote
 
Martin Wells
Guest
Posts: n/a
 
      10-24-2007
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

 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      10-24-2007
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
 
Reply With Quote
 
James Kuyper Jr.
Guest
Posts: n/a
 
      10-24-2007
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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-24-2007
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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-24-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      10-24-2007
In article <(E-Mail Removed). com>,
loudking <(E-Mail Removed)> wrote:
>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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET 2.0 ASPx Page does not load, but HTM does prabhupr@hotmail.com ASP .Net 1 02-08-2006 12:57 PM
Button OnClick does not fire on first postback, but does on second Janet Collins ASP .Net 0 01-13-2006 10:08 PM
Does the 2.0 Framework come out when Visual Studio .NET 2005 does? needin4mation@gmail.com ASP .Net 3 10-07-2005 12:55 AM
CS0234 Global does not exist ... but it genuinely does Bill Johnson ASP .Net 0 07-08-2005 06:34 PM
Does no one else think microsoft does a poor job? =?Utf-8?B?SmVyZW15IEx1bmRncmVu?= Wireless Networking 2 11-20-2004 12:17 AM



Advertisments