Thread: Int to char

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    21

    Int to char

    Hi,

    I'm trying to tranform an integer value(like 65) to its corresponding ascii char (in this case 'A'). How can I do this?

    Regards,

    JKepler

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    A char is just a small number, usually a byte, either signed (-128 to 127) or unsigned (0 to 255).
    To "transform" 65 into a char you just copy it into a char.
    Really it's still just the number 65 but held in a single byte instead of a (usually) 4-byte int.
    The way it really becomes a character is in how it's interpreted.
    If we print it as an int, we get an int. If we print it as a char, we get the character symbol.
    Code:
    #include <stdio.h>
    
    int main(void) {
        char c = 65;
        printf("%d  %c\n", c, c);   // should print: 65 A
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  2. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  3. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  4. undefined reference to `RunSwmmDll(char*, char*, char*)'
    By amkabaasha in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2011, 12:33 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

Tags for this Thread