Sujet : Re: Suggested method for returning a string from a C program?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 19. Mar 2025, 19:56:00
Autres entêtes
Organisation : None to speak of
Message-ID : <87wmcku9xb.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6
User-Agent : Gnus/5.13 (Gnus v5.13)
James Kuyper <
jameskuyper@alumni.caltech.edu> writes:
On 3/19/25 13:23, DFS wrote:
...
int64_t n = 0, max = 0, thismax = 0;
...
printf("\nmax n = %lld reached at input = %d\n", max, input);
...
You'll get compilation warnings about the printf specifier used with
int64_t.
>
Not if you use the correct specifier:
#include <inttypes.h>
printf("\nmax n = %" PRId64 " reached at input = %d\n", max, input);
Or you can use long long and "%lld" (that's what I did).
long long is at least 64 bits and is guaranteed to exist (in C99
and later, and the autotester uses "gcc -std=c99"). int64_t is
exactly 64 bits and is not guaranteed to exist. (In every C99 or
later implementation I'm aware of, long long and int64_t are both
exactly 64 bits.)
Another alternative for output is to cast to intmax_t and use "%jd".
(Why "j"? It was available.) Input with *scanf() is slightly
trickier; you can't just cast the pointer argument. (And the
*scanf() functions have undefined behavior on numeric input if the
input is out of range. fgets() with strto*() is a safer alternative
if you can't be sure what the input is going to look like.)
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */