Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以下代码:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t mypid = getpid(); write(1, &mypid, sizeof(pid_t)); return 0; }
打印乱码而不是实际的 pid。为什么?
write(..不会打印格式化文本,而是直接将二进制输出到文件描述符。
write(..
只需使用printf或fprintf:
printf
fprintf
fprintf(stdout, "%d", (int) mypid);