Thread: is it possible to assign a number to a character as a constant?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    33

    is it possible to assign a number to a character as a constant?

    What I want to do is have printf print a variable which will randomly generate either the number 1 or 2. But I want the number 1 and 2 to be printed as X and O, respectively.

    Code:
    void printRndm(int input)    //input is assigned to randomly generate either 1 or 2
    {
       printf("%d", input);
    }
    So would there be some type of constant declaration I could put in this function that has it print 1 or 2 as X or O?


    edit:
    title should read: is it possible to assign a character to a number as a constant?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Tricky version:
    Code:
    printf("%c", (input == 1)?'X':'O');
    Tricky version 2:
    Code:
    char table[2] = { 'X', 'O' };
    printf("%c", table[input-1]);
    Simpler to understand version:
    Code:
    if (input == 1)
       printf("X");
    else
       pritnf("O");
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Code:
    void printRndm(int input)
    {
        switch(input)
       {
            case 1: putchar('X'); break;
            case 2: putchar('O'); break;
       }
    }

  4. #4
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    One more
    Code:
    void printRndm(int input)
    {
       putchar(70+9*input);
    }
    Depending on this
    79 -> O -> 1
    88 -> X -> 2

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Depending on this
    79 -> O -> 1
    88 -> X -> 2
    That's obfuscation, in my opinion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Quote Originally Posted by laserlight View Post
    That's obfuscation, in my opinion.
    It is only consecrated on this specific problem and nowhere else.
    Of course this is no algorithm neither technique and not easy-readable. It's a mess but still a thought.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by ch4 View Post
    It is only consecrated on this specific problem and nowhere else.
    Of course this is no algorithm neither technique and not easy-readable. It's a mess but still a thought.
    It's also not portable to non-ASCII systems.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by ch4 View Post
    One more
    Code:
    void printRndm(int input)
    {
       putchar(70+9*input);
    }
    Depending on this
    79 -> O -> 1
    88 -> X -> 2
    That is not very unicode compliant... then again, my point is moot since putchar() isn't unicode compliant either....

    Example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
     
    static char printable_characters[256];
     
    void printRndm(int input){
      static int init = 0;
    
      if(!init)  {
        memset(printable_characters, '0', sizeof(printable_characters));
        printable_characters[1] = '1';
        init = 1;
      }
    
      putchar(printable_characters[(usigned char)input]);
    }

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ch4 View Post
    One more
    Code:
    void printRndm(int input)
    {
       putchar(70+9*input);
    }
    Buzz ... FAIL!

    If you ever have to look at an ASCII chart to work out what a program outputs then the code is just plain wrong.

    I'd probably prefer something like matsp's version 2, since the table can be declared once and used throughout the program, and if you want to change the chars to lowercase for example, you only have to change one place - the table.
    Code:
    const char* table[] = { "X", "O" };
    
    printf(table[input-1]);
    Last edited by iMalc; 10-21-2008 at 01:15 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. number to a character
    By steven in forum C Programming
    Replies: 2
    Last Post: 09-06-2001, 02:45 PM