C is a typed language. Each variable is given a specific type which defines what values it can represent, how its data is stored in memory, and what operations can be performed on it. By forcing the programmer to explicitly define a type for all variables and interfaces, the type system enables the compiler to catch type-mismatch errors, thereby preventing a significant source of bugs.
There are three basic types in the C language: characters, and integer and floating-point numbers.
The numerical types come in several of sizes. Table 2.1 shows a list of C types and their typical sizes, although the sizes may vary from platform to platform. Nearly all current machines represent an int with at least 32-bits and many now use 64-bits. The size of an int generally represents the natural word-size of a machine; the native size with which the CPU handles instructions and data.
C Data Types
|
|
Char
Int
Short
Int
Long
Int
Float
Double
Long
Double
|
Usually
8-bits (1 byte)
Usually
the nature word size for a machine or OS (e.g., 16,32,64, etc.,)
At
least 16 bits
At
least 32 bits
Usually
32 bits
Usually
64 bits
Usually
at least 64 bits
|
With regard to size, the standard merely states that a short int be at least 16-bits, a long int
at least 32-bit, and
short int ≤ int ≤ long int
The standard says nothing about the size of floating-point numbers except that
float ≤ double ≤ long double.
A program to print the range of values for certain data types is shown below. The parameters such as INT_MIN can be found in standard headers limits.h and float.h.
#include <limits.h> /* integer specifications */
#include <float.h> /* floating-point specifications */
/* Look at range limits of certain types */
int main (void)
{
printf("Integer range:\t%d\t%d\n", INT MIN, INT MAX);
printf("Long range:\t%ld\t%ld\n", LONG MIN, LONG MAX);
printf("Float range:\t%e\t%e\n", FLT MIN, FLT MAX);
printf("Double range:\t%e\t%e\n", DBL MIN, DBL MAX);
printf("Long double range:\t%e\t%e\n", LDBL MIN, LDBL MAX);
printf("Float-Double epsilon:\t%e\t%e\n", FLT EPSILON, DBL EPSILON);
}
Example:1
#include <stdio.h>#include <limits.h> /* integer specifications */
#include <float.h> /* floating-point specifications */
/* Look at range limits of certain types */
int main (void)
{
printf("Integer range:\t%d\t%d\n", INT MIN, INT MAX);
printf("Long range:\t%ld\t%ld\n", LONG MIN, LONG MAX);
printf("Float range:\t%e\t%e\n", FLT MIN, FLT MAX);
printf("Double range:\t%e\t%e\n", DBL MIN, DBL MAX);
printf("Long double range:\t%e\t%e\n", LDBL MIN, LDBL MAX);
printf("Float-Double epsilon:\t%e\t%e\n", FLT EPSILON, DBL EPSILON);
}
Note.
The size of a type in number of characters (which is usually equivalent to number of bytes) can be found using the sizeof operator. This operator is not a function, although it often appears like one, but a keyword. It returns an unsigned integer of type size_t, which is defined in header-file stddef.h.Example:2
#include <stdio.h>
int main (void)
/* Print the size of various types in “number-of-chars” */
{
printf("void\tchar\tshort\tint\tlong\tfloat\tdouble\n");
printf("%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\n",
sizeof(void), sizeof(char), sizeof(short), sizeof(int),
sizeof(long), sizeof(float), sizeof(double));
}
The keywords short and long are known as type qualifiers because they affect the size of a basic int type. (The qualifier long may also be applied to type double.) Note, short and long, when used on their own as in
short a;
long x;
are equivalent to writing short int and long int, respectively. Other type qualifiers2 are signed, unsigned, const, and volatile. The qualifiers signed or unsigned can apply to char or any integer type. A signed type may represent negative values; the most-significant-bit (MSB) of the number is its sign-bit, and the value is typically encoded in 2’s-complement binary. An unsigned type is always non-negative, and the MSB is part of the numerical value—doubling the maximum representable value compared to an equivalent signed type. For example, a 16-bit signed short can represent the numbers −32768 to 32767 (i.e., −215 to 215 − 1), while a 16-bit unsigned short can represent the numbers 0 to 65535 (i.e., 0 to 216−1).
Note.
Integer types are signed by default (e.g., writing short is equivalent to writing signed
short int). However, whether plain char’s are signed or unsigned by default is machine dependent.
The qualifier const means that the variable to which it refers cannot be changed.
const int DoesNotChange = 5;
DoesNotChange = 6; /* Error: will not compile */
The qualifier volatile refers to variables whose value may change in a manner beyond the normal control of the program. This is useful for, say, multi-threaded programming or interfacing to hardware; topics which are beyond the scope of this text. The volatile qualifier is not directly relevant to standard-conforming C programs, and so will not be addressed further in this text.
Finally, there is a type called void, which specifies a “no value” type. It is used as an argument for functions that have no arguments, and as a return type for functions that return no value.
No comments:
Post a Comment