-
How to use Macros??
Hey peoples im new to macros and i have seem to have gotten the notation for my VOLUME macro, however i dont know what to do for the PRINTSTR macro which is supposed to ask a string for you to input which will then be outputted onto the next line.
Code:
#include <stdlib.h>
#include <stdio.h>
int main()
{
#define VOLUME(r) ((4.0 /3) * 3.14 * (r)*(r)*(r))
#define PRINTSTR(str) printf("%s")
char str[100];
float r;
printf("Please enter the radius of a sphere \n");
scanf("%f", &r);
printf("The volume of the sphere is: %.2f\n", VOLUME(r));
printf("Enter a string you want to print \n");
scanf("%s", str);
PRINTSTR(str);
return 0;
}
-
You went wrong into the printf function, replace it with:
Code:
#define PRINTSTR(str) printf("%s", str);
-
Whenever you have doubts in the macro just give
Code:
gcc -E filename.c
may be gcc or cc
This will show you how the macros are expanded
-
This gets tedious on most files as the preprocessor also includes everything. Checking the macros and warnings is enough most of the time.