A simple type specifier either specifies a (previously declared) user-defined type or a fundamental type. A fundamental type is a one that is built into the language. The following outline shows the categories of fundamental types:
Related References
Variables of type bool can hold either one of two values:
true or false. An rvalue of type bool
can be promoted to an integral type. A bool rvalue of
false is promoted to the value 0, and a bool rvalue of
true is promoted to the value 1.
The result of the equality, relational, and logical operators is of type bool: either of the Boolean constants true or false.
Use the type specifier bool and the literals true and false to make boolean logic tests. A boolean logic test is used to express the results of a logical operation. For example:
bool f(int a, int b) { return a==b; }
If a and b have the same value, f() returns true. If not, f() returns false.
Related References
The char specifier has the following syntax:
>>-+----------+--char------------------------------------------>< +-unsigned-+ '-signed---'
The char specifier is an integral type.
A char has enough storage to represent a character from the basic character set. The amount of storage allocated for a char is implementation-dependent.
You initialize a variable of type char with a character literal (consisting of one character) or with an expression that evaluates to an integer.
Use signed char or unsigned char to declare numeric variables that occupy a single byte.
For the purposes of distinguishing overloaded functions, a C++ char
is a distinct type from signed char and unsigned
char.
Examples of the char Type Specifier
The following example defines the identifier end_of_string as a constant object of type char having the initial value \0 (the null character):
const char end_of_string = '\0';
The following example defines the unsigned char variable switches as having the initial value 3:
unsigned char switches = 3;
The following example defines string_pointer as a pointer to a character:
char *string_pointer;
The following example defines name as a pointer to a character. After initialization, name points to the first letter in the character string "Johnny":
char *name = "Johnny";
The following example defines a one-dimensional array of pointers to characters. The array has three elements. Initially they are a pointer to the string "Venus", a pointer to "Jupiter", and a pointer to "Saturn":
static char *planets[ ] = { "Venus", "Jupiter", "Saturn" };
The wchar_t type specifier is an integral type that has enough storage to represent a wide character literal. (A wide character literal is a character literal that is prefixed with the letter L, for example L'x')
Related References
There are three types of floating-point variables:
To declare a data object that is a floating-point type, use the following float specifier:
>>-+-float-------+--------------------------------------------->< +-double------+ '-long double-'
The declarator for a simple floating-point declaration is an identifier. Initialize a simple floating-point variable with a float constant or with a variable or expression that evaluates to an integer or floating-point number. The storage class of a variable determines how you initialize the variable.
Examples of Floating-Point Data Types
The following example defines the identifier pi as an object of type double:
double pi;
The following example defines the float variable real_number with the initial value 100.55:
static float real_number = 100.55f;
The following example defines the float variable float_var with the initial value 0.0143:
float float_var = 1.43e-2f;
The following example declares the long double variable maximum:
extern long double maximum;
The following example defines the array table with 20 elements of type double:
double table[20];
Related References
The packed decimal data type is supported as an extension
to ISO C89 and Standard C++. The data type provides the ability to accurately represent large
numeric quantities. It can consist of an integral part, a decimal
point, a fractional part, and the suffix D. A variable of
packed decimal type can have up to sixty-three significant digits, including
integral and fractional parts.
A packed decimal data type is of the form:
>>-+-decimal--+--(--n--,--precision--)------------------------->< '-_Decimal-'
where n represents the total number of significant digits, to a maximum of value of 63, and precision represents the number of digits of precision. The value of precision must be less than or equal to n.
For example,
decimal(4,2) x = 12.34D; /* Requires #include <decimal.h> */ _Decimal(4,2) y = 12.34d; /* Declaration without including <decimal.h> */
See "Using Packed Decimal Data in a C Program," chapter 24 in ILE C/C++ Programmer's Guide.
Related References
Integer variables fall into the following categories:
The default integer type for a bit field is unsigned.
The amount of storage allocated for integer data is implementation-dependent.
The unsigned prefix indicates that the object is a nonnegative integer. Each unsigned type provides the same size storage as its signed equivalent. For example, int reserves the same storage as unsigned int. Because a signed type reserves a sign bit, an unsigned type can hold a larger positive integer value than the equivalent signed type.
The declarator for a simple integer definition or declaration is an identifier. You can initialize a simple integer definition with an integer constant or with an expression that evaluates to a value that can be assigned to an integer. The storage class of a variable determines how you can initialize the variable.
When the arguments in overloaded functions and overloaded operators are
integer types, two integer types that both come from the same group are not
treated as distinct types. For example, you cannot overload an
int argument against a signed int argument.
Examples of Integer Data Types
The following example defines the short int variable flag:
short int flag;
The following example defines the int variable result:
int result;
The following example defines the unsigned long int variable ss_number as having the initial value 438888834 :
unsigned long ss_number = 438888834ul;
Related References
The void data type always represents an empty set of values. The only object that can be declared with the type specifier void is a pointer.
When a function does not return a value, you should use void as the type specifier in the function definition and declaration. An argument list for a function taking no arguments is void.
You cannot declare a variable of type void, but you can explicitly convert any expression to type void. The resulting expression can only be used as one of the following:
Example of void Type
In the following example, the function find_max is declared as having type void.
/** ** Example of void type **/ #include <stdio.h> /* declaration of function find_max */ extern void find_max(int x[ ], int j); int main(void) { static int numbers[ ] = { 99, 54, -102, 89}; find_max(numbers, (sizeof(numbers) / sizeof(numbers[0]))); return(0); } void find_max(int x[ ], int j) { /* begin definition of function find_max */ int i, temp = x[0]; for (i = 1; i < j; i++) { if (x[i] > temp) temp = x[i]; } printf("max number = %d\n", temp); } /* end definition of function find_max */
Related References
(C) Copyright IBM Corporation 1992, 2006. All Rights Reserved.