#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
Above is my programme and im supposed to fix it. I've been at it for almost 3 hours now still can figure out the problem. I've got a error and a few warnings.
warning: too few arguments for format
Several warnings for the statements in the mid body
error: expected ')' before numeric constant
this error is for printing two constants.
Printing is a process for mass reproducing text and images using a master form or template. The earliest non-paper products involving printing include cylinder seals and objects such as the Cyrus Cylinder and the Cylinders of Nabonidus.
print("\n\nPrinting two defined constants: "UPPER_S" and "LOWER_S"\n");
would only work if UPPER_S
and LOWER_S
were #define
d as:
#define UPPER_S "999999"
#define LOWER_S "11111"
Alternatively you could use the two following macros to "stringify" the numerical #define
s:
#define _STRINGIFY(s) #s
#define STRINGIFY(s) _STRINGIFY(s)
and then do:
#define UPPER 999999
#define LOWER 11111
fputs("\n\nPrinting two defined constants: "STRINGIFY(UPPER)" and "STRINGIFY(LOWER)"\n", stdout);
I came here looking for the answer to this question and it seems most people just got hung up on syntax errors rather than caring what the actual question is. You can use printf with #define
d values as though they are normal variables. However you must pay close attention to the type.
#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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With