MCIT 593 – Introduction to Computer Systems
BASIC VARIABLE TYPES IN C
Property of Penn Engineering 2
Copyright By PowCoder代写 加微信 powcoder
MCIT 593 – Introduction to Computer Systems
Variables In C
• Four built-in data types in The Standard C Language • char
• float (doesn’t exist on LC4) • double (doesn’t exist on LC4)
• The width of each data type is machine dependent:
• float • double
16/32/64-bits (16-bits on LC4)
32-bits (IEEE Floating Point Standard) 64-bits (double the size of the float)
• C has data type modifiers to change width of data types
• long(doubleswidth),short(halveswidth),unsigned(binaryisunsigned)
• long int my_variable, short int my_variable, unsigned my_variable
Property of Penn Engineering 3
MCIT 593 – Introduction to Computer Systems
VARIABLES IN C ON THE STACK
Property of Penn Engineering 5
MCIT 593 – Introduction to Computer Systems
Variables in C on the Stack
int main() {
char my_char = ‘A’ ; /* ASCII characters are only 8-bits */
int my_int = 32767 ; /* largest positive #: 216-1 – 1 */
float my_float = 0.5 ;/* float’s are 32-bits */
}LC4 Assembly translation: main
; prologue first
; store local variables on the stack
LC4 Data Memory
(example stack)
CONST R7, x41
STR R7, R5 #-1
CONST R7, xFF
HICONST R7, x7F
STR R7, R5 #-2
CONST R7, x00
HICONST R7, x00
STR R7, R5 #-3
CONST R7, x00
HICONST R7, x3F
STR R7, R5 #-4
; my_char = ‘A’
; my_int = 32767
; my_float = 0.5
x7FFC R5 x7FFD
How many rows in memory would a double take? How many rows for a long int?
Property of Penn Engineering 6
x7FFE x7FFF
MCIT 593 – Introduction to Computer Systems
ARRAYS IN C
Property of Penn Engineering 8
MCIT 593 – Introduction to Computer Systems
Arrays in C
• Any data type can be made into an array in C
example: example: example:
• double example: double my_double_array [6]
char my_char_array [5]
int my_int_array [4]
float my_float_array [2]
• ThenameofanarrayinC…
• essentially a label for the memory address where it is stored
• Elements in an array are stored sequentially in memory
• Starting at the label, elements are stored sequentially in data memory
• The index operator matches offset in data memory from the label • The index operator []
Property of Penn Engineering 9
MCIT 593 – Introduction to Computer Systems
Example of Arrays in C in Memory
Declaration and initialization of an array in C:
int my_int [4] = {1, 2, 3, 4} ;
int my_int [] = {1, 2, 3, 4} ;
int my_int [4] ;
my_int[0] = 1 ;
my_int[1] = 2 ;
my_int[2] = 3 ;
my_int[3] = 4 ;
Example LC4 Assembly translation:
.ADDR x2000
LC4 Data Memory
my_int my_int [0] x2001 my_int [1]
Name of array becomes label
for start address of array
Arrays are stored consecutively in memory Arrays are stored in increasing order
in data memory
x2002 x2003
my_int [2]
my_int [3]
Property of Penn Engineering 10
MCIT 593 – Introduction to Computer Systems
TYPECASTING IN C
Property of Penn Engineering 12
MCIT 593 – Introduction to Computer Systems
Forcibly Changing a Variable’s Type in C…
• …is called type-casting
• Casting can be used to convert between types
• Smaller to bigger conversions are implicit; compiler does the casting for you: int a=5 ;
float b = 5.0 ;
b = a ; // implicit cast
• Bigger to smaller conversions are not implicit; you must cast explicitly: int a=5 ;
float b = 5.0 ;
a = (int)b ; // explicit cast, decimal is truncated
// (fraction is lost – no rounding)
• When going from bigger to smaller, the data is truncated, so you must know what you are doing!
Property of Penn Engineering 13
MCIT 593 – Introduction to Computer Systems
DEFINING YOUR OWN TYPES IN C
Property of Penn Engineering 15
MCIT 593 – Introduction to Computer Systems
User Defined Types in C
• You can create your own data types in C
• You must use existing data types
• This is called a “structure” or “struct”
• You can even use a struct within a struct!
• How to define your own data structure: struct fraction {
int numerator ;
int denominator; };
• How to use a data structure: struct fraction my_frac ;
my_frac.numerator = 3 ;
my_frac.denominator = 5 ;
We refer to internal variables as “fields” in the data structure
// declares variable
// space for 2 integers must be allocated // ß how to populate fields
// now “my_frac” represents 3/5
Property of Penn Engineering 16
MCIT 593 – Introduction to Computer Systems
User Defined Types in C
• For long variable declarations, it’s handy to use “typedef” • Allowsyoutogiveaknowntypeanewname
• How to use typedef for basic types:
typedef unsigned char BYTE ;
BYTE b1, b2 ;
b1=0xFF;b2=0xAA; //sidenote:youcanusehexinC!
• How to use typedef with structures:
typedef struct {
int numerator ;
int denominator ;
} fraction ;
fraction my_frac ; // now we can drop “struct” keyword
Property of Penn Engineering 17
MCIT 593 – Introduction to Computer Systems
POINTERS IN C
Property of Penn Engineering 19
MCIT 593 – Introduction to Computer Systems
You Are Familiar with Pointers from Assembly
• A pointer is a variable that can only contain the memory address of another variable
int main() {
LC4 Data Memory
(example stack)
x7FFA my_ptr x7FFB my_int
x7FFC R5 x7FFD
int my_int = 32767 ;
int* my_ptr = 0x7FFB ;
• my_ptr is an integer pointer
• It can only contain the memory address
of an integer
• Pointers are “typed” in C
• So they can be “dereferenced”
• A pointer is a variable too and it has a memory address of its own
• ex: my_ptr contains x7FFB BUT it is on the stack at x7FFA
Property of Penn Engineering 20
MCIT 593 – Introduction to Computer Systems
DEREFERENCING A POINTER
Property of Penn Engineering 22
MCIT 593 – Introduction to Computer Systems
Dereferencing a Pointer: *
• Dereferencing is the act of looking up the value that the pointer points to
int main() {
int my_int = 32767 ;
int* my_ptr = 0x7FFB ;
print (my_ptr) ;
print (*my_ptr) ; // dereference
LC4 Data Memory
(example stack)
• The output would be: x7FFB
• The * operator is the dereferencing operator in C
• If it precedes a pointer variable, it will look up the value it points to
• Can you imagine the assembly?
x7FFC R5 x7FFD
(a double LDR…why??)
Property of Penn Engineering 23
MCIT 593 – Introduction to Computer Systems
Determining the Address of a Variable: &
• In C, we won’t know exactly where variables are stored
• There is a mechanism to determine their address
int main() {
int my_int = 32767 ;
int* my_ptr = &my_int ;
LC4 Data Memory
(example stack)
x7FFA my_ptr x7FFB my_int
x7FFC R5 x7FFD
• The & operator is the “address operator” in C
• If it proceeds any variable, it will determine its runtime address
• What do you think this will return: &my_ptr
Property of Penn Engineering 24
MCIT 593 – Introduction to Computer Systems
You Can Have Pointers to Any Data Type in C
• Four built-in data types in standard C language:
char* my_char_ptr
int* my_int_ptr
float* my_float_ptr
double* my_double ptr
• You can even point to user defined data types in C:
typedef struct {
int numerator ;
int denominator ;
} fraction ;
How much memory does a pointer consume?
Does the type matter?
fraction my_frac ;
fraction* my_frac_ptr = &my_frac ;
(*my_frac_ptr).numerator = 3 ; // dereferencing ptr to struct is ugly L my_frac_ptr->denominator = 5 ; // C offers arrow operator instead J
Property of Penn Engineering 25
MCIT 593 – Introduction to Computer Systems
WHY DO WE NEED POINTERS IN C?
Property of Penn Engineering 27
MCIT 593 – Introduction to Computer Systems
Why We Need Pointers in C
• There are many uses, but one particular purpose is to help us pass addresses to functions instead of data
int square (int var) {
var = var * var ;
return (var) ;
int main() {
int a = 10 ;
a = square (a) ;
Recall that in C we pass copies of
local variables to functions
in order to ensure their local scope is protected
LC4 Data Memory
(example stack)
x7FF8 RA
x7FF9 RV
x7FFA var (arg) x7FFB a
x7FFC R5 x7FFD
Can you identify the frames?
Property of Penn Engineering 28
MCIT 593 – Introduction to Computer Systems
Why We Need Pointers in C
• There are many uses, but one particular purpose is to help us pass addresses to functions instead of data
int square (int var) {
var = var * var ;
return (var) ;
int main() {
LC4 Data Memory
(example stack)
int a = 10 ;
a = square (a) ;
var (arg) a
x7FF8 RA
x7FFC R5 x7FFD
x7FFE x7FFF
After square returns, we update the value of A by assigning it to the RV
Property of Penn Engineering 29
MCIT 593 – Introduction to Computer Systems
Why We Need Pointers in C
• There are many uses, but one particular purpose is to help us pass addresses to functions instead of data
void square (int* var) {
*var = (*var) * (*var) ;
int main() {
int a = 10 ;
LC4 Data Memory
(example stack)
x7FF9 RA x7FFA *var x7FFB a x7FFC R5 x7FFD
square (&a) ;
Pointers allow us to “break scope” and send address of a local variable
Now “square” can operate directly on “a”
Property of Penn Engineering 30
MCIT 593 – Introduction to Computer Systems
Why We Need Pointers in C
• There are many uses, but one particular purpose is to help us pass addresses to functions instead of data
void square (int* var) {
*var = (*var) * (*var) ;
int main() {
int a = 10 ;
square (&a) ;
Nothing to return from square, because we directly update “a” through its pointer
LC4 Data Memory
(example stack)
x7FF9 RA x7FFA *var
x7FFC R5 x7FFD
Property of Penn Engineering 31
MCIT 593 – Introduction to Computer Systems
POINTERS AND ARRAYS IN C
Property of Penn Engineering 33
MCIT 593 – Introduction to Computer Systems
Pointers and Arrays in C
• PointersandArraysareverycloselyrelatedinC
• Recall, an array is a label for an address in data memory that contains data
• A pointer is a variable that contains a memory address of a variable that contains data
int main() {
int a[3] = {1, 2, 3} ;
int b = 4 ;
LC4 Data Memory
(example stack)
int* b_ptr = &b ;
} (a)x7FF9 a[0]
Property of Penn Engineering 34
MCIT 593 – Introduction to Computer Systems
Pointers to Arrays in C
• PointersandArraysareverycloselyrelatedinC
• Recall, an array is a label for an address in data memory that contains data
• A pointer is a variable that contains a memory address of a variable that contains data
• To confuse and delight…you can have a pointer point to an array! int main() {
int a[3] = {1, 2, 3} ;
int* var = &a[0] ;
int* var = a ;
LC4 Data Memory
(example stack)
x7FF8 var (a)x7FF9 a[0]
Property of Penn Engineering 35
MCIT 593 – Introduction to Computer Systems
Pointers to Arrays in C
• Youcanevenusetheindexoperator[]todereferenceapointer!
• This is what leads to such confusion about differences between arrays and pointers in C!
int main() {
int a[3] = {1, 2, 3} ;
int* var = &a[0] ;
int*var=a ; print (var[0]) ; print (a[0]) ; print (*var) ;
LC4 Data Memory
(example stack)
x7FF8 var (a)x7FF9 a[0]
Use the stack to determine what these lines print
Property of Penn Engineering 36
MCIT 593 – Introduction to Computer Systems
Pointers to Arrays in C
• Youcanusethe++operatoronapointertohaveit“point”…
• …to the next element it points to in memory
• Called: pointer arithmetic (works on any pointer, whether it points to an array or not!)
int main() {
int a[3] = {1, 2, 3} ;
int* var = &a[0] ;
int* var = a ; print (var[0]) ; print (a[0]) ; print (*var) ; var++ ; print (var[0]) ; print (a[0]) ;
LC4 Data Memory
(example stack)
x7FF8 var (a)x7FF9 a[0]
Use the stack to determine what these lines print
Property of Penn Engineering 37
MCIT 593 – Introduction to Computer Systems
Pointers and Arrays in C
• Pointers and Arrays, a quick summary:
• The difference:
• An array is a label for an address in data memory that contains data
• A pointer is a variable that contains a memory address of a variable that contains data
• You can change the address held onto by a pointer
• You cannot change the address of an array (because it is essentially a label!)
• You can use the [ ] operator as a dereferencing operator on a pointer!
• Very helpful if pointer points to an array
• Don’t let that confuse you into thinking a pointer to an array!!
• You can do pointer arithmetic on a pointer
• You cannot do pointer arithmetic on an array (see the above rules as to why!)
Property of Penn Engineering 38
MCIT 593 – Introduction to Computer Systems
POINTERS, ARRAYS, AND FUNCTIONS IN C
Property of Penn Engineering 40
MCIT 593 – Introduction to Computer Systems
Pointers, Arrays, and Functions in C
• How do we pass an array to a function in C? • C isn’t doing what you might be thinking!
void square (int var[], int length) {
for (int i=0 ; i