Constants - Objective C
The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.
Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumration constants as well.
The constants are treated just like regular variables except that their values cannot be modified after their definition.
Integer Literals
An integer literal can be decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
An integer literal can also have 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.
Here are some examples of integer literal.
Following are other example of various types of integer literals:
Floating-Point literals
A floating-point literal has an integer part, a deciaml point, a fractional part, and an exponent part. You can represent floating-point literal either in decimal or exponential form.
While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or bot. The signed exponent is introduced by e or E.
Here are some example of floating-Point literals:
Character constants
Character literals are enclosed in single quotes e.g. 'x' and can be stored in a single variable of char type.
A character literal can be a plain character (e.g. 'x') an escape sequence (e.g. '\r') pr a universal character (e.g. '\u02C0').
There are certain character in C when they are proceeded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Here you have list of some of such escape dequence codes:
Following is the example to show few escape sequence characters:
String literals
String literals or constants are enclosed in double quotes "" . A string contains character that are similar to character literals; plain characters, escape sequences and universal characters.
You can break a long line into ,multiple line using string literals and separating them using whitespaces.
Example
There are two simple ways in C to define constants:
The #define Preprocessor
Following is the form to use #define preprocessor to define a constant:
#define identifier value
Following example explain it in details:
The const keyword
You can use const prefix to declare constant with a specific type as follows:
cont type variable = value;
Example
Note that it is a good programming practice to define constants in CAPITALS.
Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumration constants as well.
The constants are treated just like regular variables except that their values cannot be modified after their definition.
Integer Literals
An integer literal can be decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
An integer literal can also have 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.
Here are some examples of integer literal.
212 /* legal */
215u /* legal */
0xFeel /* legal */
078 /* illegal: 8 is not an octal digit */
032UU /* illegal: cannot repeat a suffix */
215u /* legal */
0xFeel /* legal */
078 /* illegal: 8 is not an octal digit */
032UU /* illegal: cannot repeat a suffix */
Following are other example of various types of integer literals:
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
Floating-Point literals
A floating-point literal has an integer part, a deciaml point, a fractional part, and an exponent part. You can represent floating-point literal either in decimal or exponential form.
While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or bot. The signed exponent is introduced by e or E.
Here are some example of floating-Point literals:
3.14159 /* legal */
314159E-5L /* legal */
510E /* illegal: incomplete exponent */
210f /* illegal: no decimal or exponent */
.e55 /* illegal: missing integer or fraction */
314159E-5L /* legal */
510E /* illegal: incomplete exponent */
210f /* illegal: no decimal or exponent */
.e55 /* illegal: missing integer or fraction */
Character constants
Character literals are enclosed in single quotes e.g. 'x' and can be stored in a single variable of char type.
A character literal can be a plain character (e.g. 'x') an escape sequence (e.g. '\r') pr a universal character (e.g. '\u02C0').
There are certain character in C when they are proceeded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Here you have list of some of such escape dequence codes:
Escape sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n New line
\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
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n New line
\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
Following is the example to show few escape sequence characters:
String literals
String literals or constants are enclosed in double quotes "" . A string contains character that are similar to character literals; plain characters, escape sequences and universal characters.
You can break a long line into ,multiple line using string literals and separating them using whitespaces.
Example
"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"
Defining Constants"hello, \
dear"
"hello, " "d" "ear"
There are two simple ways in C to define constants:
- Using #define preprocessor.
- Using const keyword.
The #define Preprocessor
Following is the form to use #define preprocessor to define a constant:
#define identifier value
Following example explain it in details:
The const keyword
You can use const prefix to declare constant with a specific type as follows:
cont type variable = value;
Example
Note that it is a good programming practice to define constants in CAPITALS.
No comments: