Thread: Type conversions

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    Type conversions

    I want to temporarily change a variable's type from int to char....

    How would I do this

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    int MyInt = 65;
    char MyChar = (char)Myint;
    printf("%c", MyChar);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or ... Maybe you want something like this:
    Code:
    char numstring[20];
    int num = 123;
    sprintf (numstring, "%d", num);
    printf ("%s %d", numstring, num);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or... you may want to use a union (two variables share the same data)

    This code prints the character A (ASCII value 65).
    Code:
    #include <conio.h>
    #include <stdio.h>
    
    typedef union
    {
       int IntVal;
       char CharVal;
    }IntChar;
    
    int main()
    {
       IntChar MyVar;
       MyVar.IntVal = 65;
       printf("%c", MyVar.CharVal);
       getch();
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM