dealing with ints stored in char variables

This is a discussion on dealing with ints stored in char variables within the C Programming forums, part of the General Programming Boards category; Hello all, I'm learning C and I am doing a code review. Now I am looking at a line I ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    dealing with ints stored in char variables

    Hello all,

    I'm learning C and I am doing a code review. Now I am looking at a line I have no clue what it's doing... could you guys help me out? It says:
    int number = (array[3] - '0') - 1; // array is of the type char * and in the position 3 will have a 1,2,3 or 4.
    The part I don't get is the "- '0'" part...

    Thank you!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's a common syntax to remove the offset for digits, from ascii values.

    Using that, the char '4', becomes the digit 4. It works for all digits because they are all consecutively valued by the ascii numbering system.

    If you don't have a copy of the ascii table, you should google it, and grab one. You'll find it very useful indeed.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    C uses ASCII character values:

    ASCII Table / Extended ASCII Codes

    Single quoted characters stand in for their ascii value, eg
    Code:
    if ('0' == 48)
    is true.

    You can see the ascii values this way:
    Code:
    char string[]="hello world";
    int i;
    for (i=0;i<11;i++) printf("%d ",string[i]);
    As Adak points out, using ascii values are a surprisingly fundamental aspect of C programming.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    Code:
    main()
    {
            char arr[]={1,2,3,4};
            int number=(arr[3]-'0')-1;
            printf("%d\n",number);
    }
    '0' will take the ASCII value of zero.
    ASCII value of zero is 48.
    In the above code the value of arr[3] is 4.
    So (4-48)-1 is equal to (-44)-1=>-45.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    integer to character conversion by ASCII standard

    It is Just default type conversion. Normally character values will convert into integer based on its ASCII value.

    For Ex: '0' will be taken as 48 in ASCII value. So C follows this ASCII standard for conversion.

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    To be perfectly pedantic, C doesn't require ASCII. It does, however, require that '0', '1', .. '9' be consecutive, so the idiom will always work.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by sganesh View Post
    For Ex: '0' will be taken as 48 in ASCII value. So C follows this ASCII standard for conversion.
    No it doesn't. Your implementation does. The standard says nothing about '0' = 48.

    Quote Originally Posted by MK27
    C uses ASCII character values:
    No again. Your implementation does...

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    5
    thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM

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