c - 请问,为什么当我运行此代码时,会打印出额外的 "n"?

标签 c

我正在尝试使用此代码获取 pid。但是当我运行编译的代码时。我收到一条错误消息“警告:格式指定类型“unsigned long”,但参数类型为“pid_t”(又名“int”)[-Wformat]”。

当我将格式说明符更改为“%lu”时,它打印时没有多余的字符。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
pid_t pid = getpid();

printf("pid: %lun", pid);
}

我期望 pid 为“60316”。我得到的 pid 为“60316n”。

最佳答案

n 打印的原因与 p 打印的原因相同。 n 不是说明符的一部分。

      vvv  --- specifier for unsigned long 
"pid: %lun"
 ^^^^^   ^ --- non-specifier characters
<小时/>

旁白:

pid_t 数据类型是 signed integer能够表示进程ID的类型

参见What is the correct printf specifier for printing pid_t?

关于c - 请问,为什么当我运行此代码时,会打印出额外的 "n"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240207/

相关文章:

C函数不能正常工作

c++ - 创建大图像

c - 当在 0 索引处打印链表值时,会出现段错误。

c - 什么更有效率?

c - 保存 struct c 的指针

c - Ising 1-Dimensional C - 程序

c - 定义 C 结构时出现问题...(又名挫败感)

c - tsp 使用动态规划

c - 通用链表库中的列表创建功能出现问题

c - 在 C 中修改函数中的变量