5_Objective-C Constants

Fixed values that the program may not alter during its execution.

Note : These fixed values are also called literals.

Data types of constants

Constants can be any of the basic data types

  • integer
  • floating
  • character
  • string
  • enumeration

Integer Constants

  • decimal
  • octal : Start with 0
  • hexadecimal : Start with 0x or 0X

Suffix for an integer literal 

  • Combination of U and L, for unsigned and long, respectively.
  • Uppercase or lowercase and can be in any order.


85 decimal 
0213 octal 
0x4b  hexadecimal
30  int
30u  unsigned int
30l  long
30ul unsigned long

Floating Constants

A floating-point literal has an

  • integer part
  • decimal point
  • fractional part
  • an exponent part.

Represent floating 

You can represent floating either in decimal form or exponential form.

But while representing using decimal form you must include the

  • decimal point
  • the exponent
  • or both

while representing using exponential form, you must include the

  • integer part
  • the fractional part
  • or both.

The signed exponent is introduced by e or E




Character constants

Character literals are enclosed in single quotes e.g., ‘x’ and can be stored in a simple variable of char type.

A character literal can be

  • Plain character (e.g., ‘x’)
  • Escape sequence (e.g., ‘\t’)
  • Universal character (e.g., ‘\u02C0’).
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

String literals

String literals or constants are enclosed in double quotes “”. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

Defining Constants

There are two simple ways define constants:

  • Using #define preprocessor.
  • Using const keyword.

The #define Preprocessor


#define constant_name value


Variables_declaring_defining_an_ initializing

The const Keyword

You can use const prefix to declare constants with a specific type


const type variable_name = value;



Note : that it is a good programming practice to define constants in CAPITALS.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s