5
#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 

此错误用于打印两个常量。

4

5 回答 5

10
print("\n\nPrinting two defined constants: "UPPER_S" and "LOWER_S"\n");

仅当UPPER_S且d 为LOWER_S#define

#define UPPER_S  "999999"
#define LOWER_S  "11111"

或者,您可以使用以下两个宏来“字符串化”数字#defines:

#define _STRINGIFY(s) #s
#define STRINGIFY(s) _STRINGIFY(s)

然后做:

#define UPPER 999999
#define LOWER  11111

fputs("\n\nPrinting two defined constants: "STRINGIFY(UPPER)" and "STRINGIFY(LOWER)"\n", stdout);
于 2012-10-11T16:27:20.423 回答
6

我来这里是为了寻找这个问题的答案,似乎大多数人只是被语法错误挂断,而不是关心实际问题是什么。您可以将 printf 与#defined 值一起使用,就好像它们是普通变量一样。但是,您必须密切注意类型。

#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;
}
于 2014-07-29T16:32:31.280 回答
2

printf("grade = %d, grade\n");

期待 '%d' 的参数。它的形式应该是

printf("grade = %d\n", grade);
于 2012-10-11T16:21:02.540 回答
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);
于 2012-10-11T16:21:15.580 回答
0

一种方法是将常量定义的变量值分配给局部变量值。这样它将在 printf("%d", X); 中很有用。方法。

但是,您可以使用 const int EXAMPLE=12,然后可以在标准 printf 方法中使用 EXAMPLE,例如 printf("%d\n", EXAMPLE);

于 2016-11-11T01:29:41.877 回答