The c programming language
Advertisement
This presentation is the property of its rightful owner.
1 / 22

The C Programming Language PowerPoint PPT Presentation

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!

Download Presentation

The C Programming Language

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


The c programming language

The C Programming Language

Eric Vidal

CS 280


What is c

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

Sample C Programs

  • Hello, world!

    #include<stdio.h>

    intmain(intargc, char *argv[], char *envp[])

    {

    printf("Hello, world!\n");

    return 0;

    }


Sample c programs1

Quicksort

#include<stdio.h>

voidquicksort(int *array, intstart, intend)

{

intlast_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);

}

intmain(intargc, char *argv[], char *envp[])

{

intfoo[7] = { 4, 1, 6, 10, 9, 7, 3 };

inti;

quicksort(foo, 0, 6);

for (i = 0; i < 7; i++)

printf("%d\n", foo[i]);

return 0;

}

Sample C Programs


Real world c applications

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 


Evolution of c

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 c1

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 c2

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:In B:In C:

      letv = vec 10autov[10];intv[10];

      v!i = 42v[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


Evolution of c3

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 c4

Evolution of C

  • NB versus C

    • Previously, arrays and pointers are the same, but Ritchie wanted to implement structures:

      structdirentry

      {

      intinumber;

      charname[14];

      };

    • Pointer to an array != actual array in the structure

      • Inconvenient for reading from disk


Evolution of c5

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:

      inti, *pi, **ppi;

      intfi(), *fpi(), (*pfi)();

      int *api[10], (*pai)[10];


Evolution of c6

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>

      #defineMACRO

    • unsigned, long, union, enum

    • Type casts


Evolution of c7

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:

      doublesin();doublesin(double);

    • const, volatile

    • Ratified a Standard C Library

      • Should be implemented by all ANSI C-compliant vendors


Evolution of c8

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

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 c1

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 c2

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 error-prone

    • Automatic garbage collection is difficult


Advantages of c

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


Advantages of c1

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

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 c1

Compilers for C

  • Generally 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/


References

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/IECJTC1/SC22/WG14 – C. Denmark: International Standards Organization, 2003 [cited 31 July 2003]. Available from World Wide Web: (http://std.dkuug.dk/JTC1/SC22/WG14/)


  • Login