 # Objective-C Constants

Constants represents a fixed values, and the program may not alter these values during the execution. You can also called these fixed values as literals.

Constants are of any of the following basic data types:

• character constant
• integer constant
• string constant
• floating-point constant
• enumeration

## Integer Constants in Objective-C

Integer constants in Objective-C refers to decimal, octal, or hexadecimal constant. There is a prefix specifies the base, which are:

• 0x or 0X for hexadecimal
• 0 for octal
• nothing for decimal

And an integer constants can also have a suffix that is a combination of:

• U or u for unsigned
• L or l for long

Here are some examples listed, which are Objective-C integer constants:

• 414
• 315u
• 0xFeeL

Following are some other examples of the various types of Integer literals:

• 414 - decimal
• 0314 - octal
• 40 - int
• 40u - unsigned int
• 40l - long
• 40ul - unsigned long

## Floating-point Constants in Objective-C

Floating-point literals have an integer part, a decimal point, and a fractional part, and then an exponent part. You have two choice to represent floating-point literals, either in decimal form, or in exponential form.

Here are some of the examples of floating-point literals in Objective-C

• 3.14159
• 314159E-5L

## Character Constants in Objective-C

Character constants are enclosed in single quotes like:

• 'a'
• 'b'
• 'c'
• 'm'
• 'x'

A character literal can be a plain character (as shown above, like 'a', 'b') or an escape sequence like '\t' or a universal character like '\u02C0'. Here, the following table lists some of the escape sequence codes in Objective-C:

Escape Sequence Code Name
\n Newline
\t Horizontal tab
\v Vertical tab
\b Backspace
\\ \ (character)
\' ' (character)
\" " (character)
\f Form feed
\r Carriage return
\? ? (character)
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

Let's look at the following example, which shows some of the above characters:

```/* Objective-C Constants - Example Program */

#import <Foundation/Foundation.h>
int main()
{
NSLog(@"Hello\tWorld,\n\nThis is Objective-C Constants Tutorial.");
return 0;
}```

When the above code is compile and executed, it would produce the following result:

```2013-08-03 13:18:37.932 demo Hello	World,

This is Objective-C Constants Tutorial.```

## String Constants in Objective-C

String constants are simply enclosed in double quotes (""). A string constants looks like:

• "Hi"
• "Hello, World"
• "This is Objective-C Tutorial"

## Define Constants in Objective-C

Here are the two ways to define constants in Objective-C:

• using #define preprocessor
• using const keyword

## The #define Preprocessor in Objective-C

Here is the general form to use the #define preprocessor in defining a constant in Objective-C:

`#define identifier value`

Here is an example, illustrating the use of #define preprocessor to define an Objective-C constant:

```/* Objective-C Constants - Example Program */

#import <Foundation/Foundation.h>
#define LENGTH 10
#define WIDTH  20
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
NSLog(@"value of area: %d", area);
NSLog(@"%c", NEWLINE);
return 0;
}```

When the above code is compile and executed, it would produce the following result:

```2013-08-03 13:18:37.932 demo value of area: 200
2013-08-03 13:18:37.932 demo```

## The const Keyword in Objective-C

The const keyword is used to declare constants with a particular type. Here is the general form to define constants using the const keyword in Objective-C:

`const data-type variable-name = value;`

Here is an example, demonstrating the use of const keyword in defining the constants in Objective-C:

```/* Objective-C Constants - Example Program */

#import <Foundation/Foundation.h>
int main()
{
const int  LENGTH = 10;
const int  WIDTH  = 20;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
NSLog(@"Value of area: %d", area);
NSLog(@"%c", NEWLINE);
return 0;
}```

When the above code is compile and executed, it would produce the following result:

```2013-08-03 13:18:37.932 demo Value of area: 200
2013-08-03 13:18:37.932 demo```

Tools
Calculator