Thread: Question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    54

    Question

    Hi

    Say i want to declare a variable called menu_number (which is obviously used for menu number ) and it needs to be able to hold numbers such as 1,2, etc and also it should be able to hold characters e.g. Q or ?

    when declaring do i need to say int menu_number or char menu_number

    or possibly is there any other decleration that can hold both characters and numbers?????

    ta

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    datatype int is only for integer values, char is of course for any single character, so i imagine char would be the datatype to use.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    thank you nadroj

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your welcome

    if your mixing numbers and characters, make sure to always use single quotes around whatever the value is, whether number or character. see my short example:
    Code:
    #include<stdio.h>
    
    int main()
    {
        char menu;
        
        menu = 1; // menu now holds the ASCII value of the integer 1, a smiley face when printed
        menu = '1'; // menu now holds the number one '1'
        
        menu = 97; // menu now holds the ASCII value of number 97, the charcter 'a'
        menu = 'a'; // same as previous statement
        
        menu = '5';
        if (menu == 5) // this will be false, because the _value_ of menu is number '5', not ASCII value 5
             printf("menu is '5'\n");
        
        return 0;
    }
    hope it helps

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    very helpful indeed

    i have another question though. the thread is called "closest value" can you please have a look?

    ta

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM