我对 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_name
和last_name
变量声明为char
数据类型。它们应声明为 char*
并分配内存或将其声明为所需大小的 char[]
。
同时更改您的 scanf 语句以删除 &
,同时捕获用户对变量的输入,因为这些变量已经保存了地址。
关于打印时出现 C 程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991943/