Lab 1
61C Fall 2023
QR Code for Slides
Compiling a C Program
ex1.c
test_ex1.c
gcc
a.out
Running a C Program
Variable Types and Sizes
Guarantee: sizeof(long long) >= sizeof(long) >= sizeof(int) >= sizeof(short)
To know for sure what size your variable is, use uintN_t or intN_t types.
Defining a Function
Specify return type, function name, and function parameters.
int add(int x, int y) { return x + y; }
void nothing() { return; }
Conditionals
if (condition) {� do this;�} else if {� do this;�} else {� do this;�}
If-else
Switch statements
switch (expression) {
case constant1: � do these;
break;� case constant2:
do these;
break;
default:
do these;�}
Loops
While loop
For loop
while (condition) {
do this;�}
for (int i; i < 10; i++) {
do this;
}
Structs
Structure Tag
Variable declarations
Structs
Pointers
0x7fffebafb32c
What will this print?
Pointers
0x7fffebafb32c
0x7fffebafb32c
Pointers
0x7fffebafb32c
0x7fffebafb32c
What will this print?
Pointers
0x7fffebafb32c
0x7fffebafb32c
Another address, ex: 0x7fffebafb320
Pointers
0x7fffebafb32c
0x7fffebafb32c
0x7fffebafb320
What will this print?
Pointers
0x7fffebafb32c
0x7fffebafb32c
0x7fffebafb320
22
Pointers
0x7fffebafb32c
0x7fffebafb32c
0x7fffebafb320
22
What will this print?
Pointers
0x7fffebafb32c
0x7fffebafb32c
0x7fffebafb320
22
22
Pointers
0x7fffebafb32c
22
Address
Data
0x7fffebafb330
0x7fffebafb32c
&my_var
my_var_p
&my_var_p
my_var
*my_var_p
&x = address of x
*x = contents at x
Pointers to Structs
Arrow operator
Arrays
Strings
Dynamic Memory
Dynamic Memory
Options for allocating memory on the heap: