01: What is special about C?

Evolution of programming languages

  • first high level language was FORTRAN in 1954-1956
  • FORTRAN = “FORmula TRANslation”, created at IBM
    • procedural languages (1960s) — FORTRAN, BASIC, COBOL, PL/I
      • arithmetic and logical expressions, variables, assignment statements
      • statements that define loops (repeated actions)
      • statements that allow decisions (conditionally executed actions)
      • short variable names (2 characters in early version of BASIC)
      • every statement begins at left edge of page in early version of FORTRAN
    • structured programming languages (1970s) — C, Pascal
      • long names for variables and functions
      • use indentation and blank lines to make program
        easier to read
      • less use of GOTO statements — just a few ways
        of repeating actions and choosing between alternative
        actions
      • write for 2 audiences: computer and other people
    • abstract data type languages (1980s) — Ada
      • bundle description of data and definitions of functions
        for working with the data
      • (Ada Lovelace worked with Charles Babbage in 1800s
        in England — Babbage tried to build a computer
        way back then! She wrote programs for the computer!)
    • object oriented languages (1990s) — C++, Java, C#
      • define relationships among abstract data types (classes)
      • inheritance
      • aggregation / composition
      • information hiding / encapsulation
      • polymorphism
      • write a specialized kind of abstract data type
        without having to rewrite parts

The C programming language

  • Dennis Ritchie — AT\&T Bell Laboratories — 1972
    • 16-bit DEC PDP-11 computer (right)w
    • widely used today
    • extends to newer system architectures
    • efficiency/performance
    • low-level access
  • features of C:
    • Few keywords
    • Structures, unions—compound data types
    • Pointers — memory, arrays
    • External standard library — I/O, other facilities
    • Compiles to native code
    • Macro preprocessor
  • language has evolved over the years:
    • 1972 — C invented
    • 1978 — The C Programming Language published; first
      specification of language
    • 1989 — C89 standard (known as ANSI C or Standard C)
    • 1990 — ANSI C adopted by ISO, known as C90
    • 1999 — C99 standard
    • mostly backward-compatible
    • not completely implemented in many compilers
    • 2007 — work on new C standard C1X announced
  • C is used for systems programming:
    • OSes, like Linux
    • microcontrollers: automobiles and airplanes
    • embedded processors: phones, portable electronics, etc.
    • DSP processors: digital audio and TV systems
  • more recent derivatives: C++, Objective C, C#
  • the C language has influenced: Java, Perl, Python (quite different)
  • C lacks:
    • exceptions
    • range-checking
    • garbage collection
    • object-oriented programming
    • polymorphism
  • low-level language (more direct access to and control of the machine), faster executing code (usually)
  • with great power comes great responsibility
  • C language is inherently unsafe:
    • No range checking
    • Limited type safety at compile time
    • No type checking at runtime

People and institutions

Resource

These notes include material copied and adapted from 6.087 Practical Programming in C. Daniel Weller and Sharat Chikkerur taught the course taught at the Massachusetts Institute of Technology (MIT) during MIT’s Independent Activities Period in 2010. They published their course materials in MIT’s OpenCourseWare under a Creative Commons license.

The adaptations include edits and additions to the notes published on MIT’s OpenCourseWare.

Example program

// put this code in a file named planets.c
// compile the program by typing:
//     gcc planets.c -o planets
// run the program by typing:
//     planets mercury venus earth mars
// exercises:
//   what happens if you try running the program by typing:
//     planets mercury venus earth
//   what happens if you try running the program by typing:
//     planets mercury venus earth mars jupiter

// directive to the preprocessor
// tells gcc to replace "#include <stdio.h>"
// with the contents of the file stdio.h
#include <stdio.h>
#include <stdlib.h>

// execution of program begins in main() function

// main() function returns an integer (int)
// to its caller---this integer is a code
// that indicates success or failure


int main( int argc, char** argv ) {

    // print a message
    printf( "I am here.\n" );

    // %2d is a formatting code
    // it means a decimal integer with 2 digits
    printf( "argc = %2d\n", argc );
    printf( "planet = %s\n", argv[0] );
    printf( "planet = %s\n", argv[1] );
    printf( "planet = %s\n", argv[2] );
    printf( "planet = %s\n", argv[3] );
    printf( "planet = %s\n", argv[4] );

    // signal successful completion of program
    exit( 0 );
} // main( int, char** )