Thread: ascii conversion

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    ascii conversion

    int d;
    scanf("%c", &d);
    printf("%d", d);

    here giving a alpha value as input and am trying to get ascii value as output why is not working instead it shows address of the char all other ways it works, like getting decimal in char and convert into char.

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by zero_code View Post
    int d;
    scanf("%c", &d);
    printf("%d", d);

    here giving a alpha value as input and am trying to get ascii value as output why is not working instead it shows address of the char all other ways it works, like getting decimal in char and convert into char.
    You should have your code the other way - scan an integer, print a character.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    since the %c in scanf expects pointer to char your d var should be declared as char d
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    4
    LIke this?

    Code:
    #include<stdio.h>
    
    main()
    {
        char d;
        scanf(" %c", &d);
        printf("%d\n", d);
    return 0;
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Looks better and even works...

    You may benefit from reading FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    And I will add loop so several conversions could be made in the same run...
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        char d;
        while(scanf(" %c", &d) == 1)
        {
            printf("%d 0x%x\n", d, d);
        }
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Looks better and even works...

    You may benefit from reading FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    And I would add loop so several conversions could be made in the same run...
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        char d;
        while(scanf(" %c", &d) == 1)
        {
            printf("%d 0x%x\n", d, d);
        }
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    relating to the same question suppose if the variable is declared as one type and read as other type in printf is the behavior not undefined. For example
    Code:
    float f; printf("%d",f);
    Is this behavior undefined or it is ok? Similarly for char and int types. I am getting confused.

  8. #8
    Registered User
    Join Date
    Apr 2015
    Posts
    4
    Quote Originally Posted by Satya View Post
    Code:
    float f; printf("%d",f);
    The variable must be read as the same type float "%f" therefore your code is incorrect.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Satya View Post
    relating to the same question suppose if the variable is declared as one type and read as other type in printf is the behavior not undefined. For example
    Code:
    float f; printf("%d",f);
    Is this behavior undefined or it is ok? Similarly for char and int types. I am getting confused.
    You are correct in that the behavior is undefined.

    Quote Originally Posted by c99-draft
    7.19.6.1 The fprintf function

    ...

    9 If a conversion specification is invalid, the behavior is undefined. If any argument is
    not the correct type for the corresponding conversion specification, the behavior is
    undefined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion from ascii to decimal
    By subashini in forum C Programming
    Replies: 8
    Last Post: 10-22-2012, 12:30 PM
  2. Binary to Ascii Conversion
    By codemaster12 in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 10:57 AM
  3. binary to ASCII conversion
    By loaded_tk in forum C Programming
    Replies: 3
    Last Post: 03-01-2006, 09:28 AM
  4. Pointer array's and ASCII to bit conversion
    By AdamLAN in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2005, 05:55 PM
  5. Hex to Ascii conversion
    By Ryno in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 09:16 AM

Tags for this Thread