Ted*_*ddy 11 c string integer scanf c99
一些背景:如果我想用于,例如,scanf()
将字符串转换为标准整数类型,比如uint16_t
,我将使用SCNu16
from <inttypes.h>
,如下所示:
#include <stdio.h>
#include <inttypes.h>
uint16_t x;
char *xs = "17";
sscanf(xs, "%" SCNu16, &x);
Run Code Online (Sandbox Code Playgroud)
但是一个更不常见的整数类型就像pid_t
没有任何这样的东西; 只支持普通的整数类型<inttypes.h>
.要转换的另一种方式,可移植printf()
一个pid_t
,我可以把它转换为intmax_t
和使用PRIdMAX
,就像这样:
#include <stdio.h>
#include <inttypes.h>
#include <sys/types.h>
pid_t x = 17;
printf("%" PRIdMAX, (intmax_t)x);
Run Code Online (Sandbox Code Playgroud)
然而,似乎没有办法可移植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! /*
Run Code Online (Sandbox Code Playgroud)
我想到了scanf()
一个intmax_t
然后pid_t
在转换之前检查该值是否在s限制范围内pid_t
,但似乎没有办法获得最大值或最小值pid_t
.
有一个强大的便携式解决方案,即使用strtoimax()
和检查溢出.
也就是说,我解析一个intmax_t
,检查错误strtoimax()
,然后pid_t
通过转换它并将其与原始intmax_t
值进行比较来查看它是否"适合" .
#include <inttypes.h>
#include <stdio.h>
#include <iso646.h>
#include <sys/types.h>
char *xs = "17"; /* The string to convert */
intmax_t xmax;
char *tmp;
pid_t x; /* Target variable */
errno = 0;
xmax = strtoimax(xs, &tmp, 10);
if(errno != 0 or tmp == xs or *tmp != '\0'
or xmax != (pid_t)xmax){
fprintf(stderr, "Bad PID!\n");
} else {
x = (pid_t)xmax;
...
}
Run Code Online (Sandbox Code Playgroud)
不可能使用scanf()
,因为(正如我在评论中所说)scanf()
不会检测到溢出.但我说错误的strtoll()
相关功能都没有intmax_t
; strtoimax()
的确!
strtoimax()
除非您知道整数类型的大小(pid_t
在本例中),否则它也不会使用任何其他内容.