Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing #define'd constants

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

like image 579
Kexy Kathe Avatar asked Oct 11 '12 16:10

Kexy Kathe


People also ask

What is called printing?

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.


2 Answers

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

would only work if UPPER_S and LOWER_S were #defined as:

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

Alternatively you could use the two following macros to "stringify" the numerical #defines:

#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);
like image 115
alk Avatar answered Oct 11 '22 14:10

alk


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 #defined 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;
}
like image 33
user3817250 Avatar answered Oct 11 '22 13:10

user3817250