Thread: Type cast clarification

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Type cast clarification

    Hi all,

    I have this program.
    Code:
    int test_variable;
    
    int main()
    {
    test_variable = (int)0x12;
    }
    Now my doubt is what is the advantage of type casting 0x12 to int. suppose if the test_variable data type is "char" then should i type cast to "char"?

    Thanks and regards,
    Satya

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No advantage in this case since the type of 0x12 is already int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Quote Originally Posted by laserlight View Post
    No advantage in this case since the type of 0x12 is already int.
    Thank you for the reply. Can you please tell me in which cases i have to use type casts specially for constants.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For constants, I would try to use a constant of an appropriate type, e.g., to use a u suffix when I want an unsigned integer constant, or an f suffix if I want a float rather than a double.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Satya View Post
    Thank you for the reply. Can you please tell me in which cases i have to use type casts specially for constants.
    Some functions, like fgetc, getchar, use type casting to compare the value being returned with EOF. Which is why you normally store the return value in type int. If you instead store them in type char, the comparison with EOF will never succeed.

    There are probably better examples, but generally you type cast for comparisons and other things that wouldn't work otherwise. Take this compare function, part of a bubble sort program than does multiple data types:

    Code:
    int compare_long(const void *m, const void *n)
    {
        long *ml, *nl;
        ml=(long *)m;
        nl=(long *)n;
    
        return (*ml>*nl);
    }
    The void values being passed in are cast in the assignment so that the proper long values can be compared.
    Last edited by Alpo; 05-08-2014 at 12:15 PM. Reason: made mistake

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The void values being passed in are cast in the assignment so that the proper long values can be compared.
    void* can be converted, without a cast, to any object pointer type. In fact, the casts used here hide a potential problem:
    Code:
      const void *m;
      long *ml;
      ml = m;
    yields, with one compiler: “warning #2332: a value of type "const void *" cannot be assigned to an entity of type "long *" (dropping qualifiers)”

    A diagnostic of some sort is required with the above code.

    The casts hide the fact that const was removed. Instead:
    Code:
    const void *m;
    const long *ml;
    ml = m;
    yields no diagnostic (or at least is not required to).

    In general, my advice for casting is this: don't do it. Whenever I see a cast I get suspicious, because typically there's a better way to handle the problem. A few places where casting is probably the right choice:

    • When you have two integer variables but need to do floating point division with them (if a constant is used as the divisor or dividend, it's better to just use a double such as 100.0 instead of an integer 100).
    • If you're passing something to a function with a variable argument list, and the promoted type isn't guaranteed to match what is expected. For example, casting a pointer to void* when using %p in printf().
    • If you're using one of the ctype functions such as isalpha(), and passing a char to it, the char should be cast to unsigned char. This is due to (what I consider) a poor design in ctype functions, which was likely inherited from pre-standard implementations.

  7. #7
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    @Cas - Thanks, I'll update that on file.

    Plus when using const this always gets added to my confusion:

    int *const p1 = q; // constant pointer to int variable
    int const* p2 = q; // pointer to constant int
    const int* p3 = q; // pointer to constant int

    from - Stroustrup: C++ Style and Technique FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clarification: Type cast double to int
    By strokebow in forum C Programming
    Replies: 1
    Last Post: 05-10-2011, 07:59 AM
  2. type cast problem
    By tikelele in forum C Programming
    Replies: 10
    Last Post: 10-31-2007, 09:23 PM
  3. Replies: 4
    Last Post: 12-02-2004, 01:57 PM
  4. type cast to a pointer....
    By rc7j in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2002, 04:13 PM
  5. type cast or conversion?
    By steviecrawf in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 03:08 PM