Username:
Password:
Advanced C Programming Tutorials

Types of Data / Convert & Cast

Subtitles of the Movie

In an expression, operations like plus and minus operate only if the two operands are of the same type. That is, instructions exist in the hardware for the machine to add two 32-bit integers together but it can't add a 32-bit integer to a 16-bit integer. For the addition to take place, one of the items has to be converted by the compiler into the type of the other item. And C has some rules about doing that. It will do it automatically. In any expression, the first thing that has to happen is all char and short values are converted to ints. Once this initial conversion has taken place, any other necessary conversions are made for the individual operations. The compiler looks at the two operands of a binary operation. If either one of them is a long double, the other one is converted into a long double before the operation takes place and the result of that operation is then a long double. If, however, neither operand is a long double, it checks for either one being a double. If so, the other is converted into a double and the operation takes place with the result being a double. Otherwise if either operand is a float, the other is converted to a float and the result is a float. Otherwise, if either operand is an unsigned long, the other operand is converted. The operation is performed and the result is an unsigned long. Otherwise, if either operand is a long, the other value is converted to a long and the result of the operation is a long. Unless the other operand is an unsigned int that can not be represented by a long, then both operands are converted to unsigned longs. Otherwise if either operand is an unsigned int, the other is converted to an unsigned int for the operation. This means that all operations always take place on data items of the same types. And in any expression the result will be that of the highest type in the list. For example, in this expression, the result is a double. Some of the arithmetic, adding C and S and I, maybe done as an int data type but with a double being included in the list, the final result will be a double because of the automatic promotions. If you want to force the result to be an int, you'll have to cast the value like this. Of course, this means that X must be an int. If int is a double, the value would just be promoted again for the assignment operation. The general rule is that you only need to cast when the type of conversion could possible cause the loss of data. For example, when you stuff an int into a short. But generally speaking, it's a good idea to cast all values that need to be converted. That way you can make sure the compiler does what you want it to do and it's somebody who comes along later and reads your code can figure out what your code is doing. All pointers are the same size, so converting a pointer from one type to another is only a matter of changing its attitude, changing what it believes it's pointing to. A void pointer is a special case. It doesn't know what it points to so no conversion is necessary. You can assign any pointer to a void pointer and any void pointer can be assigned to any other pointer. For example, you can assign either the int or the double pointer value to the void and then assign the void pointer to either of the other two. No problem. Exactly what this means is up to you. You need to be careful what you do with void pointers because they can point to anything and they have no idea what they're pointing to. The only other way to convert pointers is to use a cast to specify the type you want the pointer to become. This assignment statement converts the double pointer into an int pointer. Of course, the conversion doesn't require the address to be anything in particular so you can mess yourself up with casting pointers.

Tutorial Information

Course: Advanced C Programming
Author: Arthur Griffith
SKU: 33965
ISBN: 1-935320-24-6
Release Date: 2009-01-30
Duration: 5.5 hrs / 82 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available