From Ian James Abbott · 1996-04-10 07:32 · #75288 in R25C
In reply to C Data Types & Variables by Matthew Probert
From: jambo@spuddy.mew.co.uk (Ian James Abbott)
Yo Matthew!
>There are four basic types of data in the C language; character, integer,
>floating point, and valueless that are referred to by the C key words;
>"char", "int", "float" and "void" respectively.
Though "char" is just another sort of integer. Also, "double" does
appear above, and although it is effectively a "long float", there is no
such type as "long float" in ANSI C.
All the above are 'scalar' types and 'arithmetic' types, apart from
"void", which is an incomplete type that cannot be completed and which
has an empty set of values. Pointer types are also 'scalar'.
>To the basic data types may be added the type modifiers; signed, unsigned,
>long and short to produce further data types. By default data types are
They only apply to the arithmetic types and only the following
combinations are defined:
char,
signed char.
unsigned char,
short, short int, signed short, signed short int,
unsigned short, unsigned short int,
signed, int, signed int,
unsigned, unsigned int,
long, long int, signed long, signed long int,
unsigned long, unsigned long int,
float,
double,
long double
All those on the same line above are the same type and distinct from
those on other lines. In particular, all three "char" types are distinct
from each other, though a plain "char" has the same range as one of the
other two.
>assumed signed, and the signed modifier is rarely used, unless to overide a
>compiler switch defaulting a data type to unsigned.
It is required when you specifically want a "signed char" rather than a
plain "char". It is also required when you want a signed bit-field
rather than a plain one.
>The size of each data type varies from one hardware platform to another, but
>the least range of values that can be held is described in the ANSI standard
>as follows;
>
>
>TYPE SIZE Range
SIZE is in bits of course!
>
>char 8 -127 to 127
Correction:
char 8 0 to 127
signed char 8 -127 to 127
(Actually the range of "char" is the same as either "signed char" or
"unsigned char", but since it isn't defined which, you can only assume
the overlapping region of the minimum range of each.)
>unsigned char 8 0 to 255
short int 16 -32767 to 32767
unsigned short int 16 0 to 65535
>int 16 -32767 to 32767
>unsigned int 16 0 to 65535
>long int 32 -2147483647 to 2147483647
>unsigned long int 32 0 to 4294967295
>float 32 Six digit precision
>double 64 Ten digit precision
>long double 80 Ten digit precision
>
>
>In practice, this means that the data type 'char' is particularly suitable
>for storing flag type variables, such as status codes, which have a limited
>range of values. The 'int' data type can be used, but if the range of values
>does not exceed 127 (or 255 for an unsigned char), then each declared
>variable would be wasting storage space.
If the range of values required does not exceed +/- 32767 then 'short
int' can be used. On many platforms, this is half the size of an 'int'.
>Which real number data type to use: 'float', 'double' or 'long double' is
>another tricky question. When numeric accuracy is required, for example in
>an accounting application, the instinct would be to use the 'long double',
>but this requires at least 10 bytes of storage space for each variable. And
>real numbers are not as precise as integers anyway, so perhaps one should
>use integer data types instead and work around the problem. The data type
Yes, floating point types should *definitely* be avoided for accounting
applications. COBOL doesn't have floating point types for good reason!
>So, to declare a string thirty characters long, and called 'name' you would
>use the declaration;
>
> char name[30];
Be sure to allow for the null terminator if placing a null-terminated
string into such a character array.
>If a second source module wished to access the variable 'error' it would
>need to declare 'error' as an 'extern' global variable thus;
>
>extern int error;
This is best done in a common include file to allow cross-checking of
types of variables for consistency across modules. Linkers don't usually
give a toss.
>In this example the variable 'x' can only store a value between -127 and
>128, so the figure 5000 will NOT be assigned to the variable 'x'. Rather the
>value 136 will be assigned!
Most compilers will issue a warning if an assigned literal value is out
of range.
>Access modifiers:
>
>There are two access modifiers; 'const' and 'volatile'. A variable declared
>to be 'const' may not be changed by the program, whereas a variable declared
>as type as type 'volatile' may be changed by the program. In addition,
>declaring a variable to be volatile prevents the C compiler from allocating
>the variable to a register, and reduces the optimization carried out on the
>variable.
Specifically, 'volatile' informs the compiler that the value of the
object may change unexpectedly outside the current execution sequence
(e.g. by a signal handler, or it may be a memory-mapped hardware
register).
>Static variables are only accessible within the code block that declared
>them, and additionally if the variable is local, rather than global, they
>retain their old value between subsequent calls to the code block.
You are mixing up scope and storage class in the above paragraph. Any
variable declared in a code block is local, unless it is declared
'extern'. The scope of the declaration is to the end of the code block
in which it is declared. If variables are declared in a code block as
'static', the it has "static storage duration" and retains it's last
value when the block is re-entered. External variables also have "static
storage duration". Variables declared static outside a function have
"static storage duration" and "internal linkage" (cannot be accessed
from outside the module). Variables declared outside a function which
are not declared static have "static storage duration" and "external
linkage".
--
* Meeeow! Call Spuddy on +44 1268 515441 for FREE mail & Usenet access. *
--- ifmail v.2.8c
* Origin: Spud's Public Usenet Domain (2:257/108.12@fidonet)