enter a decimal number and give me a ASCII character upto 127?

This is a discussion on enter a decimal number and give me a ASCII character upto 127? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> void main() { int x; printf("Enter a decimal number\n",x); printf("The character is %c\n",x); getch (); ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    enter a decimal number and give me a ASCII character upto 127?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
    int x;
    printf("Enter a decimal number\n",x);
    printf("The character is %c\n",x);
    getch ();
    }
    whats wrong with it.it does not give any ascii code.please tell me simple program.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Are you familiar with the concept of a manual? If not, I suggest you look it up. Here, straight from the printf manual, headed under 'conversion specifier', 'c':

    ... the int argument is converted to an unsigned char, and the resulting character is written.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by danishzaidi View Post
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
    int x;
    printf("Enter a decimal number\n",x);
    printf("The character is %c\n",x);
    getch ();
    }
    whats wrong with it.it does not give any ascii code.please tell me simple program.
    What decimal numbers are you entering? try 65 which should print A or 49 which should print 1

    Look at an ASCII code chart... *not all decimal values lead to printable characters!*

    For example if you entered 7 ... your computer should beep at you (ascii bell character)

    The ASCII code was originally created to standardize communications between teletype stations on the old "Telegram" network... It thus includes a number of control characters such as Carriage Return, End of Page, Line Feed, etc. that were used to control the mechanism itself.

    Teleprinter - Wikipedia, the free encyclopedia
    Last edited by CommonTater; 11-06-2011 at 09:31 AM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    my above program is returning me the same value every time.it prints unknown character every time and on each value i give.whats wrong with my program?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    Quote Originally Posted by CommonTater View Post
    What decimal numbers are you entering? try 65 which should print A or 49 which should print 1

    Look at an ASCII code chart... *not all decimal values lead to printable characters!*

    For example if you entered 7 ... your computer should beep at you (ascii bell character)

    The ASCII code was originally created to standardize communications between teletype stations on the old "Telegram" network... It thus includes a number of control characters such as Carriage Return, End of Page, Line Feed, etc. that were used to control the mechanism itself.

    Teleprinter - Wikipedia, the free encyclopedia
    it is not printing anything!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by danishzaidi View Post
    my above program is returning me the same value every time.it prints unknown character every time and on each value i give.whats wrong with my program?
    Well first of all you're not asking for input anywhere...

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
    int x = 0;
    printf("Enter a decimal number\n",x);  // this does not read the keyboard
    
    
    // get number from user here...
    
    
    printf("The character is %c\n",x);
    getch ();
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    Quote Originally Posted by CommonTater View Post
    Well first of all you're not asking for input anywhere...

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
    int x = 0;
    printf("Enter a decimal number\n",x);  // this does not read the keyboard
    
    
    // get number from user here...
    
    
    printf("The character is %c\n",x);
    getch ();
    }
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
    int x;
    char ch;
    printf("Enter a decimal number\n",x);
    scanf("%d",&x);
    if (x<=127)
    ch='x' + '0';
    printf("The character is %c\n",ch);
    
    getch ();
    }
    see this program!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Lose lines 10 and 11 ...
    in line 12... print x, not ch;

    As it is... the character printed will not accurately represent the value entered.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    ok thanks.done.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Printing an int with %c is wrong and probably wont work on a big-endian machine. It really should be casted to char first.
    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. Typing decimal number and cover it binary number!
    By Kazumi in forum C Programming
    Replies: 32
    Last Post: 04-16-2011, 07:21 PM
  2. decimal ascII character
    By byfreak in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2008, 10:36 PM
  3. decimal ascII character
    By byfreak in forum C Programming
    Replies: 2
    Last Post: 05-24-2008, 06:37 PM
  4. Replies: 3
    Last Post: 04-05-2008, 07:44 AM
  5. how to convert decimal number to ASCII code?
    By oie in forum C Programming
    Replies: 11
    Last Post: 11-03-2006, 05:19 PM

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