一些背景知识:例如,如果我想使用 forscanf()将字符串转换为标准整数类型,例如uint16_t,我会使用SCNu16from <inttypes.h>,如下所示:
#include <stdio.h>
#include <inttypes.h>
uint16_t x;
char *xs = "17";
sscanf(xs, "%" SCNu16, &x);
但是更不常见的整数类型pid_t没有这样的东西。仅支持普通整数类型<inttypes.h>。要以另一种方式转换为可移植printf()的 a pid_t,我可以将其转换为intmax_t并使用PRIdMAX,如下所示:
#include <stdio.h>
#include <inttypes.h>
#include <sys/types.h>
pid_t x = 17;
printf("%" PRIdMAX, (intmax_t)x);
但是,似乎没有办法可移植scanf()到pid_t. 所以这是我的问题:如何便携?
#include <stdio.h>
#include <sys/types.h>
pid_t x;
char *xs = 17;
sscanf(xs, "%u", &x); /* Not portable! pid_t might not be int! /*
我想到了scanf()ing intmax_t,然后在强制转换为 之前检查该值是否在pid_t's 范围内pid_t,但似乎没有办法获得 的最大值或最小值pid_t。