KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Some background: If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t , I’d use SCNu16 from <inttypes.h> , like this: #include <stdio.h> #include <inttypes.h> uint16_t x; char *xs = "17"; sscanf(xs, "%" SCNu16, &x); But a more uncommon integer type like pid_t does not have any such thing; only the normal integer types are supported by <inttypes.h> . To convert the other way, to portably printf() a pid_t , I can cast it to intmax_t and use PRIdMAX , like this: #include <stdio.h> #include <inttypes.h> #include <sys/types.h> pid_t x = 17; printf("%" PRIdMAX, (intmax_t)x); However, there does not seem to be a way to portably scanf() into a pid_t . So this is my question: How to do this portably? #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! /* I thought of scanf() ing to an intmax_t and then checking that the value is within pid_t ’s limits before casting to pid_t , but there does not seem to be a way to get the maximum or minimum values for pid_t .
Tags (comma-separated)
Save Edits
Cancel