Objective-C: Programming fundamentals

To write an Objective-C program, it requires basic knowledge of programming in C-language and object oriented concepts. As Objective-C is primarily used by Apple, providing its own developer tool (Xcode) for developing MAC and iPhone applications, a developer can also write programs on Terminal window by using the GNU Objective-C compiler (gcc).

 

Data types in Objective-C

Data type specifies the type of data a variable holds. In Objective-C, the primitive data types are same as that of C-language and some new data types are also included in its library that are as follows:

 

Type Description
char A character of the local character set
int An integer (whole numbers), e.g. 1, 2, …
float Floating point number, e.g. 1.2, 1.7, 5.8, …
short A short integer
long A double precision of short
double A double precision of float
signed Specifies a integer value either +ve or –ve
unsigned Specifies only +ve integer values
Bool Used for conditions, either True(1)/False(0)
id Used to store  reference to an object

 Data types in Objective-C

 

Escape sequences in Objective-C

Following is the list of escape sequences used in Objective-C programming language.

 

Symbol Function

\b

Backspace

\f

Form feed

\n

New line

\r

Carriage return

\t

Horizontal tab

\v

Vertical tab

\\

Backslash

\?

Question mark

\’ and \’’

Single and double quotes

 Escape sequences in Objective-C

Identifiers

  • An identifier is any combination of alphanumeric characters that specifies a variable, type, function or class in the program.
  • An identifier must start with alphabet and may include underscore and digits but with limited length, for example,

int a=5;

Here a is an integer type variable, whose identifier is a.

Note that a keyword can never be used as an identifier.

 

Literals

Literals are constant values that are stored in variables.

There are 5 types of literal constants that are generally used while programming namely, Integer, Floating-point, Characters, String and Boolean values, for example, when we wrote:

a = 3;

3 is the literal constant in this statement.

 

  • Integer literals: Numerical constants that identify integer values are called integer literals, for example,

                  512           : Decimal value

                  023           : Octal value

                  0x2           : Hexadecimal value

 

  • Floating-point literals: Numerical constants that identify decimal and exponential values are called floating-point literals, for example,

                  4.5           

                  4.5e12      : 4.5 x 1012

 

  •  Characters literals: Non-numeric constants with single characters within single quotes are called character literals, for example,

                  ‘x’

 

  •  String literals: Non-numeric constants with collection of characters within double quotes are called string literals, for example,

                 “hello! This is a string literal”

 

  • Boolean literals: There are only two values: true and false, and these are represented as values via type bool by literals true and false


Constants

Constants are values that never change. Other than using literal constants while programming, we can also declare or define constants as per our requirements.

Declaring constants

Constant can be declared by using keyword- const, as prefix with the type of variable as follows:

const int aAsconstantinteger = 10;

const float bAsconstantfloat = 5.2; 

 Now, the values of these two variables cannot be modified as they are declared as constants.  

Defining constants 

Constants can also be defined by the developer with a desired name via using #define preprocessor directive as follows:

#define pi 3.14

Now, this constant pi can be used in the code directly.