Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #157545
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: I need help understand some c code |
| Date | 2020-12-20 19:56 -0800 |
| Organization | None to speak of |
| Message-ID | <87lfdrbymy.fsf@nosuchdomain.example.com> (permalink) |
| References | <rrp1gb$kle$1@dont-email.me> |
T <T@invalid.invalid> writes:
> I am trying to use Raku's native call to read the
> time in Linux.
>
> I found this C code:
>
> #include<stdio.h>
> #include<time.h>
>
> void main()
This should be "int main(void)". See questions 11.12a and following in
the comp.lang.c FAQ, <http://www.c-faq.com/>.
> {
> time_t t;
> time(&t);
Could also be written as:
t = time(NULL);
If the time() function were being designed today, it would
undoubtedly take no arguments and return a time_t value. Instead,
for obscure historical reasons I won't go into, it takes a
pointer-to-time_t argument and returns a time_t result, both of
which are updated with the result. Passing a null pointer means you can
just use the returned result.
> printf("\n current time is : %s",ctime(&t));
Output should normally *end* with a newline:
printf(" current time is : %s\n", ctime(&t));
But, for bizarre historical reasons, ctime() returns a pointer to a
string that already ends with a newline (I had mercifully forgotten
that until just now), so you could drop the final \n. "man 3 ctime"
> }
>
>
> What is "time_t t" and why the space?
time_t is a type defined in <time.h>.
This:
time_t t;
defines an object (variable) "t" of type "time_t". Similarly, this:
int n;
would define an object "n" of type "int".
> What is "time($t)"?
A syntax error, but "time(&t)" calls the time function, passing the
address of t as its argument. The time function requires an argument of
type time_t* (pointer to time_t). "man 2 time" is likely to show you
the manual for the time function if you're on a Unix-like system.
> I also need to know the length in bit.
Normally you wouldn't need to know that, but if you're interfacing from
another language you might. The size of time_t is
implementation-defined. Its size in bits for a given implementation is
sizeof (time_t) * CHAR_BIT
where CHAR_BIT is a macro defined in <limits.h>. CHAR_BIT is almost
certainly 8, and that "almost" can likely be dropped for any system that
supports Raku (formerly known as Perl 6), but it's good style to use it
anyway. It's probably 64 bits if you're using a reasonably modern
system, but assuming that is a good way to shoot yourself in the foot.
Of course Raku has its own built-in time function, but I presume the
point is to learn how to use Raku's native call functionality, and a
simple example is a good starting point even if a simpler solution would
be better for that example.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
I need help understand some c code T <T@invalid.invalid> - 2020-12-20 18:32 -0800
Re: I need help understand some c code Siri Cruise <chine.bleu@yahoo.com> - 2020-12-20 19:03 -0800
Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 19:56 -0800
Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 20:02 -0800
Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 20:43 -0800
Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 21:06 -0800
Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 21:56 -0800
Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 20:33 -0800
Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 20:51 -0800
Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 21:11 -0800
Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-20 22:10 -0800
Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-20 22:15 -0800
Re: I need help understand some c code Bart <bc@freeuk.com> - 2020-12-21 13:11 +0000
Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-21 13:47 -0800
Re: I need help understand some c code scott@slp53.sl.home (Scott Lurndal) - 2020-12-23 17:18 +0000
Re: I need help understand some c code Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-23 12:05 -0800
Re: I need help understand some c code T <T@invalid.invalid> - 2020-12-21 14:25 -0800
Re: I need help understand some c code Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-21 05:45 +0100
Re: I need help understand some c code Bart <bc@freeuk.com> - 2020-12-21 12:16 +0000
Re: I need help understand some c code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-21 20:03 +0000
csiph-web