Algorithms and Data Structures

C Review ๐Ÿ’ป

Aryan Mediratta

Here's what we'll do.

Here's what we'll do.

A quick review of C fundamentals

Here's what we'll do.

A quick review of C fundamentals

Expressions, statements, lvalues, and rvalues

Here's what we'll do.

A quick review of C fundamentals

Expressions, statements, lvalues, and rvalues

Structs and Pointers

Here's what we'll do.

A quick review of C fundamentals

Expressions, statements, lvalues, and rvalues

Structs and Pointers

UNIX review and debugging

Here's what we'll do.

A quick review of C fundamentals

Expressions, statements, lvalues, and rvalues

Structs and Pointers

UNIX review and debugging

Good coding practices

The Build Process ๐Ÿ”ง

 

C Tokens ๐ŸŽŸ๏ธ

Tokens are the smallest unit of a programming language.

C Tokens ๐ŸŽŸ๏ธ

  • Keywords ๐Ÿ”‘
  • Identifiers ๐Ÿชช
  • Constants 2๏ธโƒฃ
  • Operators โž•
  • Delimiters โœ‚๏ธ

C Keywords ๐Ÿ”‘

  • alignas (C23)
  • alignof (C23)
  • auto
  • bool (C23)
  • break
  • case
  • char
  • const
  • constexpr (C23)
  • continue
  • default
  • do
  • double
  • else
  • enum
  • extern
  • false (C23)
  • float
  • for
  • goto
  • if
  • inline (C99)
  • int
  • long
  • nullptr (C23)
  • register
  • restrict (C99)
  • return
  • short
  • signed
  • sizeof
  • static
  • static_assert (C23)
  • struct
  • switch
  • thread_local (C23)
  • true (C23)
  • typedef
  • typeof (C23)
  • typeof_unqual (C23)
  • union
  • unsigned
  • void
  • volatile
  • while

Primitive Types ๐Ÿท๏ธ

Variable Declaration

 

Variable Declaration with Initialization

 

Multiple Declarations of the same type

Control Statements ๐ŸŽฎ

Control Statements ๐ŸŽฎ

Conditionals

Switch Statements

YouTube: Why are Switch Statements So Fast? - Low Level Learning

Control Statements ๐ŸŽฎ

Loops

While Loop

Control Statements ๐ŸŽฎ

Loops

While Loop

flowchart showing do-while loop

Control Statements ๐ŸŽฎ

Loops

Do-while Loop

Control Statements ๐ŸŽฎ

Loops

Do-while Loop

flowchart showing do-while loop

Control Statements ๐ŸŽฎ

Loops

Roadrunner and Coyote, demonstrating difference between do-while and while

Control Statements ๐ŸŽฎ

Loops

 

Functions

Functions



Remember Recursion? ๐Ÿค”

Let's turn it into code.

Implement a function int fibonacci(int n) that takes n as input and returns the nth Fibonacci number.

Expressions vs Statements

Expressions vs Statements

Expressions can also be used as statements.

Assignments

Assignments are expressions!

lvalues and rvalues

XKCD Comic on Pointers

Source: XKCD, Creative Commons Attribution-NonCommercial 2.5 License

Pointers and Indirection ๐Ÿ‘‰

Pointers and Indirection ๐Ÿ‘‰

Pointers and Indirection ๐Ÿ‘‰

Arrays ๐ŸŽž๏ธ

Stack Allocated Arrays ๐Ÿ“ฆ

 

Structures ๐Ÿข

Structures help define our own compound datatypes as combinations of primitive types.

They are like classes but can only contain member variables and not functions/methods.

Structures ๐Ÿข

 

Structures ๐Ÿข

Dynamic Memory Allocation

Memory on the heap can be allocated using malloc().

If you also want it initialized to 0, you can use calloc().

To resize heap-allocated memory, use realloc().


All memory allocated using these functions must be freed using free(). Failing to do so produces a memory leak.

Dereferencing with Arrays

The name of an array resolves to its address.

Or more precisely, the address of its first element.

array[i] is just syntactical sugar for *(array + i).

Dynamically Allocating 2D Arrays

 
 
arr[0] arr[1] arr[2] arr[3] arr[0][0] arr[0][1] arr[0][2] arr[1][0] arr[1][1] arr[1][2] arr[2][0] arr[2][1] arr[2][2] arr[3][0] arr[3][1] arr[3][2]

Freeing Dynamically Allocated 2D Arrays

 
 
 
 
arr[0] arr[1] arr[2] arr[3] arr[0][0] arr[0][1] arr[0][2] arr[1][0] arr[1][1] arr[1][2] arr[2][0] arr[2][1] arr[2][2] arr[3][0] arr[3][1] arr[3][2]

Strings

What data type is this? ๐Ÿค”

char *var[6];

Passing Pointer Arrays

 

Command-Line Arguments

Command-Line Arguments are passed to the main function as a pointer array.

To use command-line arguments, use this signature for the main function

Command-Line Arguments

Command Line Arguments in C Demo

UNIX Review and Debugging

Shell Commands

Redirection

Redirection: Output Demo

Redirection: Input Demo

Matching Expected Outputs and Diff

Matching Expected Outputs and Diff

Say we want to print a 2D array.

Example output:
-------------------------
|  2|  4|  5|  6|  7|  9|
| 10|-23|  4|  5|  2|  7|
|  0|  3| 10| 33|234| 40|
-------------------------
				

Implement void print_array(int* arr[], int rows, int cols) using this output format.

Matching Expected Outputs and Diff

Checking that we did it right!

The diff command takes two files and tells if they are identical, and optionally tells where they differ, if they do.

$ diff file1 file2

$ diff your_output.txt expected_output.txt

GDB Commands

break
print
next
step
run
continue
watch
list
quit

Reverse Debugging

Common Errors

Common Errors

Common Errors

Good Coding Practices

Good Coding Practices

XKCD: Good Code

Source: XKCD, Creative Commons Attribution-NonCommercial 2.5 License

Topics Not Covered Here

  • Strings and the string.h library
  • File Handling
  • Bit Manipulation and Bitmasking
  • const