What is Objective-C Constants ?

               The constants allude to settled qualities that the project may not modify amid its execution. These settled qualities are likewise called literals.

Constants can be of any of the essential information sorts like a whole number steady, a coasting consistent, a character consistent, or a string strict. There are additionally count constants too.

The constants are dealt with much the same as normal variables aside from that their qualities can't be adjusted after their definition.

Integer literals

              A whole number strict can be a decimal, octal, or hexadecimal consistent. A prefix determines the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. 

A whole number strict can likewise have an addition that is a mix of U and L, for unsigned and long, individually. The postfix can be uppercase or lowercase and can be in any request. 

Here are a few illustrations of number literals:

212         /* Legal */
215u        /* Legal */
0xFeeL      /* Legal */
078         /* Illegal: 8 is not an octal digit */
032UU       /* Illegal: cannot repeat a suffix */
Taking after are different cases of different sorts of Integer literals:


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

Floating-point literals

             A gliding point strict has a whole number part, a decimal point, a fragmentary part, and an example part. You can speak to skimming point literals either in decimal structure or exponential structure. 

While speaking to utilizing decimal structure, you must incorporate the decimal point, the example, or both keeping in mind speaking to utilizing exponential structure, you must incorporate the whole number part, the fragmentary part, or both. The marked example is presented by e or E. 

Here are a few samples of skimming point literals:

3.14159       /* Legal */
314159E-5L    /* Legal */
510E          /* Illegal: incomplete exponent */
210f          /* Illegal: no decimal or exponent */
.e55          /* Illegal: missing integer or fraction */

Character constants


               Character literals are encased in single quotes e.g., "x" and can be put away in a basic variable of singe sort. 

A character strict can be a plain character (e.g., 'x'), a departure arrangement (e.g., '\t'), or an all inclusive character (e.g., '\u02C0'). 

There are sure characters in C when they are continued by an oblique punctuation line they will have exceptional importance and they are utilized to speak to like newline (\n) or tab (\t). Here, you have a rundown of some of such escape grouping codes:

Escape sequenceMeaning
\\\ character
\'' character
\"" character
\?? character
\aAlert or bell
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\oooOctal number of one to three digits
\xhh . . .Hexadecimal number of one or more digits

Taking after is the illustration to demonstrate few break grouping characters:

#import <Foundation/Foundation.h>;
int main()
{
           NSLog(@"Hello\tWorld\n\n");
return 0;
}


At the point when the above code is assembled and executed, it creates the accompanying result:

2013-09-07 22:17:17.923 demo[17871] Hello World

String literals

                 String literals or constants are encased in twofold quotes "". A string contains characters that are like character literals: plain characters, departure groupings, and all inclusive characters. 

You can break a long line into different lines utilizing string literals and dividing them utilizing whitespaces. 

Here are a few samples of string literals. All the three structures are indistinguishable strings

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

Characterizing Constants

There are two basic courses in C to characterize constants: 

1. Utilizing #define preprocessor. 

2. Utilizing const decisive word.

The #define Preprocessor

Taking after is the structure to utilize #define preprocessor to characterize a consistent:

#define identifier value
Taking after sample clarifies it in subtle element:

#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;
}

At the point when the above code is accumulated and executed, it delivers the accompanying result:

2013-09-07 22:18:16.637 demo[21460] value of area : 50
2013-09-07 22:18:16.638 demo[21460] 

The const Keyword

You can utilize const prefix to proclaim constants with a particular sort as takes after:

const type variable = value;
Taking after case clarifies it in subtle element:

#import <Foundation/Foundation.h>;

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


At the point when the above code is ordered and executed, it delivers the accompanying result:

2013-09-07 22:19:24.780 demo[25621] value of area : 50
2013-09-07 22:19:24.781 demo[25621] 

Note that it is a decent programming practice to characterize constants in CAPITALS.


0 comments:

Post a Comment

My Instagram