type casting

This is a discussion on type casting within the C Programming forums, part of the General Programming Boards category; what is it exactly i dont' quite understand. can some one explain? thanks...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    28

    type casting

    what is it exactly i dont' quite understand. can some one explain? thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    C has rules on what type one variable is vs another in order to protect you from making mistakes. Type casting is for when you want to break such a rule.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Or it is used as the conversion that it is, when applicable.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Every value is made up of a bit pattern and a type. The bit pattern doesn't change, but just a bit pattern isn't useful. The type tells C how to represent the bit pattern. An example is that the bit patten 01000001 represented as an int is 65, but represented as a char it's 'A'. The bit pattern doesn't change, but the type it's represented as does.

    Type casting is when you force C to use the representation you want. The representation you want might not be possible, but a type cast also tells C to assume it is. That's why it's easy to crash a program if you aren't careful with type casts.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    And in the case of an actual conversion (such as floating point to integral), the actual bit pattern can change. Other conversions may relate to the size of the object, which obviously also deals with an underlying bit pattern.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    The easiest way to guarantee the bit pattern does not change is to to make a pointer, cast it to the correct type and dereference it. You may have to do this with some troublesome types such as float or double. But it may not give you the result you expect if you do:

    Code:
    float f = 1.0f;
    int n1 = (int)f; /* n1 == 1 */
    int n2 = *(int*)&f; /* This one won't be what you expect, but the bit pattern is intact. */
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 07:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21