Lecture 4: C in a Day (Well, the important bits)

Introduction

About C

Hello, World!


#include <stdio.h>         /* <- make sure the I/O functions are visible to the compiler */

/* main is the functions the OS calls when a program is loaded. It gets the 
 * number of command-line arguments and an array with the arguments. The first
 * entry is the name of the executable.
 *
 * Main returns an integer that is the exit status of the program (0 on success).
 */
int main(int argc, char **argv) { 
  printf("Hello, World!\n");  /* We've used printf before */

  return 0;
}

Brief Overview of Familiar Features

  1. Blocks of scope are delimited by { and }

  2. Functions are declared pretty much like Java methods:

    return_type function_name(type1 arg1, type2 arg2, ...)
  3. Control flow

  4. Types

  5. No booleans: just 0 and non-0

  6. No string type, just a collection of characters

Pointers

Declaring a Pointer

De-referencing a Pointer

Address-of Operator

Pointers Pointing to Pointers

Arrays

Strings

Dynamic Memory Allocation

Structs

typedef

Pointers to structs

The Preprocessor

#define

#include

#ifdef/#ifndef

Header Files

Separate Compilation

Global and Local Variables