打印时出现 C 程序错误

标签 c console-application

我对 C 编程非常陌生,我正在编写一个基本代码,用于收集名字和姓氏、工作时间、小时工资,然后返回姓氏、名字和工时 * 工资:

    char first_name;
    char last_name;

    printf("\n%s, %s: %.2lf\n", last_name, first_name, total_payment);

我在编译时收到的错误是:

payroll.c:22:13: warning: format '%s' expects argument of type char *' but argument 2 has type 'int' [-Wformat=]
printf("\n%s, %s: %.2lf\n", last_name, first_name, total_payment);
          ^

payroll.c:22:13: warning: format '%s' expects argument of type char *' but argument 3 has type 'int' [-Wformat=]
printf("\n%s, %s: %.2lf\n", last_name, first_name, total_payment);
              ^

我不明白这个错误。 char * 是指针吗?为什么它不返回列出的第一个 char 变量,然后返回第二个,在 .2 浮点 int 之前?我也在使用 nano,因此无法像往常一样进行调试。 (推荐的 IDE 也会非常有帮助。)

最佳答案

您已将first_namelast_name 变量声明为char 数据类型。它们应声明为 char* 并分配内存或将其声明为所需大小的 char[]

同时更改您的 scanf 语句以删除 &,同时捕获用户对变量的输入,因为这些变量已经保存了地址。

关于打印时出现 C 程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991943/

相关文章:

c - scanf() 在文件处理中不起作用?

c - 枚举类型及其在 C 中的链接

c# - 无法使用 EF6 插入\更新

c# - 如何在 VS2010 C# 控制台应用程序中将我的 App.config 编译成我的 exe?

c++ - if (flag==0) 或 if (0==flag) 哪个会执行得更快?

c - 结构和 union 名称冲突

c - 使用 C 合并 k 个排序数组

c# - 如何在我的 C# 控制台应用程序中使用命令行参数?

c# - 知道为什么这个计算器控制台应用程序不起作用吗?

c# - 创建 GraphQLHttpClient 时出现 GraphQLClient 错误 : IGraphQLWebsocketJsonSerializer