C (Basic) Data Types: - Different Data Representations Need Different Types in Programming
C (Basic) Data Types: - Different Data Representations Need Different Types in Programming
www.tenouk.com, 1/31
C BASIC (DATA) TYPES
www.tenouk.com, 2/31
C BASIC (DATA) TYPES
Other
Type Size in Bits Comments
Names
Primitive Types in ANSI C (C89)/ISO C (C90)
www.tenouk.com, 3/31
C BASIC (DATA) TYPES
A sample output.
www.tenouk.com, 4/31
C BASIC (DATA) TYPES
Can store integers in the range:
-32767 ~ 32767 (216 / 2) portably.
short int,
16, size of Reduce memory usage though
short signed short,
char the resulting executable may be
signed short int
larger and probably slower as
compared to using int.
Can store integers in the range: 0
~ 65535 (216) portably.
unsigned Same as short Used to reduce memory usage
unsigned short int
short but unsigned though the resulting executable
may be larger and probably slower
as compared to using int.
Basic signed integer type.
Represent a typical processors
16, size of data size which is word-size signed,
int
short An integral data-type. signed int
Can store integers in the range:
-32767 ~ 32767 (216 / 2) portably.
Same as int but Can store integers in the range: 0
unsigned int unsigned
unsigned. ~ 65535 (216) portably.
short int type program example
www.tenouk.com, 5/31
C BASIC (DATA) TYPES
A sample output.
www.tenouk.com, 6/31
C BASIC (DATA) TYPES
long signed integer type.
long int,
32, size of Can store integers in the range:
long signed long,
int -2147483647 ~ 2147483647 (232 / 2)
signed long int
portably.
unsigned Same as long Can store integers in the range: 0 ~ unsigned long
long but unsigned 4294967295 (232) portably. int
Used to reduce memory usage when
the values used do not vary widely.
The format used is implementation
float size of char
defined and unnecessarily obeys the
IEEE 754 single-precision format.
unsigned cannot be specified.
Typical floating-point data type used
by processor.
The format used is implementation
double size of float
defined and unnecessarily obeys the
IEEE 754 double-precision format.
unsigned cannot be specified.
size of
long double unsigned cannot be specified.
double
www.tenouk.com, 8/31
C BASIC (DATA) TYPES
www.tenouk.com, 9/31
C BASIC (DATA) TYPES
A sample output.
www.tenouk.com, 10/31
C BASIC (DATA) TYPES
It is a typedef represents
Signed integer types the signed integer type with
capable of largest possible range.
intmax_t representing any If you want an integer with
value of any signed the widest range possible on
integer type. the platform on which it is
being used.
It is a typedef represents
Unsigned integer
the unsigned integer type
types capable of
with largest possible range.
representing any
uintmax_t If you want an integer with
value of any
the widest range possible on
unsigned integer
the platform on which it is
type
being used.
www.tenouk.com, 12/31
C BASIC (DATA) TYPES
Unfortunately this is not supported by MSVC++
< 2012
inttypes.h vs. stdint.h: The C99 standard
says that inttypes.h includes stdint.h, so
there's no need to include stdint.h separately in
a standard environment.
Some implementations have inttypes.h but not
stdint.h.
VS/VC++ users may want to use msinttypes.
Other references,
1. http://www.qnx.com/developers/docs/6.5.0/index.jsp?topic=/com.qnx.doc.dinkum
_en_c99/stdint.html
2. http://pubs.opengroup.org/onlinepubs/007904975/basedefs/stdint.h.html
3. http://publib.boulder.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtr
ef%2Fstdinth.htm
www.tenouk.com, 13/31
C BASIC (DATA) TYPES
Actual size of integer types varies by implementation: Windows, Linux,
BSD etc.
The only guarantee is that the long long is not smaller than long,
which is not smaller than int, which is not smaller than short.
int should be the integer type that the target processor is most efficient
working with. For example, all types can be 64-bit.
Actual size of floating point types also varies by implementation.
The only guarantee is that the long double is not smaller than
double, which is not smaller than float.
The 32-bit and 64-bit IEEE 754 floating point formats should be used.
www.tenouk.com, 14/31
C BASIC (DATA) TYPES
Boolean type
www.tenouk.com, 15/31
C BASIC (DATA) TYPES
Size and pointer difference types
Both of these types are defined in the stddef.h header file (cstddef in
C++).
www.tenouk.com, 17/31
C BASIC (DATA) TYPES
Used as the return type of the sizeof() operator.
The maximum size of size_t is provided via SIZE_MAX, a
macro constant which is defined in the stdint.h header file
(cstdint in C++).
It is guaranteed to be at least 65535.
ptrdiff_t is used to represent the difference between
pointers.
Is the signed integer type of the result of subtracting two
pointers.
The type's size is chosen so that it could store the
maximum size of a theoretically possible array of any type.
On a 32-bit system ptrdiff_t will take 32 bits and on a
64-bit one - 64 bits and it is portable.
size_t and ptrdiff_t: a story ptrdiff_t program example
www.tenouk.com, 18/31
C BASIC (DATA) TYPES
A sample output.
www.tenouk.com, 19/31
C BASIC (DATA) TYPES
Interface to the properties of the basic types
www.tenouk.com, 20/31
C BASIC (DATA) TYPES
Fixed width integer types
www.tenouk.com, 21/31
C BASIC (DATA) TYPES
Exact width integer types - are guaranteed to have the
same number N of bits across all implementations.
Included only if it is available in the implementation.
Least width integer types - are guaranteed to be the
smallest type available in the implementation, that has at
least specified number N of bits. Guaranteed to be
specified for at least N=8, 16, 32, 64.
Fastest integer types - are guaranteed to be the fastest
integer type available in the implementation, that has at
least specified number N of bits. Guaranteed to be
specified for at least N=8, 16, 32, 64.
Pointer integer types - are guaranteed to be able to hold a
pointer.
Maximum width integer types - are guaranteed to be the
largest integer type in the implementation.
www.tenouk.com, 22/31
C BASIC (DATA) TYPES
www.tenouk.com, 23/31
USER DEFINED (DATA) TYPES
Keyword Size Note
sum of size
An aggregate type which can contain more
struct of each
than one different types.
member
typedef struct
tag or label is optional {
struct theEmployee { int x;
int age; int SomeArray[100];
} MyFoo;
double salary;
char department; int main()
char name[15]; {
char address[5][25]; MyFoo strctVar;
};
struct theEmployee workerRec; return 0;
}
struct newPoint {
short xPoint;
short yPoint;
} justPoint;
justPoint thePoint;
www.tenouk.com, 24/31
USER DEFINED (DATA) TYPES
An aggregate type which can contain more than
size of the
one other types. union uses shared memory
union largest
space compared to struct, so only one member
member
can be accessed at one time.
union someData
{
int pNum;
float qNum;
double rNum;
};
union someData simpleData;
union OtherData{
char aNum;
int xNum;
float fNum;
} simpleData;
simpleData saveData;
www.tenouk.com, 25/31
USER DEFINED (DATA) TYPES
www.tenouk.com, 26/31
USER DEFINED (DATA) TYPES
same as the type; typedef used to give new identifier names or alias (to
typedef being given a new simplify the long identifier names), normally used for
name aggregate defined types.
typedef unsigned char BYTE; /* Declares BYTE to be a synonym for unsigned char */
typedef float FLOAT; /* Declares FLOAT (uppercase letter) to be a synonym for unsigned float
(lowercase) */
www.tenouk.com, 28/31
DERIVED (DATA) TYPES
Use to declare a variable with
collection of identical properties or
types.
Simplify variable declaration.
In a declaration which also initializes
type [integer]
integer size of the array (including a function
type parameter declaration), the size of
(an array)
the array (the integer) can be
omitted, which is called unsized.
type [ ] is not the same as type*.
Only under some circumstances one
can be converted to the other.
char cName1[ ] =
{'a','r','r','a','y'};
int fstudentNumber[3] = {4,7,1};
char cName2[ ] = {"array"};
int nrowandColumn[1][2] = {34, 21}; char cName3[6] = "array";
int nlongHeightWidth[3][4][5] = 0; int nrowCol[2][3] = {4,2,3,7,2,8};
www.tenouk.com, 29/31
DERIVED (DATA) TYPES
www.tenouk.com, 30/31
End of the C data types
www.tenouk.com, 31/31