|< C/C++ Statements, Expressions & Operators 3 | Main | C & C++ Functions >| Site Index | Download |


 

STANDARD C TO IMPLEMENTATION SPECIFIC: SOME STORY

 

The second edition, ISO/IEC 9899:1999 (C99)  cancels and replaces the first edition, ISO/IEC 9899:1990, as amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC 9899/AMD1:1995, and ISO/IEC 9899/COR2:1996. In the standard there are many recommendations that supposed to be implemented by vendors. In this module we will see some Standard C definitions and how it is implemented in VC++ (VC++ 2005 in this case) by Microsoft.

 

 

C Constants

A "constant" is a number, character, or character string that can be used as a value in a program. Use constants to represent floating-point, integer, enumeration, or character values that cannot be modified.

 

  1. floating-point-constant
  2. integer-constant
  3. enumeration-constant
  4. character-constant

 

Constants are characterized by having a value and a type. Floating-point, integer, and character constants are discussed in the next three sections. Enumeration constants are described in Enumeration Declarations.

Integer-constant

An "integer constant" is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value. Use integer constants to represent integer values that cannot be changed. The following syntax implemented by Microsoft also defined by ISO/IEC 9899:1999.

Syntax

integer-constant:

decimal-constant integer-suffix opt

octal-constant integer-suffix opt

hexadecimal-constant integer-suffix opt

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: one of

1 2 3 4 5 6 7 8 9

octal-digit: one of

0 1 2 3 4 5 6 7

hexadecimal-digit: one of

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 long-suffix opt

long-suffix unsigned-suffix opt

unsigned-suffix: one of

u          U

long-suffix: one of

l           L

64-bit integer-suffix:

i64

 

The following table lists the suffix that defined by ISO, also implemented by Microsoft. You can find it usage in variable declaration, the standard input function (scanf()/scanf_s()) and standard output function, printf()/printf_s()). You will find the examples in the C lab worksheet practice.

 

Suffix

Decimal Constant

Octal or Hexadecimal Constant

none

int

long int

long long int

int

unsigned int

long int

unsigned long int

long long int

unsigned long long int

u or U

unsigned int

unsigned long int

unsigned long long int

unsigned int

unsigned long int

unsigned long long int

l or L

long int

long long int

long int

unsigned long int

long long int

unsigned long long int

Both u or U and l or L

unsigned long int

unsigned long long int

unsigned long int

unsigned long long int

ll or LL

long long int

long long int

unsigned long long int

Both u or U and ll or LL

unsigned long long int

unsigned long long int

 

Integer constants are positive unless they are preceded by a minus sign (–). The minus sign is interpreted as the unary arithmetic negation operator. An integer constant begins with a digit, but has no period or exponent part. It may have a prefix that specifies its base and a suffix that specifies its type. If an integer constant begins with 0x or 0X, it is hexadecimal. If it begins with the digit 0, it is octal. Otherwise, it is assumed to be decimal. The following lines are equivalent:

 

0x1C   /* = Hexadecimal representation for decimal 28 */

034     /* = Octal representation for decimal 28 */

 

No white-space characters can separate the digits of an integer constant. These examples show valid decimal, octal, and hexadecimal constants.

 

/* Decimal Constants */

10

132

32179

 

/* Octal Constants */

012

0204

076663

 

/* Hexadecimal Constants */

0xa or 0xA

0x84

0x7dB3 or 0X7DB3

Numerical limits

An implementation is required to document all the limits specified in this subclause, which are specified in the headers <limits.h> and <float.h>. Additional limits are specified in <stdint.h>.

Sizes of integer types <limits.h> (ISO/IEC 9899:1999)

An implementation is required to document all the limits specified in this subclause, which are specified in the headers <limits.h> and <float.h>. Additional limits are specified in <stdint.h>.

 

Constant

Meaning

Value

CHAR_BIT

number of bits for smallest object that is not a bit-field (byte)

8

SCHAR_MIN

minimum value for an object of type signed char

127 // -(27 - 1)

SCHAR_MAX

maximum value for an object of type signed char

+127 // 27 - 1

UCHAR_MAX

maximum value for an object of type unsigned char

255 // 28 - 1

CHAR_MIN

minimum value for an object of type char

a, see below

CHAR_MAX

maximum value for an object of type char

a, see below

MB_LEN_MAX

maximum number of bytes in a multibyte character, for any supported locale

1

SHRT_MIN

minimum value for an object of type short int

-32767 // -(215 - 1)

SHRT_MAX

maximum value for an object of type short int

+32767 // 215 - 1

USHRT_MAX

maximum value for an object of type unsigned short int

65535 // 216 - 1

INT_MIN

minimum value for an object of type int

-32767 // -(215 - 1)

INT_MAX

maximum value for an object of type int

+32767 // 215 - 1

UINT_MAX

maximum value for an object of type unsigned int

65535 // 216 - 1

LONG_MIN

minimum value for an object of type long int

-2147483647 // -(231 - 1)

LONG_MAX

maximum value for an object of type long int

+2147483647 // 231 - 1

ULONG_MAX

maximum value for an object of type unsigned long int

4294967295 // 232 - 1

LLONG_MIN

minimum value for an object of type long long int

-9223372036854775807 // -(263 - 1)

LLONG_MAX

maximum value for an object of type long long int

+9223372036854775807 // 263 - 1

ULLONG_MAX

maximum value for an object of type unsigned long long int

18446744073709551615 // 264 - 1

 

a - If the value of an object of type char is treated as a signed integer when used in an expression, the value of CHAR_MIN shall be the same as that of SCHAR_MIN and the value of CHAR_MAX shall be the same as that of SCHAR_MAX. Otherwise, the value of CHAR_MIN shall be 0 and the value of CHAR_MAX shall be the same as that of UCHAR_MAX. The value UCHAR_MAX shall equal 2CHAR_BIT - 1.

Microsoft Implementation

The limits for integer types are listed in the following table. These limits are defined in the standard header file LIMITS.H. Microsoft C also permits the declaration of sized integer variables, which are integral types of size 8-, 16-, or 32-bits.

Limits on Integer Constants

Constant

Meaning

Value

CHAR_BIT

Number of bits in the smallest variable that is not a bit field.

8

SCHAR_MIN

Minimum value for a variable of type signed char.

–128

SCHAR_MAX

Maximum value for a variable of type signed char.

127

UCHAR_MAX

Maximum value for a variable of type unsigned char.

255 (0xff)

CHAR_MIN

Minimum value for a variable of type char.

–128; 0 if /J option used

CHAR_MAX

Maximum value for a variable of type char.

127; 255 if /J option used

MB_LEN_MAX

Maximum number of bytes in a multicharacter constant.

5

SHRT_MIN

Minimum value for a variable of type short.

–32768

SHRT_MAX

Maximum value for a variable of type short.

32767

USHRT_MAX

Maximum value for a variable of type unsigned short.

65535 (0xffff)

INT_MIN

Minimum value for a variable of type int.

–2147483647 – 1

INT_MAX

Maximum value for a variable of type int.

2147483647

UINT_MAX

Maximum value for a variable of type unsigned int.

4294967295 (0xffffffff)

LONG_MIN

Minimum value for a variable of type long.

–2147483647 – 1

LONG_MAX

Maximum value for a variable of type long.

2147483647

ULONG_MAX

Maximum value for a variable of type unsigned long.

4294967295 (0xffffffff)

 

If a value exceeds the largest integer representation, the Microsoft compiler generates an error.

Sizes of Fundamental Types

The following table summarizes the storage associated with each basic type.

 

Type

Storage

char, unsigned char, signed char

1 byte

short, unsigned short

2 bytes

int, unsigned int

4 bytes

long, unsigned long

4 bytes

float

4 bytes

double

8 bytes

long double

8 bytes

 

The C data types fall into general categories. The "integral types" include char, int, short, long, signed, unsigned, and enum. The "floating types" include float, double, and long double. The "arithmetic types" include all floating and integral types.

C Sized Integer Types

Microsoft Implementation

Microsoft C features support for sized integer types. You can declare 8-, 16-, 32-, or 64-bit integer variables by using the __intn type specifier, where n is the size, in bits, of the integer variable. The value of n can be 8, 16, 32, or 64. The following example declares one variable of each of the four types of sized integers:

 

__int8 nSmall;         // declares 8-bit integer

__int16 nMedium;  // declares 16-bit integer

__int32 nLarge;     // declares 32-bit integer

__int64 nHuge;      // declares 64-bit integer

 

The first three types of sized integers are synonyms for the ANSI types that have the same size, and are useful for writing portable code that behaves identically across multiple platforms. Note that the __int8 data type is synonymous with type char, __int16 is synonymous with type short, and __int32 is synonymous with type int. The __int64 type has no equivalent ANSI counterpart.

C Floating-Point Constants

A "floating-point constant" is a decimal number that represents a signed real number. The representation of a signed real number includes an integer portion, a fractional portion, and an exponent. Use floating-point constants to represent floating-point values that cannot be changed. The following syntax implemented by Microsoft and also defined by ISO/IEC 9899:1999.

Syntax

floating-point-constant:

fractional-constant exponent-part opt floating-suffix opt

digit-sequence exponent-part floating-suffix opt

fractional-constant:

digit-sequence opt . digit-sequence

digit-sequence.

exponent-part:

e sign opt digit-sequence

E sign opt digit-sequence

sign : one of

+ –

digit-sequence:

digit

digit-sequence digit

floating-suffix : one of

f  l  F  L

The ISO said that a floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type. The components of the significand 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 components of the exponent part are an e, E, p, or P (p and P is for binary-exponent-part) followed by an exponent consisting of an optionally signed digit sequence. Either the whole-number part or the fraction part has to be present; for decimal floating constants, either the period or the exponent part has to be present.

An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.

You can omit either the digits before the decimal point (the integer portion of the value) or the digits after the decimal point (the fractional portion), but not both. You can leave out the decimal point only if you include an exponent. No white-space characters can separate the digits or characters of the constant. The following examples illustrate some forms of floating-point constants and expressions:

 

15.75

1.575E1   /* = 15.75   */

1575e-2   /* = 15.75   */

-2.5e-3     /* = -0.0025 */

25E-4      /* =  0.0025 */

 

Floating-point constants are positive unless they are preceded by a minus sign (–). In this case, the minus sign is treated as a unary arithmetic negation operator. Floating-point constants have type float, double, long, or long double.

A floating-point constant without an f, F, l, or L suffix has type double. If the letter f or F is the suffix, the constant has type float. If suffixed by the letter l or L, it has type long double. For example:

 

100   /* has type double */

100L  /* has type long double */

100F  /* has type float       */

 

Note that the Microsoft C compiler maps long double to type double. You can omit the integer portion of the floating-point constant, as shown in the following examples. The number .75 can be expressed in many ways, including the following:

 

.0075e2

0.075e1

.075e1

75e-2

Characteristics of floating types <float.h>

The Limits on Floating-Point Constants (ISO/IEC 9899:1999)

The characteristics of floating types are defined in terms of a model that describes a representation of floating-point numbers and values that provide information about an implementation’s floating-point arithmetic.16) The following parameters are used to define the model for each floating-point type:

 

Symbol

Description

s

Sign (±1)

b

Base or radix of exponent representation (an integer > 1)

e

Exponent (an integer between a minimum emin and a maximum emax)

p

Precision (the number of base-b digits in the significand)

fk

Nonnegative integers less than b (the significand digits)

 

A floating-point number (x) is defined by the following model:

 

 

 

Constant

Meaning

Value

FLT_RADIX

radix of exponent representation, b

2

FLT_MANT_DIG

DBL_MANT_DIG

LDBL_MANT_DIG

number of base-FLT_RADIX digits in the floating-point significand, p

-

DECIMAL_DIG

number of decimal digits, n, such that any floating-point number in the widest supported floating type with pmax radix b digits can be rounded to a floating-point number with n decimal digits and back again without change to the value

10

FLT_DIG

DBL_DIG

LDBL_DIG

number of decimal digits, q, such that any floating-point number with q decimal digits can be rounded into a floating-point number with p radix b digits and back again without change to the q decimal digits

6

10

10

FLT_MIN_EXP

DBL_MIN_EXP

LDBL_MIN_EXP

minimum negative integer such that FLT_RADIX raised to one less than that power is a normalized floating-point number, emin

-

FLT_MIN_10_EXP

DBL_MIN_10_EXP

LDBL_MIN_10_EXP

minimum negative integer such that 10 raised to that power is in the range of normalized floating-point numbers

-37

-37

-37

FLT_MAX_EXP

DBL_MAX_EXP

LDBL_MAX_EXP

maximum integer such that FLT_RADIX raised to one less than that power is a representable finite floating-point number, emax

-

FLT_MAX_10_EXP

DBL_MAX_10_EXP

LDBL_MAX_10_EXP

maximum integer such that 10 raised to that power is in the range of representable finite floating-point numbers

+37

+37

+37

The values given in the following list shall be replaced by constant expressions with implementation-defined values that are greater than or equal to those shown:

FLT_MAX

DBL_MAX

LDBL_MAX

maximum representable finite floating-point number

1E+37

1E+37

1E+37

The values given in the following list shall be replaced by constant expressions with implementation-defined (positive) values that are less than or equal to those shown:

FLT_EPSILON

DBL_EPSILON

LDBL_EPSILON

the difference between 1 and the least value greater than 1 that is representable in the given floating point type

1E-5

1E-9

1E-9

FLT_MIN

DBL_MIN

LDBL_MIN

minimum normalized positive floating-point number

1E-37

1E-37

1E-37

Microsoft Implementation

Limits on the values of floating-point constants are given in the following table. The header file FLOAT.H contains this information.

Limits on Floating-Point Constants

Constant

Meaning

Value

FLT_DIGDBL_DIGLDBL_DIG

Number of digits, q, such that a floating-point number with q decimal digits can be rounded into a floating-point representation and back without loss of precision.

6 15 15

FLT_EPSILONDBL_EPSILONLDBL_EPSILON

Smallest positive number x, such that x + 1.0 is not equal to 1.0

1.192092896e–07F 2.2204460492503131e–016 2.2204460492503131e–016