1 of 24

Lab 1

61C Fall 2023

2 of 24

QR Code for Slides

3 of 24

Compiling a C Program

  • gcc is used to compile C programs
  • gcc ex1.c test_ex1.c

  • Can specify the name of the executable file with the -o flag
    • gcc -o ex1 ex1.c test_ex1.c

ex1.c

test_ex1.c

gcc

a.out

4 of 24

Running a C Program

  • To run an executable located in the current directory, use ./<executable_name>
    • ./ex1
  • The dot refers to the current directory
  • If you want to run a file in a different directory, specify the path after the dot
    • ./path/to/file/ex1

5 of 24

Variable Types and Sizes

  • char = 1 byte (8 bits)
  • short = at least 2 bytes (16 bits), can be longer
  • int = at least 2 bytes (16 bits), can be longer
    • unsigned int
  • float = 4 bytes (32 bits)
  • double = 8 bytes (64 bits)
  • long = at least 4 bytes (32 bits), can be longer
  • long long = at least 8 bytes (64 bits), can be longer

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.

6 of 24

Defining a Function

Specify return type, function name, and function parameters.

int add(int x, int y) { return x + y; }

void nothing() { return; }

7 of 24

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;�}

8 of 24

Loops

While loop

For loop

while (condition) {

do this;�}

for (int i; i < 10; i++) {

do this;

}

9 of 24

Structs

  • What do they allow us to do?
  • What is a structure tag?
  • What are the two ways to declare struct variables?
  • How do we access the members of a struct?

Structure Tag

Variable declarations

10 of 24

Structs

  • typedef
    • Lets you avoid rewriting struct every time you want to declare a new struct variable
    • Can no longer declare variables in the struct definition

11 of 24

Pointers

0x7fffebafb32c

What will this print?

12 of 24

Pointers

0x7fffebafb32c

0x7fffebafb32c

13 of 24

Pointers

0x7fffebafb32c

0x7fffebafb32c

What will this print?

14 of 24

Pointers

0x7fffebafb32c

0x7fffebafb32c

Another address, ex: 0x7fffebafb320

15 of 24

Pointers

0x7fffebafb32c

0x7fffebafb32c

0x7fffebafb320

What will this print?

16 of 24

Pointers

0x7fffebafb32c

0x7fffebafb32c

0x7fffebafb320

22

17 of 24

Pointers

0x7fffebafb32c

0x7fffebafb32c

0x7fffebafb320

22

What will this print?

18 of 24

Pointers

0x7fffebafb32c

0x7fffebafb32c

0x7fffebafb320

22

22

19 of 24

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

20 of 24

Pointers to Structs

  • Pass a pointer of a struct to a function so that you can edit the contents
  • Access the struct contents by:
    • (*student).major
    • student->major

Arrow operator

21 of 24

Arrays

  • A block of memory: size is static
    • int arr[2];
    • int arr[] = {1, 2};
  • Accessing elements: array indexing
    • arr[1]
  • An array variable is a “pointer” to the first element.
    • You can use pointers to access arrays!
    • arr[0] is the same as *arr
    • Can use pointer arithmetic to move the pointer
      • Each operation automatically moves the size of one whole “type” that ptr points to
      • arr[1] is the same as *(arr + 1)

22 of 24

Strings

  • An array of chars representing individual characters
    • Ends in a null terminator ‘\0’
  • strlen(char* string)
  • strcpy(char* dest, char* src)

23 of 24

Dynamic Memory

  • Pointers can be allocated using malloc that persist in the heap
  • Heap is a memory space separate from other variables declared in the stack
  • Always remove dynamically allocated pointers by calling free(ptr); after use

24 of 24

Dynamic Memory

Options for allocating memory on the heap:

  • Malloc: Standard allocation of bytes
    • malloc(amount of bytes), usually malloc(sizeof(type)*number of items)
  • Calloc: Allocate some number of blocks (of given size) and zero out the memory
    • calloc(number of items, size of each item)
  • Realloc: Reallocate pointer to have a new size
    • realloc(old pointer, new size)