The C Programming Language Eric Vidal CS 280 What is C? Originally: System programming language for the UNIX operating system Today: One of the most dominant development languages for general purpose applications, especially for low-level software Sample C Programs Hello, world! #include <stdio.h> int main(int argc, char *argv[], char *envp[]) { printf("Hello, world!\n"); return 0; } Sample C Programs Quicksort #include <stdio.h> /* move pivot to the center of the array */ if (array[start] > array[last_start]) start--; swap = array[last_start]; array[last_start] = array[start]; array[start] = swap; void quicksort(int *array, int start, int end) { int last_start = start, last_end = end, swap; /* we do not need to sort zero/one item arrays */ if (start >= end) return; /* move items greater than pivot to the end */ /* and keep items less than pivot at the start */ start++; while (start < end) { if (array[start] > array[last_start]) { swap = array[start]; array[start] = array[end]; array[end--] = swap; } else start++; } /* recursively sort array before and after pivot */ quicksort(array, last_start, start - 1); quicksort(array, start + 1, last_end); } int main(int argc, char *argv[], char *envp[]) { int foo[7] = { 4, 1, 6, 10, 9, 7, 3 }; int i; quicksort(foo, 0, 6); for (i = 0; i < 7; i++) printf("%d\n", foo[i]); return 0; } Real-World C Applications Most modern operating systems Most modern compilers Kernel – Linux kernel, NT kernel, etc. Command line processors – bash, csh, cmd.exe, etc. Native windowing system – X Window System, Windows Shell Other utilities – grep, make, etc. GNU Compiler Collection – gcc, g++, etc. Visual Studio’s base compilers – cl.exe, rc.exe, etc. Most modern PC and console games Evolution of C CPL – Cambridge Programming Language or Combined Programming Language (1963) Invented by Christopher Strachey, et al. Heavily influenced by Algol 60 Too complex to implement on existing computers BCPL – Basic CPL (1966) Invented by Martin Richards Programming language of choice for the Multics systems in Bell Laboratories Evolution of C B Programming Language (1969) Invented by Ken Thompson Implemented on a DEC PDP-7 with 8K 18-bit words of memory Intended as the system implementation language for UNIX Revision of an earlier language, “Bon” Not named after the B in BCPL, but heavily influenced by it Evolution of C BCPL versus B versus C BCPL and B are both typeless; C has types Pointers and arrays are both integer indexes to the memory array In BCPL: let v = vec 10 v!i = 42 In B: auto v[10]; v[i] = 42; In C: int v[10]; v[i] = 42; In B (unlike in BCPL and C), output is not native code, but threaded code which operates on a stack machine BCPL (unlike B and C) allows nested procedures, and links between separately compiled modules must be explicitly stated Evolution of C NB – New B (1971) Invented by Dennis Ritchie On the DEC PDP-11 with 12K 16-bit words of memory, character data is not accessed in words but in bytes Required two distinct data types: int and char Floating point arithmetic will also require a new float data type NB compiles to native code Evolution of C NB versus C Previously, arrays and pointers are the same, but Ritchie wanted to implement structures: struct direntry { int inumber; char name[14]; }; Pointer to an array != actual array in the structure Inconvenient for reading from disk Evolution of C C Programming Language (1972) Also invented by Ritchie, proper successor to B In C, arrays are still similar to pointers Pointer to an array is created only when the array name is mentioned in an expression Generalization of pointers: int i, *pi, **ppi; int fi(), *fpi(), (*pfi)(); int *api[10], (*pai)[10]; Evolution of C K&R C – first C standard (1978) Appeared in The C Programming Language by Brian Kernighan and Dennis Ritchie && and || operators Previously & and | means both logical and bitwise AND and OR Unfortunately, precedence was never fixed (to preserve compatibility with B): if (a & mask == b) /* logical error */ Preprocessor #include <header.h> #define MACRO unsigned, long, union, enum Type casts Evolution of C ANSI C or C89 – first official C standard (1989) Produced by the ANSI X3J11 working group Required the types of formal arguments in the type signature of the function: In K&R C: double sin(); In ANSI C: double sin(double); const, volatile Ratified a Standard C Library Should be implemented by all ANSI C-compliant vendors Evolution of C ISO/IEC 9899 C90 – second official C standard (1990) Exactly the same as C89, with changes to numbering to reflect ISO practice ISO/IEC 9899 C99 – current official C standard (1999) Produced by the ISO/IEC/JTC/SC22/WG14 working group Added invariant ISO646 and multibyte support extensions Clarified the existing standard via technical corrigenda: ISO/IEC 9899 TCOR1 (1995) ISO/IEC 9899 TCOR2 (1996) Available at: http://std.dkuug.dk/JTC1/SC22/WG14/ Disadvantages of C Ambiguity in variable use Type safety (integers versus pointers) Fence post errors (arrays versus pointers) Indirection problems int *fp(); int (*pf)(); int *(*pfp)(); Treats strings as null-terminated character arrays Finding the length of the string is O(n) Generally not well-suited for string processing Disadvantages of C Array problems Dynamically changing the size of an array is clumsy Functions expecting multiple pointers to arrays cannot be optimized fully for SIMD or vector machines; arrays may overlap Modularization problems Only two levels of naming: external (global) and internal (function) Developers must create their own modularization conventions Disadvantages of C Memory problems C itself only provides two types of storage: automatic (in the stack segment) and static (in the data segment) Dynamically allocated storage in the heap (malloc() and free()) is tedious and errorprone Automatic garbage collection is difficult Advantages of C Low-level functionality High-level syntax Bitwise operators Pointers Type looseness Abstract enough to describe underlying algorithms Result: A language that can do practically everything, is portable to other systems, and runs as fast as assembly Advantages of C Small and simple Easy to parse; compilers occupy very little memory Ties with UNIX Language not designed in isolation, but in a real environment with emphasis on practicality Meets the needs of programmers, but does not supply too much Compatibility with old programs – C has been remarkably stable through the years (unlike FORTRAN or Pascal) Compilers for C First C compiler, cc, is developed by Dennis Ritchie First portable C compiler, pcc, is developed by Steve Johnson (1978) Became the reference implementation for K&R C Currently a ton of compilers available Mostly because compiler theory classes use a subset of C for their final project Compilers for C Generally available C compilers (also works for C++): Borland C++Builder Microsoft Visual C++ .NET http://www.borland.com/cbuilder/ http://msdn.microsoft.com/visualc/ The GNU Compiler Collection For Linux: http://gcc.gnu.org/ For Windows: http://www.mingw.org/ References Ritchie, Dennis M. The Development of the C Language. April 1993. Internet on-line. Available from <http://cm.belllabs.com/cm/cs/who/dmr/chist.html> [31 July 2003] International Standards Organization. 2003. ISO/IEC JTC1/SC22/WG14 – C. Denmark: International Standards Organization, 2003 [cited 31 July 2003]. Available from World Wide Web: (http://std.dkuug.dk/JTC1/SC22/WG14/)
© Copyright 2025 ExpyDoc