HPlogo HP C/HP-UX Reference Manual: Workstations and Servers > Chapter 2 Lexical Elements

Constants

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

A constant is a primary expression whose literal or symbolic value does not change.

Syntax

constant ::= floating-constant
integer-constant
enumeration-constant
character-constant

Description

Each constant has a value and a type. Both attributes are determined from its form. Constants are evaluated at compile time whenever possible. This means that expressions such as

  2+8/2

are automatically interpreted as a single constant at compile time.

Floating Constants

Floating constants represent floating-point values.

Syntax

floating-constant   ::=
fractional-constant [exponent-part] [floating-suffix]
digit-sequence exponent-part [floating-suffix]
fractional-constant ::=
[digit-sequence] . digit-sequence
digit-sequence .



exponent-part ::=
e [sign] digit-sequence
E [sign] digit-sequence

sign ::=
+
-

digit-sequence ::=
digit
digit-sequence digit

floating-suffix ::=
F
f
L
l
NOTE: Suffixes in floating-constants are available only in ANSI mode.

Description

A floating constant has a value part that may be followed by an exponent part and a suffix specifying its type. The value part may include a digit sequence representing the whole-number part, followed by a period (.), followed by a digit sequence representing the fraction part. The exponent includes an e or an E followed by an exponent consisting of an optionally signed digit sequence. Either the whole-number part or the fraction part must be used; either the period or the exponent part must be used.

Refer to Chapter 10 “HP C/HP-UX Implementation Topics” for the format of floating-point numbers.

A floating constant may include a suffix that specifies its type. F or f specifies type float (single precision). L or l specifies long double (quad precision). The default type (unsuffixed) is double.

Examples

3.28e+3f     float constant = 3280
6.E2F float constant = 600
201e1L long double constant = 2010
4.8 double constant = 4.8

Integer Constants

Integer constants represent integer values.

Syntax

integer-constant ::=
decimal-constant [integer-suffix]
octal-constant [integer-suffix]
hex-constant [integer-suffix]

decimal-constant ::=
nonzero-digit
decimal-constant digit

octal-constant ::=
0
octal-constant octal-digit

hexadecimal-constant ::=
0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit

nonzero-digit ::= any character from the set
1 2 3 4 5 6 7 8 9


octal-digit ::= any character from the set
0 1 2 3 4 5 6 7


hexadecimal-digit ::= any character from the set
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F

integer-suffix ::=
unsigned-suffix [length-suffix]
length-suffix [unsigned-suffix]

unsigned-suffix ::= any character from the set
u U

length-suffix ::=
long-suffix
long-long-suffix

long-suffix ::= any character from the set
l L
long-long-suffix ::= any character from the set
ll LL Ll lL
NOTE: The u and U suffixes are available only in ANSI mode (-Aa option).
NOTE: The ll, LL, Ll, and lL suffix is not available under strict ANSI (-Aa) compilation mode.

Description

An integer constant begins with a digit, but has no period or exponent part. It may have a prefix that specifies its base (decimal, octal, or hexadecimal) and suffix that specifies its type.

Refer to Chapter 10 “HP C/HP-UX Implementation Topics” for information on the size and type of integer constants.

Octal constants begin with a zero and can contain only octal digits. Several examples of octal constants are:

077  01L  01234567 0222l

Hexadecimal constants begin with either 0x or 0X. The case of the x character makes no difference to the constant's value. The following are examples of hexadecimal constants:

0xACE   0XbAf  0x12L

The suffix L or l stands for long. The suffix ll, LL, lL, or Ll stands for long long. The suffix U or u stands for unsigned. These suffixes can be used on all three types of integer constants (decimal, octal, and hexadecimal).

The type of an integer constant is the first of the corresponding list in which its value can be represented, as shown:

  • Unsuffixed decimal: int, unsigned long int.

  • Unsuffixed octal or hexadecimal: int, unsigned int.

  • Suffixed by the letter u or U: unsigned int.

  • Suffixed by the letter l or L: long int, unsigned long int.

  • Suffixed by both the letters u or U and l or L: unsigned long int.

  • Suffixed by the letters ll, LL, Ll, or lL: long long int, unsigned long long int.

  • Suffixed by both the letters u or U and ll, LL, Ll, or lL: unsigned long long int.

Examples

0xFFFFu unsigned hexadecimal integer
4196L signed long decimal integer
0X89ab signed hexadecimal integer
047L signed long octal integer
64U unsigned decimal integer
15 signed 32-bit decimal integer
15L signed long (32-bit) decimal integer
15LL signed 64-bit decimal integer
15U unsigned decimal integer
15UL unsigned long decimal integer
15LU unsigned long decimal integer
15ULL unsigned long long decimal integer
0125 signed 32-bit octal integer
0125ll signed 64-bit octal integer

Enumeration Constants

Enumeration constants are identifiers defined to have an ordered set of integer values.

Syntax

enumeration-constant ::= identifier

Description

The identifier must be defined as an enumerator in an enum definition. Enumeration constants are specified when the type is defined. An enumeration constant has type int.

Character Constants

A character constant is a constant that is enclosed in single quotes.

Syntax

character-constant ::=
'c-char-sequence'
L'c-char-sequence'

c-char-sequence ::=
c-char
c-char-sequence c-char

c-char ::=
any character in the source character set except
the single-quote ', backslash \, or new-line character
escape-sequence
escape-sequence ::=
simple-escape-sequence
octal-escape-sequence
hexadecimal-escape-sequence

simple-escape-sequence ::= one of
\' \" \? \\
\a \b \f \n \r \t \v

octal-escape-sequence ::=
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit

hexadecimal-escape-sequence ::=
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
NOTE: \a and \? are available only in ANSI mode.

Description

There are two types of character constants — integral character constants and wide character constants.

Integral character constants are of type int. They do not have type char. However, because a char is normally converted to an int in an expression, this seldom is a problem. The contents can be ASCII characters, octal escape sequences, or hexadecimal escape sequences.

Octal escape sequences consist of a backslash, ( \ ) followed by up to three octal digits. Hexadecimal escape sequences also start with a backslash, which is followed by lowercase x and any number of hexadecimal digits. It is terminated by any non-hexadecimal characters.

The digits of the escape sequences are converted into a single 8-bit character and stored in the character constant at that point. For example, the following character constants have the same value:

 'A'        '\101'      '\x41'

They all represent the decimal value 65.

Character constants are not restricted to one character; multi-character constants are allowed. The value of an integral character constant containing more than one character is computed by concatenating the 8-bit ASCII code values of the characters, with the leftmost character being the most significant. For example, the character constant 'AB' has the value 256*'A'+'B' = 256*65+66 = 16706. Only the rightmost four characters participate in the computation.

Wide character constants (type wchar_t) are of type unsigned int. A wide character constant is a sequence of one or more multibyte characters enclosed in single quotes and prefixed by the letter L. The value of a wide character constant containing a single multibyte character is a member of the extended execution character set whose value corresponds to that of the multibyte character. The value of a multibyte character can be found by calling the function mbtowc.

For multi-character wide character constants, the entire content of the constant is extracted into an unsigned integer and the resulting character is represented by the final value.

Some characters are given special representation in escape sequences. These are nonprinting and special characters that programmers often need to use (listed in Table 2-1 “Special Characters ”).

Table 2-1 Special Characters

Character

Description

\n

New line

\t

Horizontal tab

\v

Vertical tab

\b

Backspace

\r

Carriage return

\f

Form feed

\\

Backslash character

\'

Single quote

\'

Double quote

\a

Audible or visible alert (control G)

\?

Question mark character '?'

 

Examples

'a'        represents the letter a, the value 97
'\n' represents the newline character, the value 10
'\?' represents a question mark, the value 63
'7' represents the character 7, the value 55
'\0' represents the null character, the value 0
'\101' represents the letter A, the value 65