C programming (3 days)

Synopsis

This course leads the student from the basics of writing and compiling C programs to such advanced techniques as file I/O and dynamic memory management.

All concepts are reinforced by challenging and pertinent lab exercises. The complexity of the labs increases as the students master more skills.

Target audience

This course is appropriate for anyone who needs to learn C. This course can also serve as background for someone who will ultimately move on to Perl, C++, or Java, since these are all derived from C.

Prerequisites

Some programming experience in a high-level language (Pascal, COBOL, Visual Basic, etc.) is highly recommended. This course assumes some knowledge of elementary programming concepts such as variables and conditional execution. No previous knowledge of C is expected.

Lab exercises Students will write numerous C programs that reinforce the language elements learned.

Course outline

1. Diving into C

  • The general structure of a C program
  • Compiling and linking
  • The main() function
  • Using #include
  • Simple console output
  • Command line arguments
  • Comments

2. Variables and arrays

  • Variable types
  • Declaring variables
  • Arrays
  • Using sizeof
  • Constants and operators
  • Expressions
  • Initializers
  • Casting
  • Converting

3. Control structures

  • The if test
  • Comparison operators
  • The while loop
  • The for loop
  • The switch statement

4. File I/O

  • Opening and closing files
  • Reading text
  • Reading binary
  • Writing to files
  • Console I/O

5. Pointers

  • What is a pointer?
  • Declaring pointers
  • Dereferencing pointers
  • Pointers to arrays
  • Pointer arithmetic
  • Pointers as function parameters

6. Functions

  • Declaring functions
  • Calling functions
  • Passing parameters by value
  • Passing parameters by reference
  • Return values
  • Function prototypes

7. Dynamic memory

  • The real world
  • Using malloc() and realloc()
  • Danger of null pointers
  • Allocating arrays
  • Multidimensional arrays
  • Using calloc()
  • Freeing unneeded memory

8. Structures

  • Modeling data
  • Defining structures
  • Declaring structure variables
  • Accessing elements
  • Pointers to structures

9. Strings and Bytes

  • Strings are char arrays
  • Pointers to strings
  • String length
  • Comparing strings
  • Other string functions
  • Character functions
  • Generic byte-oriented functions

10. The preprocessor

  • Symbolic constants
  • Manifest constants
  • Macros
  • Conditional compilation
  • Using typedef

11. Bit fiddling

  • Why use bits?
  • Bit fields and why not to use them
  • Hex and octal constants
  • Bitwise operators
  • Testing and setting bits
  • Bit macros

12. Scope and storage class

  • Automatic variables
  • External variables
  • Accessing data in other source files
  • Static variables
  • Global variables and sanity

13. Common Mistakes

  • Uninitialized variables
  • Null pointers
  • Using = for ==
  • Referencing past end of arrays
  • Memory leaks
  • Returning auto data from functions

14. Programming Style

  • Indenting
  • Comments
  • Variable names
  • Source file layout

15. Multiple source files

  • Multi-file projects
  • Object files
  • Header files
  • Library files
  • The linker
  • A simple makefile

Downloadable files for this course: