---
title: "Objective-C : Type Casting"  
description: "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"  
author: "Tarun Kumar"  
published: 2015-07-14  
updated: 2026-05-13  
canonical: https://www.mindstick.com/articles/1823/objective-c-type-casting  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 3 minutes  

---

# Objective-C : Type Casting

*Previously we learn about macros identifiers in ObjC* : Objective-C : Macros

**T**ype 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](https://yourviews.mindstick.com/view/81607/hindi-language-in-pakistan-is-getting-killed) a semantics of TypeCasting. Today we will discuss about type casting in [Objective C](https://www.mindstick.com/blog/10914/objective-c-data-type-enum). You can convert values from one type to another explicitlyusing the cast operator as follows:\

(type_name)[expression](https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp)

InObjective-C, we generally use CGFloat for doing floating point [operation](https://yourviews.mindstick.com/view/83916/why-is-it-necessary-to-sterilize-operation-theater), 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](https://yourviews.mindstick.com/view/88478/vibe-coding-the-rise-of-ai-assisted-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](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-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](https://www.mindstick.com/blog/12923/do-you-want-an-early-and-valuable-promotion-at-work) 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](https://www.mindstick.com/articles/126300/apa-citation-style-get-to-know-about-it-for-your-next-assignment) [operators](https://www.mindstick.com/articles/13159/operating-press-brakes-an-ultimate-guide-for-brake-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](https://yourviews.mindstick.com/view/81093/palghar-sadhu-lynching-urban-naxals-and-conversion-lobby-behind-it) applies and compiler converts iand c into float and add them yielding a float result.

---

Original Source: https://www.mindstick.com/articles/1823/objective-c-type-casting

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
