Thread: getchar() probelm?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    getchar() probelm?

    can i know what does it do actually


    while (( c=getchar())!=EOF)

    if ( c>= '0' && c <= '9')


    ++ndigit{c-'0'];

    when ever i type the numerics into the arrays

    it will enter into the ndigits

    so what is exactly c-'0' here

  2. #2
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    please use code tags. Read the sticky at the top of this forum. Otherwise your code is a mess and hard to understand.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    please help?

    can i know what does it do actually


    while (( c=getchar())!=EOF)
    if ( c >= '0' && c <= '9')
    ++ndigit [c-'0'];

    when ever i type the numerics into the arrays

    it will enter into the ndigits

    so what is exactly [c-'0'] here

    what i know is c is character entered through the console

    and then what is happening next?

  4. #4
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    PLEASE READ THIS

    http://cboard.cprogramming.com/showthread.php?t=25765

    If you cannot be bothered to read the guidelines on posting code when asked to do so, then most people cannot be bothered to help you either. Also your code lacks formatting. Please remember this in future.

    Code:
    while (( c=getchar())!=EOF)
    if ( c >= '0' && c <= '9')
    ++ndigit [c-'0'];
    [c-'0'] is converting the ASCII character you typed into an integer value by subtracting the ASCII code for '0' from the character typed.

    Hope that helps.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If you still don't understand [c-0], this might be helpful to you.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so what is exactly [c-'0'] here
    Digits in C's standard character set are required to be contiguous in value. For example, in ASCII the digits 0 through 9 would be 48 through 57. What happens if you take the value of a digit, say '2' (50) and subtract '0' (48) from it? 50 - 48 == 2, so you get the numeric value that the character represents (2) instead of the underlying numeric value of the character (50).
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM