On Sat, 27 Nov 2021 13:15:43 -0300, Meredith Montgomery wrote:
Here's an example of how to use time(). It uses %ju in printf.
Where can I find a definition of these %-specifiers?
In the printf(2) manpage ("man 2 printf") under "Length Modifier" (for
the "j" flag) and "Conversion specifiers" (for the "u" flag)
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
On Sat, 27 Nov 2021 13:15:43 -0300, Meredith Montgomery wrote:
Here's an example of how to use time(). It uses %ju in printf.
Where can I find a definition of these %-specifiers?
In the printf(2) manpage ("man 2 printf") under "Length Modifier" (for
the "j" flag) and "Conversion specifiers" (for the "u" flag)
man 3 printf.
https://man7.org/linux/man-pages/man3/printf.3.html
Here's an example of how to use time(). It uses %ju in printf. Where
can I find a definition of these %-specifiers?
Here's an example of how to use time(). It uses %ju in printf. Where
can I find a definition of these %-specifiers?
--8<---------------cut here---------------start------------->8---
EXAMPLES
Getting the Current Time
The following example uses the time() function to calculate the time
elapsed, in seconds, since the Epoch, localtime() to convert that value
to a broken-down time, and asctime() to convert the broken-down time
values into a printable string.
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t result;
result = time(NULL);
printf("%s%ju secs since the Epoch\n",
asctime(localtime(&result)),
(uintmax_t)result);
return(0);
}
This example writes the current time to stdout in a form like this:
Wed Jun 26 10:32:15 1996
835810335 secs since the Epoch
--8<---------------cut here---------------end--------------->8---
The type there is uintmax_t, perhaps defined by inttypes.h. When I look
at the manual for inttypes.h, it suggests I should use constants such as PRIxMAX. Under msys2-64, the constant PRIxMAX expands to %ld. But my
printf under MinGW-64 does understand %ju just fine. Where is the
manual I'm missing? Thank you!
(*) The source of the information
I looked at ``man 3p time'' under msys2-64. These manual pages are
installed with the package man-pages-posix. The package itself reports
that it has something to do with https://www.kernel.org/doc/man-pages.
--8<---------------cut here---------------start------------->8---
%pacman -Si man-pages-posix
Repository : msys
Name : man-pages-posix
Version : 2017_a-1
Description : POSIX Manual Pages
Architecture : any
URL : https://www.kernel.org/doc/man-pages
Licenses : custom:posix
Groups : None
Provides : None
Depends On : man
Optional Deps : None
Conflicts With : None
Replaces : None
Download Size : 2.58 MiB
Installed Size : 2.58 MiB
Packager : CI (msys2-autobuild/310a1fa4/1493542190)
Build Date : Tue Nov 23 03:25:13 2021
Validated By : MD5 Sum SHA-256 Sum Signature
--8<---------------cut here---------------end--------------->8---
Looking it up, I find
https://man7.org/linux/man-pages/man2/time.2.html
but the example is not there.
Here's an example of how to use time(). It uses %ju in printf. Where
can I find a definition of these %-specifiers?
--8<---------------cut here---------------start------------->8---
EXAMPLES
Getting the Current Time
The following example uses the time() function to calculate the time
elapsed, in seconds, since the Epoch, localtime() to convert that value
to a broken-down time, and asctime() to convert the broken-down time
values into a printable string.
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t result;
result = time(NULL);
printf("%s%ju secs since the Epoch\n",
asctime(localtime(&result)),
(uintmax_t)result);
return(0);
}
This example writes the current time to stdout in a form like this:
Wed Jun 26 10:32:15 1996
835810335 secs since the Epoch
--8<---------------cut here---------------end--------------->8---
The type there is uintmax_t, perhaps defined by inttypes.h. When I look
at the manual for inttypes.h, it suggests I should use constants such as PRIxMAX. Under msys2-64, the constant PRIxMAX expands to %ld. But my
printf under MinGW-64 does understand %ju just fine. Where is the
manual I'm missing? Thank you!
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
On Sat, 27 Nov 2021 13:15:43 -0300, Meredith Montgomery wrote:
Here's an example of how to use time(). It uses %ju in printf. Where
can I find a definition of these %-specifiers?
In the printf(2) manpage ("man 2 printf") under "Length Modifier"
(for the "j" flag) and "Conversion specifiers" (for the "u" flag)
Thank you. I found in the manuals in GNU systems, but my printf is in section 3. Section 2 is usually for system calls, I guess. Why is your printf in section 2? What system are you using?
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
On Sat, 27 Nov 2021 13:15:43 -0300, Meredith Montgomery wrote:
Here's an example of how to use time(). It uses %ju in printf. Where
can I find a definition of these %-specifiers?
In the printf(2) manpage ("man 2 printf") under "Length Modifier"
(for the "j" flag) and "Conversion specifiers" (for the "u" flag)
Thank you. I found in the manuals in GNU systems, but my printf is in section 3. Section 2 is usually for system calls, I guess. Why is your printf in section 2? What system are you using?
On 11/27/21 11:15 AM, Meredith Montgomery wrote:
Here's an example of how to use time(). It uses %ju in printf. Where
can I find a definition of these %-specifiers?
--8<---------------cut here---------------start------------->8---
EXAMPLES
Getting the Current Time
The following example uses the time() function to calculate the time
elapsed, in seconds, since the Epoch, localtime() to convert that value
to a broken-down time, and asctime() to convert the broken-down time
values into a printable string.
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t result;
result = time(NULL);
printf("%s%ju secs since the Epoch\n",
asctime(localtime(&result)),
(uintmax_t)result);
return(0);
}
While the C standard allows time_t to be any real type, Unix requires it
to be an integer type. That's the only thing you can portably assume
about it; in particular, you don't know whether or not it's a signed
type. However, that's (barely) enough to print it out reliably. You can safely convert a value of any signed integer type to intmax_t without
change of value, and you can safely convert a value of any unsigned
integer type to uintmax_t. However, if you're willing to accept a change
of value, a value of any integer type can be safely converted to
uintmax_t, which is what they're relying upon.
This example writes the current time to stdout in a form like this: >>
Wed Jun 26 10:32:15 1996
835810335 secs since the Epoch
--8<---------------cut here---------------end--------------->8---
The type there is uintmax_t, perhaps defined by inttypes.h. When I look
at the manual for inttypes.h, it suggests I should use constants such as
PRIxMAX. Under msys2-64, the constant PRIxMAX expands to %ld. But my
printf under MinGW-64 does understand %ju just fine. Where is the
manual I'm missing? Thank you!
The PRI* macros were designed to solve the problem of not knowing which length modifier to use when working with the size-named types, which
could be typedefs for different standard types on different
implementations of C. They also cover a couple of other integer
typedefs: [u]intmax_t and ptrdiff_t.
But C99, which was the version that introduced the size-named types,
also introduced new length modifiers: 'j' for [u]intmax_t, 'z' for
size_t, and 't;' for ptrdiff_t. The 'j' and 't' length modifiers render
the corresponding inttypes macros unnecessary - I've no idea why they
added both - the C99 Rationale document is no help - it doesn't mention either change. "%ju" is much simpler and easier to use than "%" PRIuMAX.
On Sat, 27 Nov 2021 13:15:43 -0300, Meredith Montgomery wrote:
Here's an example of how to use time(). It uses %ju in printf. Where
can I find a definition of these %-specifiers?
In the printf(2) manpage ("man 2 printf") under "Length Modifier"
(for the "j" flag) and "Conversion specifiers" (for the "u" flag)
Meredith Montgomery <mmontgomery@levado.to> writes:
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
On Sat, 27 Nov 2021 13:15:43 -0300, Meredith Montgomery wrote:
Here's an example of how to use time(). It uses %ju in printf. Where >>>> can I find a definition of these %-specifiers?
In the printf(2) manpage ("man 2 printf") under "Length Modifier"
(for the "j" flag) and "Conversion specifiers" (for the "u" flag)
Thank you. I found in the manuals in GNU systems, but my printf is in
section 3. Section 2 is usually for system calls, I guess. Why is your
printf in section 2? What system are you using?
It isn't. I speak a very obscure dialect of English in which the number three is spelled "2". Yeah, that's it.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 229:56:45 |
Calls: | 6,624 |
Calls today: | 6 |
Files: | 12,171 |
Messages: | 5,319,116 |