is there a weird rule about symbolic constants?

• 07-10-2006
i_can_do_this
is there a weird rule about symbolic constants?
ok im new so if this question is wayy to easy to answer, forgive me but i have no idea....

the book im learning from says that symbolic constants are exact same values the thing they representing except you can use a different name to represent that value.. but how come this:

Code:

```#include <stdio.h> #define RESPONSE1 1 #define RESPONSE2 2 int main(void) {     int c;         printf("press 1 for a response, 2 for a different one\n");     while ((c = getchar()) != EOF){         if(c == '1')           printf("hehehhehe.....\n");         if(c == '2')           printf("fool...\n");         } }```
i wrote this while i was bored but found out that "c == '1'" outputs printf but not "c == RESPONSE1"??? shouldnt it be the same thing??
• 07-10-2006
twomers
c is an integer, and the vale of '1' and '2' is 31 and 32 in hex respectively. So, what you're wanting is:

Code:

```if ( c == 1 ) // do stuff if ( c == 2 ) // do stuff```
Code:

`and (c = getchar() - '0' )`
to be coherent with logic.
• 07-10-2006
i_can_do_this
no i meant that only c == '1' works, not c == 1 or c == RESPONSE1.

i even tried it out with a new program:
Code:

```#include <stdio.h> int main(void) {     int c;     while((c = getchar()) != EOF)     if(c == 2)       printf("hehe\n"); }```
even with this, i tried if(c == 2) and no printf shows, only c == '2'.... y is it like this?
• 07-10-2006
Happy_Reaper
the reason as to why the above code doesn't work is that although getchar() returns an int, it returns the integer value of the character pressed. So if you were to press 1, c would get the value 49, as twomers mentionned.

Try scanf("%d",&c). That should work.
• 07-10-2006
glo
2 is an integer value, unlike '2', which holds the integer value for the character '2'
so what you really want is to

#define RESPONSE1 '1'
• 07-10-2006
peterchen
you need to know that '1' is not equivalent to 1.