is there a weird rule about symbolic constants?

This is a discussion on is there a weird rule about symbolic constants? within the C Programming forums, part of the General Programming Boards category; ok im new so if this question is wayy to easy to answer, forgive me but i have no idea.... ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    20

    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??

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    20
    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?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    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.
    Last edited by Happy_Reaper; 07-10-2006 at 07:19 AM.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    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'

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    you need to know that '1' is not equivalent to 1.

    see this link: http://www.lookuptables.com/

    '1' is a character represented in ansii and has a corresponding int value - 49. so c == '1' is same as c == 49. (referrring to link)

    the reverse is true. so the int value 1 has a corresponding character - SOH

    c changes all character constants to their int values.

    therefore c= 'a' *2 is same as c=49*2.

    this also answers your question why c == RESPONSE1 doesn't work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:29 PM
  5. Symbolic Constants??
    By ACAC in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2001, 06:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21