Thread: Query related to storing ASCII value for characters

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    Query related to storing ASCII value for characters

    Can you help to understand what will store in "value" variable in the following two cases. When we assign a character then ascii value will be stored, but if we assign value (sat 0 ,1 etc) then what will store in the variable.


    case 1:
    unsigned char value = '0';
    case 2:
    unsigned char value = 0;

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    have you gotten this far yet in your investigation?

    Code:
     #include <stdio.h>
     #include <stdlib.h>
    
    int main (void)
    {
    unsigned char value = '0'; 
    unsigned char value2 = 0; 
    unsigned char value3 = '65'; // added two for good measure
    unsigned char value4 = 65; // compile that to see what it tells you
    
    
    // with %c and %d
    printf("unsigned char value = '0' %c = %d\n",value, value );
     
    printf("unsigned char value = 0 %c = %d\n",value2, value2 );
    
    printf("unsigned char value = 0 %c = %d\n",value3, value3 );
    
    printf("unsigned char value = 0 %c = %d\n",value4, value4 );
    
        int i;
    
        /* Print ASCII values from 0 to 255 */
        for(i=0; i<=255; i++) 
              printf("ASCII value of character %c = %d\n", i, i);
        
        unsigned char a = '\0';
        for ( i = 0; i < 127; i++)
            printf("unsigned char ASCII value of character %c = %d\n", a, a);
            
        signed char b = '\0'; 
        for ( i = 0; i < 127; i++)
            printf("signed char ASCII value of character %c = %d\n", b, b);
        char c; // another warning in this example 
        for ( i = 0; i < 127; i++)
            printf("char ASCII value of character %c = %d\n", c, c);
        // should go to 127 // ?????
        unsigned char e[4] = {'1','2','3', '9'} ; 
    
        for ( i = 0; i < 4; i++)
            printf("unsigned char e[4] %c = %d\n", e[i], e[i]);
            
        unsigned char f[6] = {0, 1, 2, 3, 4, 5};  
        for ( i = 0; i < 6; i++)
            printf("unsigned char f[6] %c = %d\n", f[i], f[i]);
    return 0;
    }
    If you are using just zero and one then experiment with just zero and one and see what will store in the variable.

    If that leads to another question. Feel free to ask.

    Depending on what system you're running.
    Code:
    ./programName > some_made_up_file_name_to_store_it_in
    
    //to see all of the output
    
    $ cat ascII_experiment | less
    because it has a lot of output and your terminal buffer might not hold it all.
    Last edited by userxbw; 11-07-2017 at 08:03 AM.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by krkr View Post
    Can you help to understand what will store in "value" variable in the following two cases. When we assign a character then ascii value will be stored, but if we assign value (sat 0 ,1 etc) then what will store in the variable.
    Consider this code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    // #define NDEBUG
    
    int main(void)
    {
        char digit; // '0' till '9'
        int diff;
        
        scanf("%c", &digit);
        assert( digit>='0' && digit<='9' );
        diff=digit-'0';
        printf("diff is %d\n", diff);
    
        return EXIT_SUCCESS;
    }

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    add this you the bottom of that code
    Code:
    signed int z;
    unsigned int x;
    short int s;
        
    printf("sizeof (unsigned char) %lu\n",sizeof(a));
    printf("sizeof (signed char)%lu\n",sizeof(b));
    printf("sizeof (char)%ld\n",sizeof(c));
    
    printf("sizeof (unsigned int) %lu\n",sizeof(x));
    printf("sizeof (signed int)%lu\n",sizeof(z));
    printf("sizeof (int)%ld\n", sizeof(i));
    printf("sizeof (short int)%lu\n",sizeof(s));
    Last edited by userxbw; 11-07-2017 at 08:32 AM.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    It seems when we give within single quotes then it is taking it as a "ascii character", otherwise it is treating it as "ascii value" in decimal.


    But if we give more than 9 in single quotes then it is taking only first digit as "ascii character", is it because only "ascii characters" are available only from '0' to '9' ?

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    it has to do with math and fitting something of one size into something that is able to hold it instead of it being too small to hold it.

    C Data Types

    https://docs.oracle.com/cd/E19253-01...r-2/index.html

    after you read them pages to see what they say about size of data types.

    take notice of the data type that is printing out the entire ascii table. then try to figure out the why it does not work when using a char to try and do the same thing if you still have to.


    Code:
     int i; 
        /* Print ASCII values from 0 to 255 */
        for(i=0; i<=255; i++) 
              printf("ASCII value of character %c = %d\n", i, i);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-26-2015, 11:29 PM
  2. malloc related query
    By roaan in forum C Programming
    Replies: 1
    Last Post: 09-29-2009, 02:53 AM
  3. Structure related query
    By progue in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 05:37 AM
  4. Query related to DisconnectEx?
    By dp_76 in forum Networking/Device Communication
    Replies: 5
    Last Post: 05-25-2005, 02:33 AM
  5. Query related to recv()?
    By dp_76 in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-16-2005, 07:25 AM

Tags for this Thread