Objective-C: Console Programming

 

Addition of two numbers in Objective-C

A program- program1.m for Addition of two numbers in Objective-C with its output.

 

 

Difference b/w %d and %i in Objective-C

Both these format specifiers are used for numbers in Objective-C, but there is a minnor difference in the two. They both work similar when used for output but different when used for taking input. %d takes only decimal values as input whereas, %i takes default value as integer but also allows Octal number (if number is preceded by 0) and hexadecimal (if number is precedded by 0x). To make it more clear, consider the two programs given below with their outputs.

 

 

 

 

Inputs

Output of first program (%d)

Output of second program (%i)

First, decimal number      : 33

33

33

Second, octal number        : 033

33

27

Third, hexadecimal number: 0x33

0

51

 

In both the programs, input is taken same in order to have a better understandability. In the first program, only decimal values are taken an input. In the second program, octal and hexadecimal values are also taken as input and decimal values are printed.