Documente Academic
Documente Profesional
Documente Cultură
Lecture 2
C Programming Language
Lecture content
Data representation
Types
Conversions
Q&A
Data representation
All data objects in C except bit fields are represented at
run time in the computer's memory in an integral
number of abstract storage units
Each storage unit is made up of some fixed number of
bits, each of which can assume either of two values,
denoted 0 and 1
Each storage unit must be uniquely addressable and
is the same size as type char
The number of bits in a storage unit is
implementation-defined in C, but it must be large
enough to hold every character in the basic character set
The C Standard also calls storage units bytes: a storage
unit consisting of exactly eight bits
The size of a data object is the number of storage
units occupied by that data object
C Programming Language - Lecture
2
Data representation
i = 305419896 = 0x12345678
(4 bytes)
Little endian
Memory
Memory
addresses
locations
0x1200
0x78
0x1201
0x56
0x1202
0x34
0x1203
0x12
Big endianMemory
"little-endian"
Memory
architectures: the address of
addresses
locations
a 32-bit integer is also the
address of the low-order byte
0x1200
0x12
of the integer
0x1201
0x34
"big-endian" architectures
0x1202
0x56
the address of a 32-bit
integer is the address of the
0x1203
0x78
high-order byte of the
integer
C Programming Language - Lecture
2
Data representation
Types
A type is a set of values and a set of
operations on those values
A variable or expression "has type T" when its
values are constrained to the domain of T
The types of variables are established by the
variable's declaration
The types of expressions are given by the
definitions of the expression operators
The C language provides a large number of
built-in types, including integers of several
kinds, floating-point numbers, pointers,
enumerations, arrays, structures, unions, and
functions
C Programming Language - Lecture
2
Types
C Types
Type categories
Integral types
C99
enum
float, double, long double
float _Complex
double _Complex
long double
_Complex
float _Imaginary
double _Imaginary
long double
_Imaginary
T*
T []
struct {}
union {}
T ()
C99
Arithmetic types
Scalar types
Floating point
types
Pointer types
Array types
Structure types
Aggregate types
Union types
Types
unsigned
unsigned
unsigned
unsigned
short intopt
intopt
long intopt
long long intopt
C Programming Language - Lecture
2
EXAMPLES
short i, j;
long int k;
unsigned int m;
unsigned n;
8
Types
Smallest
value
Largest
Value
-32768
32767
65535
-32768
32767
65535
long int
21474836
214748364
47
8
Integer types on a 32-bit machine
unsigned long
0
42949672
int
95
Type
Smallest
Largest
value
Value
short int
unsigned short
int
int
unsigned int
C Programming Language - Lecture
2
long int
-32768
32767
65535
214748364
8
21474836
47
42949672
95
9
21474836
Types
= -1;
-1;
k = c;
10
Types
FIoating-point type specifiers:
float
double
long double (C89)
C does not dictate the sizes to be used for the floatingpoint types or even that they be different
The programmer can assume that the values represent
able in type float are a subset of those in type double,
which in turn are a subset of those in type long double
Standard C requires that the characteristics of the real
floating-point types be documented in the header file
float.h
EXAMPLES
double d;
static double pi;
float coefficients [8] ;
long double epsilon;
C Programming Language - Lecture
2
11
Types
For any type T, a pointer type "pointer to T" may be
formed
Pointer types are referred to as object pointers or
function pointers depending on whether T is an
object type or a function type.
A value of pointer type is the address of an
object or function of type T.
The two most important operators used in conjunction
with pointers are:
the address operator, &, which creates pointer values
indirection operator, *, which dereferences pointers to
access the object pointed to
12
Types
EXAMPLES
int i, j, *ip;
ip = &i;
i = 22;
j = *ip; /* j now has the value 22 */
*ip = 17; /* i now has the value 17 */
Memory
addresses
0x1200
0x1220
ip
0x1240
Variables
Memory
locations
0x00
0x00
0x00
0x00
0x00
0x00
13
Types
EXAMPLES
int i, j, *ip;
ip = &i;
i = 22;
j = *ip; /* j now has the value 22 */
*ip = 17; /* i now has the value 17 */
Memory
addresses
0x1200
0x1220
ip
0x1240
Variables
Memory
locations
0x00
0x00
0x00
0x00
0x00
0x12
14
Types
EXAMPLES
int i, j, *ip;
ip = &i;
i = 22;
j = *ip; /* j now has the value 22 */
*ip = 17; /* i now has the value 17 */
Memory
addresses
0x1200
0x1220
ip
0x1240
Variables
Memory
locations
0x16
0x00
0x00
0x00
0x00
0x12
15
Types
EXAMPLES
int i, j, *ip;
ip = &i;
i = 22;
j = *ip; /* j now has the value 22 */
*ip = 17; /* i now has the value 17 */
Memory
addresses
0x1200
0x1220
ip
0x1240
Variables
Memory
locations
0x16
0x00
0x16
0x00
0x00
0x12
16
Types
EXAMPLES
int i, j, *ip;
ip = &i;
i = 22;
j = *ip; /* j now has the value 22 */
*ip = 17; /* i now has the value 17 */
Memory
addresses
0x1200
0x1220
ip
0x1240
Variables
Memory
locations
0x11
0x00
0x16
0x00
0x00
0x12
17
Types
Every pointer type in C has a special value called a null pointer, which is
different from every valid pointer of that type
The null pointer constant in C is any integer constant expression with the
value 0
The macro NULL is traditionally defined as the null pointer constant in the
standard header files
It is good programming style to be sure that all pointers have the value
NULL when they are not designating a valid object or function.
Invalid pointers are pointer values that are not NULL but also do not
designate a proper object or function. An invalid pointer is most frequently
created by declaring a pointer variable without initializing it to some valid
pointer or to NULL
Any use of an invalid pointer, including comparing it to NULL, passing it as
an argument to a function, or assigning its value to another pointer, is
undefined in Standard C
EXAMPLES
if (ip != NULL) i = *ip;
Is equivalent to
if (ip) i = *ip;
EXAMPLES
void func( void )
{
int *ip;
if (ip != NULL) /*comparing an invalid poniter*/
}
C Programming Language - Lecture
2
18
Types
The need for a generic data pointer that can be
converted to any object pointer type arises occasionally in
low-level programming.
Standard C introduced the type void* as a "generic pointer
Generic pointers cannot be dereferenced with the * or
subscripting operators, nor can they be operands of addition
or subtraction operators
Type void* is considered to be neither an object pointer nor
a function pointer
EXAMPLES
void *generic_ptr;
int *int_ptr;
char *char_ptr;
generic_ ptr = intytr; /* OK */
int_ptr = generic_ptr; /* OK */
int_ ptr = char_ ptr; /* invalid */
Int_ptr = (int*) char_ptr; /* OK */
void *memcpy(void *s1, const void *s2, size_t n);
C Programming Language - Lecture
2
19
Types
If T is any C type except void, an incomplete type, or a
function type, then the type "array of T" may be declared
The length of the array may be specified by any integer
constant expression
Values of this type are sequences of elements of type T. All
arrays are 0-origin.
These values, known as elements, can be individually selected
by their position within the array
Subscripting or indexing is used to access a particular element
EXAMPLE
int A[3];
A[1] = 100;
A[2] = 200;
A[3] = 300;
is equivalent with
int A[3] = { 0x0064, 0x00C8, 0x012C };
A[0]
Memory
addresses
0x1200
A[1]
0x1202
A[2]
0x1204
Variables
Memory
locations
0x64
0x00
0xC8
0x00
0x2C
0x01
20
Types
C doesnt require that subscript bounds to be
checked; if a subscript goes out of range, the
programs behavior is undefined.
An array subscript may be an integer expression
The sizeof operator can determine the size of an
array (in bytes); it returns a value equal with the
product between the length of the array and the
length of one element.
EXAMPLE
int a[10], i;
for ( i = 0; i <= 10 ; i++ )
a[ i ] = 0;
21
Types
In C there is a close correspondence between types
"array of T " and "pointer to T":
When an array identifier appears in an expression, the
type of the identifier is converted from "array of T " to
"pointer to T", and the value of the identifier is converted to
a pointer to the first element of the array
The only exceptions to this conversion rule is when the array
identifier is used as an operand of sizeof or address (&)
operators, in which case sizeof returns the size of the entire
array and & returns a pointer to the array (not a pointer to a
pointer to the first element)
Memory
Memory
Variables
addresses
locations
EXAMPLE
A[0]
0x1200
0x00
0x00
int a[10], *ip;
ip = a;
It is exactly as :
ip
0x1214
0x00
0x12
ip = &a[0];
C Programming Language - Lecture
2
22
Types
In C there is a close correspondence between
types "array of T " and "pointer to T":
array subscripting is defined in terms of pointer
arithmetic. That is, the expression a [ i ] is defined
to be the same as * ((a) + (i)), where a is
converted to &a [0]
a[i] is the same as i[a]; so, any pointer may be
subscripted just like an array Memory
Memory
Variables
A[0]
addresses
0x1200
EXAMPLE
int a[10], *ip, i;
ip = a;
ip
0x1214
locations
0x00
0x00
0x00
0x00
0x12
0x00
0x12
23
Types
row 0
A[0][1]
0x1204
A[0][2]
0x1206
A[1][0]
0x1208
A[1][1]
A[1][2]
row 1
int a[2][3] = { { 1, 2, 3 },
{ 4, 5, 6 } };
addresses
0x1200
A[0][0]
EXAMPLE
0x120A
0x120C
locations
0x01
0x00
0x02
0x00
0x03
0x00
0x04
0x00
0x05
0x00
0x06
0x00
24
Types
An enumerated type in C is a set of integer values represented by
identifiers called enumeration constants. The enumeration constants
are specified when the type is defined and have type int
Enumeration tags are in the same overloading class as structure
and union tags, and their scope is the same as that of a variable declared
at the same location in the source program
Identifiers defined as enumeration constants are members of the same
overloading class as variables, functions, and typedef names. Their scope
is the same as that of a variable defined at the same location in the
source program
The first enumeration constant receives the value 0 if no explicit value
is specified
An explicit integer value may be associated with an enumeration constant
Subsequent enumeration constants without explicit associations
receive an integer value one greater than the value associated with the
previous enumeration constant.
EXAMPLE
EXAMPLE
Constant name
Small
Medium
Big
Large
Value
0
10
11
20
25
Types
EXAMPLE
struct timer {
char en;
char dir;
int val_crt;
int val_up;
int val_dn;
};
struct timer t = { 1,1, 18,2047,0};
Q: What is the value of the sizeof
operator applied to t structure?
26
Types
Holes or padding may appear between any two consecutive
components or after the last component in the layout of a structure if
necessary to allow proper alignment of components in memory
The bit patterns appearing in such holes are unpredictable and may
differ from structure to structure or over time within a single structure
EXAMPLE
struct timer {
char en;
int val_crt;
char dir;
int val_up;
Int val_dn;
};
struct timer t = { 1,18, 1, 2047, 0};
Q: What is the value of the sizeof
operator applied to t structure?
27
Types
C allows the programmer to pack integer components into
Byte
11
6
Byte
8
Page
Segment
Byte
Byte
16
Offset
28
Types
29
Types
EXAMPLE
struct S {
unsigned a : 4;
unsigned : 2;
unsigned b : 6;
};
Byte
4
2
Byte
6
EXAMPLE
struct S {
unsigned a : 4;
unsigned : 0;
unsigned b : 6;
};
Byte
4
a
Byte
12
Byte
6
Byte
10
30
Types
Byte
Byte
Byte
Byte
i
c
l
31
Types
EXAMPLE
extern int f(), (*fp) (), (*apf [ ] ) (double);
int i, j, k;
i = f(14);
i = (*fp) (j, k);
i = (*apf [ j ])(k);
extern int f () ;
int (*fpl) (), (*fp2) ();
fpl = f; /* implicit conversion to pointer */
fp2 = &f; /* explicit manufacture of a pointer */
32
Types
EXAMPLE
typedef type new_type_name;
typedef
typedef
typedef
typedef
typedef
typedef
typedef
int uint16_t;
int *IP;
int (*FP) ();
int F(int);
/* pointer to int * /
/* pointer to function returning int */
/* function with one int
parameter, returning int */
double A5[5];
/* 5-element array of
double" */
int A[ ];
/ * array of int * /
struct S { int a; int b; } stype; /* structure type */
IP ip;
IP tip () ;
FP fp;
F *fp2 ;
A5 a5;
A5 a25 [2] ;
A a;
A *ap3 [3] ;
/* pointer to an int */
/* function returning a pointer to
int */
/* pointer to a function returning int */
/* pointer to a function taking an
int parameter and returning an int */
/* 5-element double array */
/* a 2-element array
of 5-element arrays of double */
/* array of int (with unspecified bounds) */
/* 3-element array of pointers to
arrays of int (with unspecified bounds) */
33
Types
The type void has no values and no operations
Type void is used:
as the return type of a function, signifying that the
function returns no value
in a cast expression when it is desired to explicitly
discard a value
to form the type void *, a "universal" data pointer
in place of the parameter list in a function declarator
to indicate that the function takes no arguments
EXAMPLE
void main( void )
void func( int i )
int func( void )
(void)(x++ || y--);
void *memcpy(void *s1, const void *s2, size_t n);
C Programming Language - Lecture
2
34
Conversions
The C language provides for values of one type to
be converted to values of other types under
several circumstances:
A cast expression may be used to explicitly convert a
value to another type
An operand may be implicitly converted to another
type in preparation for performing some arithmetic or
logical operation
An object of one type may be assigned to a location
(lvalue) of another type, causing an implicit type
conversion
An actual argument to a function may be implicitly
converted to another type prior to the function call
A return value from a function may be implicitly
converted to another type prior to the function return
C Programming Language - Lecture
2
35
Conversions
The casting conversions allows an
object to be explicitly converted to
another type
Permitted casting conversions
Destination (cast) type
pointer to (object) T
(void *)
pointer to function
structure or union
void
any type
36
Conversions
In a simple assignment expression, the types of the
expressions on the left and right sides of the
assignment operator should be the same
If they are not, an attempt will be made to convert
the value on the right side of the assignment to the
type on the left side
Permitted assignment conversions
Left side type
(void*)
the constant 0
pointer to object T
(void *)
Pointer to (object) T
the constant 0
pointer to Q, where T an Q are compatible
(void *)
pointer to (function) T
the constant 0
Pointer to F, where T and F are compatible
C Programming Language - Lecture
2
37
Conversions
The usual unary
conversions determine
whether and how a single
operand is converted before
an operation is performed
Conversion rank
Ran
k
60
50
40
30
float
(no conversion)
array of T
pointer to T
function returning T
(no conversion)
int
unsigned int
20
38
Conversions
When two values must be operated on in
combination, they are first converted
according to the usual binary conversions to
a single common type, which is also
typically the type of the result
Usual binary conversions (choose first that applies)
If either operand has type
long double
long double
double
double
float
float
39
Q&A
1.
2.
3.
4.
char int
char* int *
double* int
int * t (where: typedef int t;)
5.
40