c - sprintf 转换在格式末尾缺少类型

标签 c printf

我在我的 C 程序中使用 sprintf,我正在尝试格式化一个字符串。

这是我的代码:

sprintf(string, "| %-5%lu | %-9%s | %-9%s | %c | %-4%f |", currentPtr->s.SID, currentPtr->s.lname, currentPtr->s.fname, currentPtr->s.initial, currentPtr->s.GPA);

我不断收到警告:

warning: conversion lacks type at end of format [-Wformat]

为什么我会收到此警告?

编辑:很抱歉,我应该添加我的结构,以便你们知道我的变量是什么类型。

结构:

typedef struct student {
    char lname[ 10 ], initial, fname[ 10 ];
    unsigned long SID;
    float GPA;
} SREC;

typedef struct node {
    SREC s;
    struct node *nextPtr;
} Node;

typedef Node *NodePtr;

currentPtr 是一个 NodePtr

Edit2:解决了,谢谢大家! :)

最佳答案

"| %-5%lu | %-9%s | %-9%s | %c | %-4%f |"
      ^        ^       ^            ^

字段宽度和类型说明符之间有额外的 %。例如,%-9%s 不是有效的格式说明符。它应该是 %-9s(即中间没有额外的 %)。

printf 格式的语法是这样的(来自 Wikipedia ):

%[参数][标志][宽度][.精度][长度]类型

所以除了 %type 之外的所有内容都是可选的,中间没有 %

P.S: 请注意,parameter 是 POSIX 扩展,不是 C 标准。

关于c - sprintf 转换在格式末尾缺少类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43692283/

相关文章:

c - C Primer Plus 6th 中的 C 语言数据类型

链接时出现c构建错误

bash - bash 中数字的表示和 bash 中十六进制数的 printf

c - 为什么在Eclipse中的printf之前执行scanf?

c - 如何创建一个普通的 win32 编辑控件?

c - 使用 execve 运行存在于 PATH 环境中的可执行文件

c - 用 C 读/写多数组数据结构

c - printf() 无格式字符串打印字符和整数数组 --> 垃圾

c - 数组只打印零

c - Printf 只打印字符串中的第一个单词?