(/EDIT
I'm sorry, I've just spotted a mistake in the code - I left a point in the macro. I'm searching for a way to delete the thread but I can't seem to find one. To moderators: feel free to delete it and sorry for posting.)
Hi all, this is my first post (and for a question like this I felt like searching a C forum, rather than posting in such places as StackOverflow).
I'm playing with C while reading a manual (K&R, 2nd edition).
I'm at symbolic names, i.e. the #define directive.
I know that names are mostly used with constant values and expressions, but I've just read in the manual one line saying it can replace the name with any sequence of characters, so I was checking just that (Code::Blocks IDE, gcc compiler).
First example (works as expected)
Here I'm defining a name for a "piece" of operation, the multiplication by 3.
It works as expected, assigning 6 (actually, replacing with "2 * 3" and hence assigning 6) to a and then printing its value of 6.
Code:
#include <stdio.h>
#define MY_PART * 3.
main()
{
int a = 2 MY_PART;
printf("%d", a);
}
Second Example (doesn't work as expected)
If I use, though, the name as an argument to the printf function, outputting an integer doesn't seem to perform the expected multiplication (2 * 3).
Even odder (at least to me), outputting a float does.
Code:
#include <stdio.h>
#define MY_PART * 3.
main()
{
printf("%d\n", a MY_PART); // prints 0 (?)
printf("%f\n", a MY_PART); // prints 6
}
Why the integer printing doesn't work? And why the float one does?
Thanks.