Thread: Cast

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Cast

    Hi ..
    Can someone explain what is a "cast" and how can be used it?
    Thank you in advance.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    The following code should set the char, character, to the value 2.

    Here's an example of what NOT to do:

    char character;
    int integer;

    integer = 2;
    character = integer;

    This stores the value 2 to character, but printing the character will not display 2. The ASCII value that character needs to hold to display 2 is actually 50!

    Try this instead:

    integer = 2;
    character = (char)integer;

    (char) is called a type-cast and is used to automatically convert variables of one type to another. character will now print as 2.

    Good programming always uses type-casting when using a line of code that uses different data types.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM