#include<stdio.h>
#define UPPER 999999
#define LOWER 11111
int main(void)
{
// Local Declarations
double price = 89.99;
char grade = 'B';
int age = 97;
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c, age\n");
printf("grade = %d, grade\n");
printf("price = %f, price\n\n");
printf("Using both printf and scanf\n");
printf("Enter a new value for age: ");
scanf("%d", &age);
printf("Enter a new value for grade: ");
scanf("%c", &grade);
printf("Enter a new value for price: ");
scanf("%lf", &price);
printf("Print the new values\n");
printf(" age = %d \n", age);
printf("grade = %c\n", grade);
printf("price = %lf\n\n", price);
print("\n\nPrinting two defined constants: "UPPER" and "LOWER"\n");
print("UPPER = %08d\n", UPPER);
print("LOWER = %08d\n", LOWER);
return 0;
} // end of main
以上是我的程序,我应该修复它。我已经待了将近3个小时,现在仍然可以找出问题所在。我有一个错误和一些警告。
warning: too few arguments for format
中间声明的几个警告
error: expected ')' before numeric constant
此错误用于打印两个常量。
答案 0 :(得分:5)
print("\n\nPrinting two defined constants: "UPPER_S" and "LOWER_S"\n");
仅在UPPER_S
和LOWER_S
为#define
时才有效
#define UPPER_S "999999"
#define LOWER_S "11111"
或者,您可以使用以下两个宏来对字符#define
进行字符串化:
#define _STRINGIFY(s) #s
#define STRINGIFY(s) _STRINGIFY(s)
然后再做
print("\n\nPrinting two defined constants: "STRINGIFY(UPPER)" and "STRINGIFY(LOWER)"\n");
答案 1 :(得分:4)
我来到这里寻找这个问题的答案,似乎大多数人只是挂了语法错误,而不是关心实际问题是什么。您可以将printf与#define
d值一起使用,就像它们是正常变量一样。但是你必须密切注意类型。
#define HEXNUM 0xA8
#define NUMBER 129
#define STRING "somestring"
#include <stdio.h>
int main(void) {
printf("hex number: %x\n", HEXNUM);
printf("number: %d\n", NUMBER);
printf("string: %s", STRING);
return 0;
}
答案 2 :(得分:2)
像
这样的事情printf("grade = %d, grade\n");
期待'%d'的参数。它应该是
的形式printf("grade = %d\n", grade);
答案 3 :(得分:1)
你需要把你的printf args放在引号之外。
电流:
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c, age\n");
printf("grade = %d, grade\n");
printf("price = %f, price\n\n");
应该是:
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c\n", age);
printf("grade = %d\n", grade);
printf("price = %f\n\n", price);
答案 4 :(得分:1)
一种方法是将常量定义的变量值分配给局部变量值。这样它在printf中很有用(&#34;%d&#34;,X);方法
但是,您可以使用const int EXAMPLE = 12,然后可以在标准printf方法中使用EXAMPLE,例如, printf(&#34;%d \ n&#34;,示例);