HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 2 Program Organization

Constants

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

There are four types of constants in C:

Every constant has two properties: value and type. For example, the constant 15 has value 15 and type int.

Integer Constants

HP C supports three forms of integer constants:

decimal

One or more digits from 0-9. The constant must not start with a 0.

octal

One or more digits from 0-7. The constant must begin with 0.

hexadecimal

One or more hexadecimal digits from 0-9, a-f, or A-F. The constant must begin with 0x or 0X.

An integer constant is a simple number like 12. It is not an integer variable (like x or y) or an integer expression.

The data type assigned to an integer constant is the first in which it will fit from the list on the right for the constant declaration on the left:

Table 2-3 Convention Summary

Constant

Assigned Data Type

decimal (no suffix)

int, long int, unsigned long int

octal or hex (no suffix)

int, unsigned int, long, unsigned long

letter u or U suffix

unsigned int, unsigned long int

letter l or L suffix

long, unsigned long

both letters u or U and

unsigned long l or L suffix:

letters ll or LL suffix:

long long, unsigned long long

both letters u or U and ll or LL suffix:

unsigned long long

 

Integer constants may not contain any punctuation such as commas or periods.

Examples of Integer Constants

The following examples show some legal constants in decimal, octal, and hexadecimal form:

Table 2-4 Convention Summary

Decimal

Octal

Hexadecimal

3

003

0x3

8

010

0x8

15

017

0xF

16

020

0x10

21

025

0x15

-87

-0127

-0x57

187

0273

0xBB

255

0377

0xff

 

Floating-Point Constants

A floating-point constant is any number that contains a decimal point and/or exponent sign for scientific notation.

The number may be followed by an f or F, to signify that it is of type float, or by an l or L, to signify that it is of type long double. If the number does not have a suffix, it is of type double even if it can be accurately represented in four bytes.

If the magnitude of a floating-point constant is too great or too small to be represented in a double, the C compiler will substitute a value that can be represented. This substitute value is not always predictable.

You may precede a floating-point constant with the unary plus or minus operator to make its value positive or negative.

Scientific Notation

Scientific notation is a useful shorthand for writing lengthy floating-point values. In scientific notation, a value consists of two parts: a number called the mantissa followed by a power of 10 called the characteristic (or exponent).

The letter e or E, standing for exponent, is used to separate the two parts.

The floating-point constant 3e2, for instance, is interpreted as 3*(102), or 300. Likewise, the value -2.5e-4 is interpreted as -2.5/(104), or -0.00025.

Examples of Floating-Point Constants

Here are some examples of legal and illegal floating-point constants.

Table 2-5 Floating-Point Constants

Constant

Legal or Illegal

3.

legal

35

legal — interpreted as an integer.

3.141

legal

3,500.45

illegal — commas are illegal.

.3333333333

legal

4E

illegal — the exponent must be followed by a number

0.3

legal

-3e2

legal

4e3.6

illegal — the exponent must be an integer

3.0E5

legal

+3.6

legal

0.4E-5

legal

 

Character Constants

A character constant is any printable character or legal escape sequence enclosed in single quotes. A character constant can begin with the letter L to indicate that it is a wide character constant; this notation is ordinarily used for characters in an extended character set. In HP C, an ordinary character constant occupies one byte of storage; a wide character constant occupies the rightmost byte of a 4-byte integer.

The value of a character constant is the integer ISO Latin-1 value of the character. For example, the value of the constant x is 120.

Escape Sequences

HP C supports several escape sequences:

Table 2-6 Character Escape Codes

Escape Code

Character

What it Does

\a

Audible alert

Rings the terminal's bell.

\b

Backspace

Moves the cursor back one space.

\f

Form feed

Moves the cursor to the next logical page.

\n

Newline

Prints a newline.

\r

Carriage return

Prints a carriage return.

\t

Horizontal tab

Prints a horizontal tab.

\v

Vertical tab

Prints a vertical tab.

\\

Backslash

Prints a backslash.

\?

Question mark

Prints a question mark.

\\u2019

Single quote

Prints a single quote.

\"

Double quote

Prints a double quote.

 

The escape sequences for octal and hexadecimal numbers are commonly used to represent characters. For example, if ISO Latin-1 representations are being used, the letter a may be written as \141 or \x61 and Z as \132 or \x5A. This syntax is most frequently used to represent the null character as \0. This is exactly equivalent to the numeric constant zero (0). When you use the octal format, you do not need to include the zero prefix as you would for a normal octal constant.

Multi-Character Constants

Each character in an ordinary character constant takes up one byte of storage; therefore, you can store up to a 4-byte character constant in a 32-bit integer and up to a 2-byte character constant in a 16-bit integer.

For example, the following assignments are legal:

{
   char   x;                 /* 1-byte integer */
   unsigned short int si;    /* 2-byte integer */
unsigned long int li;     /* 4-byte integer */

/* the following two assignments are portable: */
   x = 'j';                /* 1-byte character constant */
   li = L'j';                /* 4-byte wide char constant */

/* the following two assignments are not portable,
    and are not recommended: */
    si = 'ef';      /* 2-character constant */
    li = 'abcd';    /* 4-character constant */
}

The variable si is assigned the value of e and f, where each character takes up 8 bits of the 16-bit value. The HP C compiler places the last character in the rightmost (least significant) byte. Therefore, the constant ef will have a hexadecimal value of 6566. Since the order in which bytes are assigned is machine dependent, other machines may reverse the order, assigning f to the most significant byte. In that case, the resulting value would be 6665. For maximum portability, do not use multi-character constants. Use character arrays instead.

String Constants

A string constant is any series of printable characters or escape characters enclosed in double quotes. The compiler automatically appends a null character (\0) to the end of the string so that the size of the array is one greater than the number of characters in the string. For example,

"A short string"

becomes an array with 15 elements:

Figure 2-1 String Constants

String Constants

Like a character constant, a string constant can begin with the letter L to indicate that it is a string constant in an extended character set.

To span a string constant over more than one line, use the backslash character (\), also called the continuation character. The following, for instance, is legal:

strcpy(string,"This is a very long string that requires more \
than one line");

Note that if you indent the second line, the spaces will be part of the string.

The compiler concatenates adjacent string constants. Therefore, you can also span a string constant over one line as shown:

strcpy(string, "This is a very long string that requires more "
"than one line");

When you indent the second line with this method, the spaces are not part of the string.

The type of a string is array of char, and strings obey the same conversion rules as other arrays. Except when a string appears as the operand of sizeof or as an initializer, it is converted to a pointer to the first element of the string. Note also that the null string, "" is legal, and contains a single trailing null character.

© Hewlett-Packard Development Company, L.P.