+ All Categories
Home > Documents > The C Programming Language

The C Programming Language

Date post: 06-Feb-2016
Category:
Upload: chin
View: 42 times
Download: 0 times
Share this document with a friend
Description:
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! - PowerPoint PPT Presentation
22
The C Programming Language Eric Vidal CS 280
Transcript
Page 1: The C Programming Language

The C Programming Language

Eric VidalCS 280

Page 2: The C Programming Language

What is C?Originally: System programming language for

the UNIX operating systemToday: One of the most dominant

development languages for general purpose applications, especially for low-level software

Page 3: The C Programming Language

Sample C ProgramsHello, world!

#include <stdio.h>

int main(int argc, char *argv[], char *envp[]){ printf("Hello, world!\n"); return 0;}

Page 4: The C Programming Language

Sample C Programs Quicksort

#include <stdio.h>

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++; }

/* 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;

/* 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;}

Page 5: The C Programming Language

Real-World C Applications Most modern operating systems

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.

Most modern compilers GNU Compiler Collection – gcc, g++, etc. Visual Studio’s base compilers – cl.exe, rc.exe, etc.

Most modern PC and console games

Page 6: The C Programming Language

Evolution of CCPL – 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

Page 7: The C Programming Language

Evolution of CB 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

Page 8: The C Programming Language

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

arrayIn BCPL: In B: In C:let v = vec 10 auto v[10]; int v[10];v!i = 42 v[i] = 42; 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

Page 9: The C Programming Language

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

Page 10: The C Programming Language

Evolution of CNB 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

Page 11: The C Programming Language

Evolution of CC 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];

Page 12: The C Programming Language

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

Page 13: The C Programming Language

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: In ANSI C:double sin(); double sin(double);

const, volatile Ratified a Standard C Library

Should be implemented by all ANSI C-compliant vendors

Page 14: The C Programming Language

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/

Page 15: The C Programming Language

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

Page 16: The C Programming Language

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

Page 17: The C Programming Language

Disadvantages of CMemory 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 error-prone

Automatic garbage collection is difficult

Page 18: The C Programming Language

Advantages of C Low-level functionality

Bitwise operators Pointers Type looseness

High-level syntax 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

Page 19: The C Programming Language

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)

Page 20: The C Programming Language

Compilers for CFirst C compiler, cc, is developed by Dennis

RitchieFirst 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

Page 21: The C Programming Language

Compilers for CGenerally available C compilers (also works

for C++): Borland C++Builder

http://www.borland.com/cbuilder/ Microsoft Visual C++ .NET

http://msdn.microsoft.com/visualc/ The GNU Compiler Collection

For Linux: http://gcc.gnu.org/ For Windows: http://www.mingw.org/

Page 22: The C Programming Language

References Ritchie, Dennis M. The Development of the C

Language. April 1993. Internet on-line. Available from <http://cm.bell-labs.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/)


Recommended