Objective C Constants - Objective C

What is Objective C Constants?

  • The fixed values which program may not alter during its execution called constants or literals.
  • These fixed values are also called literals.
  • Constants can be an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.
  • The constants values cannot be modified once they are defined and are treated just like regular variables.


Integer literals

  • An integer literal can be a 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 have a suffix, a combination of U and L, for unsigned and long, respectively.
  • The suffix can be uppercase or lowercase and can be used in any order.


Example for integral literals:

The examples of various types of Integer literals are:

Floating-point literals

  • A floating-point literal includes an integer part, a decimal point, a fractional part, and an exponent part.
  • The floating point literals can be represented either in decimal form or exponential form.
  • Include the decimal point, the exponent, or both and while representing using decimal.
  • Using exponential form, include the integer part, the fractional part, or both.
  • The signed exponent is introduced by e or E.

Example for floating-point literals:

Character constants

  • A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').
  • Character literals are enclosed in single quotes are of char type e.g., 'x'.
  • There are certain characters in C when they are preceded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t).

Here, you have a list of some of such escape sequence codes:
The below example shows few escape sequence characters:

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


The following result is produced on compiling and executing the code.


String literals

  • A string contains characters that are similar to character literals enclosed in double quotes "".
  • Character literals are plain characters, escape sequences, and universal characters.
  • String literals are used to break a long line into multiple lines and separates them using whitespaces.

Examples of string literals are All the three forms are identical strings.

Defining Constants

There are two simple ways in C to define constants:

  • Using #define preprocessor.
  • Using const keyword.

The #define Preprocessor

The following form to use #defines preprocessor to define a constant :

Syntax:

Example:


The above code is compiled and executed, produces the following result:


The const Keyword

To declare constants use const keyword with a specific type as follows:

Syntax:

Example:

The above code is compiled and executed,produces the following result:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Objective C Topics