Skip to content

emmaneugene/crash-C

Repository files navigation

A brief introduction to programming in C

Examples

This repo has many C programs which you can compile and run as examples. There is a Makefile that simplifies this process.

  • To compile: make SRC=<filepath>

  • To run (compilation is handled automatically): make run SRC=<filepath>

  • To delete: make clean SRC=<filepath>

Where <filepath> is an absolute or relative path to the source file (.c).

Overview

  1. "Hello world"
  2. Variables, datatypes, conventions
  3. Operators and precedence
  4. Conditionals
  5. Loops
  6. Functions
  7. Arrays, pointers and memory
  8. Strings
  9. Structs
  10. Tips, tools and resources

1. Hello world

#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("Hello, world!\n");
}
  • What's the significance of:
    • #include?
    • stdio.h?
    • main?
    • int argc, char *argv[]?
  • How do you run this program? What are the steps involved, and why do we need them?

2. Variables, datatypes, conventions

  • Basic datatypes: char, short, int, long, float, double

  • The sizeof operator tells you how much space the datatype occupies up in bytes

  • It's possible to 'convert' one datatype into another with typecasting

  • Variables can be declared, assigned or initialized

  • Naming conventions in C:

    • use alphanumeric characters, _ as a substitute for whitespace

    • lowercase for local variables

    • uppercase for constants

    • first character should not be a number

3. Operators and precedence

  • Similar to BODMAS in mathematics, operators in C also have their own levels precedence
  • Find out more about precedence here
  • If unsure, it never hurts to use parentheses ()

4. Conditionals

  • Using boolean logic to control the execution specific blocks of code
  • De Morgan's theorem (prove it for yourself with a truth table)
  • Short-circuiting operators: || , &&

5. Loops

  • Repeating blocks of code until a certain stopping condition is met (still a boolean)
  • What's the difference between for, while, do while? Is it possible to convert one into the other?
  • Nested loops

6. Functions and scope

  • Distinguish between function declaration vs implementation
// Declaration
void common_factors(int x, int y);

// Implementation
void common_factors(int x, int y) {
  int lowest = x < y ? x : y;

  for (int i = 2; i <= lowest; i++) {
    if (x % i == 0 && y % i == 0) {
      printf("Common factor: %d\n", i)
    }
  }
}
  • Understand the concept of a function frame, stack and heap
  • Understand the general intuition behind recursion

7. Arrays, pointers and memory allocation

  • What is an array? What makes it useful?

    • Contiguous block of memory for storing a certain datatype
  • What is a pointer? What makes it useful

  • null reference

  • Passing by reference vs passing by value

  • Visualizing using memory state diagrams

  • malloc, calloc and free - how do they work?

8. Strings

  • All strings are char arrays

  • How do we know when a string terminates?

  • Strings declared using * are immutable

    #include <stdio.h>
    
    void fun(char *str) {
      printf("%c\n", str[1]);
      str[1] = 'a';
    }
    
    int main(void) {
      char *mystr = "Hello"; // constant, immutable, read-only
      char mystr2[] = "Testing"; // mutable
    
      fun(mystr2);
      printf("%s\n", mystr);
      printf("%s\n", mystr2);
    }

9. Structs

  • Logical grouping of datatypes, allowing for greater abstraction
  • Foundation for Object-Oriented Programming (your next course)

10. Useful libraries, tips, tools and resources

Libraries

  • string.h

  • math.h

  • stdlib.h

  • limits.h

  • Feel free to make your own as well!

Tips

  • Get comfortable with the command line

  • Read and understand your compile and runtime errors

  • Write comments to structure your logic, or as a placeholder for future code

  • Pay attention to variable names, especially as code gets more complex

  • Handle corner cases first

    ❌ Don't do this

    int main(void) {
      if (!/*corner case*/) {
        // Main body
      }
      // Handle corner case
    }

    ✅ Do this

    int main(void) {
      if (/*corner case*/) {
        // Handle corner case
        return
      }
    
      // Main body
    }
  • Revisit old problems and come up with better solutions. Be able to explain the tradeoffs involved with each one

Resources

Command line:

General learning:

About

Crash course in C programming (CS101)

Topics

Resources

License

Stars

Watchers

Forks