Objective C Tutorial - Objective-C Literals






The literals are fixed values that the program may not change.

Literals can be of any of the basic data types like an integer literal, a floating literal, a character literal, or a string literal.

Integer literals

An integer literal can be a decimal, octal, or hexadecimal constant.

A prefix specifies the radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

An integer literal can have a suffix that is a combination of U and L, for unsigned and long, respectively.

The suffix can be uppercase or lowercase and can be in any order.

The following code shows some examples of integer literals:

12         /* Legal */
15u        /* Legal */
0xFEEL     /* Legal */
078        /* Illegal: 8 is not an octal digit */
02UU       /* Illegal: cannot repeat a suffix */

125        /* decimal */
0213       /* octal */
0x4b       /* hexadecimal */
31         /* int */
30u        /* unsigned int */
30l        /* long */
30ul       /* unsigned long */





Floating-point literals

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part.

We can represent floating point literals either in decimal form or exponential form.

The decimal form includes the decimal point, the exponent, or both.

The exponential form includes the integer part, the fractional part, or both.

Here are some examples of floating-point literals:

3.14159       /* Legal */
314159E-5L    /* Legal */
51E           /* Illegal: incomplete exponent */
21f           /* Illegal: no decimal or exponent */




Character Literals

Character literals are enclosed in single quotes e.g., 'x'.

A character literal can be a character, an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').

The following table lists the escape sequence codes:

Escape sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

The following code shows how to use the escape sequence characters:

#import <Foundation/Foundation.h>

int main()
{
   NSLog(@"Hello\tWorld\n\n");

   return 0;
}

The code above generates the following result.

Hello    World

String literals

String literals are enclosed in double quotes ".

A string can has plain characters, escape sequences(e.g., '\t'),, and universal characters(e.g., '\u02C0').

Defining Constants

There are two simple ways in Objective-C to define constants:

  1. Using #define preprocessor.

  2. Using const keyword.

The following is the syntax to use #define preprocessor to define a constant:

#define identifier value

The following code shows how to use the syntax above.

#import <Foundation/Foundation.h>

#define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'

int main()
{

   int area;  
  
   area = LENGTH * WIDTH;
   NSLog(@"value of area : %d", area);
   NSLog(@"%c", NEWLINE);

   return 0;
}

The code above generates the following result.

value of area : 50

We can use const prefix to declare constants with a specific type.

const type variable = value;

The following code shows how to use const.

#import <Foundation/Foundation.h>

int main()
{
   const int  LENGTH = 10;
   const int  WIDTH  = 6;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   NSLog(@"value of area : %d", area);
   NSLog(@"%c", NEWLINE);

   return 0;
}

The code above generates the following result.

value of area : 60