Previously we learn about macros identifiers in ObjC : Objective-C : Macros
Type castingis a way to convert a variable from one data type to another data type. Forexample, if you want to store a long value into a simple integer then you cantype cast long to int.Type Casting a process converting one type of object to another type of object. Every language a semantics of TypeCasting. Today we will discuss about type casting in Objective C. You can convert values from one type to another explicitlyusing the cast operator as follows:
(type_name)expression
InObjective-C, we generally use CGFloat for doing floating point operation, whichis derived from basic type of float in case of 32-bit and double in case of64-bit. Consider the following example where the cast operator causes thedivision of one integer variable by another to be performed as a floating-pointoperation:
#import< Foundation/Foundation.h>
int main()
{
int sum = 17, count = 5;
CGFloat mean;
mean = (CGFloat) sum / count;
NSLog(@"Value of mean : %f\n", mean );
return 0;
}
When theabove code is compiled and executed, it produces the following result:
2015-07-14 11:38:51.813 demo[9709] Value of mean : 3.400000
It should benoted here that the cast operator has precedence over division, so the value ofsum is first converted to type double and finally it gets dividedby count yielding a double value.
Typeconversions can be implicit which is performed by the compiler automatically orit can be specified explicitly through the use of the cast operator. Itis considered good programming practice to use the cast operator whenever typeconversions are necessary.
IntegerPromotion:
Integerpromotion is the process by which values of integer type "smaller"than int or unsigned int are converted either to int or unsignedint. Consider an example of adding a character in an int:
#import< Foundation/Foundation.h>
int main()
{
int i = 17;
char c = 'c'; /* ascii value is 99 */
int sum;
sum = i + c;
NSLog(@"Value of sum : %d\n", sum );
return 0;
}
When theabove code is compiled and executed, it produces the following result:
2015-07-14 11:39:28.564 demo[9885] Value of sum : 116
Here, valueof sum is coming as 116 because compiler is doing integer promotion andconverting the value of 'c' to ascii before performing actual additionoperation.
UsualArithmetic Conversion:
The usualarithmetic conversions are implicitly performed to cast their values in acommon type.
The usualarithmetic conversions are not performed for the assignment operators, nor forthe logical operators && and ||. Let us take following example tounderstand the concept:
#import< Foundation/Foundation.h>
int main()
{
int i = 17;
char c = 'c'; /* ascii value is 99 */
CGFloat sum;
sum = i + c;
NSLog(@"Value of sum : %f\n", sum );
return 0;
}
When theabove code is compiled and executed, it produces the following result:
2015-07-14 11:40:20.760 demo[10035] Value of sum : 116.000000
Here, it issimple to understand that first c gets converted to integer but because finalvalue is float, so usual arithmetic conversion applies and compiler converts iand c into float and add them yielding a float result.
Leave a Comment