codescracker


objective c

Objective-C Constants



« Previous Tutorial Next Tutorial »


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:

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:

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

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

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

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

Character Constants in Objective-C

Character constants are enclosed in single quotes like:

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
\a Alert or bell
\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[17871] 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:

Define Constants in Objective-C

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

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[21460] value of area: 200
2013-08-03 13:18:37.932 demo[21460]

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[21460] Value of area: 200
2013-08-03 13:18:37.932 demo[21460]

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test