CSCI 243 The Mechanics of Programming Evolution of Programming...
Transcript of CSCI 243 The Mechanics of Programming Evolution of Programming...
CSCI 243The Mechanics of Programming
TVF / RIT 20195
Evolution of Programming Languages
TVF / RIT 20195 CS243: Evolution
How Did We Get Here?
• Previous courses:• Python
• Java
• Where did those languages come from?
TVF / RIT 20195 CS243: Evolution
Evolution of Programming Languages
• Computer hardware speaks machine language (ML)• Binary
• Hard to work with
• Lots of gory details
• Defines the instruction set architecture (ISA)
• Example operation:
• MIPS R2000 machine instruction to do this:
• Alternate notation, using hexadecimal:
00100000010000010000000101011110
2041015E
Add the number 350 and whatever is in register #2, putting the result into register #1.
TVF / RIT 20195 CS243: Evolution
Evolution of Programming Languages
• Machine Language too hard for humans to think about
• Idea: use a symbolic notation
• Result: assembly language (AL)• A symbolic form of ML
• Example: MIPS R2000 assembly statement:
• Translator: an assembler is a program that takes AL as input and produces ML as output
• Still has problems• Separate assembler for each machine architecture
addi $1, $2, 350
TVF / RIT 20195 CS243: Evolution
Evolution of Programming Languages
• Goal: want a higher level of abstraction, one that is independent of machine architecture
• Humans write “phrases” (e.g., a + b * c - d)
• Result: high-level language (HLL)• An abstract representation of multiple AL statements
• Example: C statement:
• Translator: a compiler• compiler translates from HLL to AL (or to ML directly)• “Back End” of the compiler knows about the machine
architecture
r1 = r2 + 350;
TVF / RIT 20195 CS243: Evolution
The C Programming Language
• C was Developed at Bell Labs by Dennis Ritchie for UNIX†
• Evolution‡:• BCPL: developed in the mid-1960s by Martin Richards
• One of the original MULTICS (operating system) languages
• B: developed 1969-1970 by Ken Thompson for systems programming
• Essentially BCPL “squeezed into 8K bytes of memory”
• Typeless language; syntax very similar to that of C
• C: developed 1971-1973 by Ritchie
• Added type system, revised control structures, etc.
†UNIX is a registered trademark of the Open Group Ltd.‡From “The Development of the C Language” by Dennis M. Ritchie
TVF / RIT 20195 CS243: Evolution
The C Programming Language
• The foundation for later languages C++ and Java
• Primarily developed for systems programming (what does this mean??)
• Closer to the machine than many (most?) HLLs
• An imperative language• A.k.a. procedural language
TVF / RIT 20195 CS243: Evolution
Language Paradigms: Imperative
• Examples: Fortran, C, Pascal, Modula-2, BASIC
• Statements are “commands”• Change the state of the program
• Program is a sequence of statements,with some control structures that can change the order of execution
• Concept: program is a “flat” collection of modules• Functions, global variables
• A.k.a. externals
• All at same “level” – no nesting
• Vs. classes
TVF / RIT 20195 CS243: Evolution
Language Paradigms: Object-Oriented
• Examples: Java, C#, C++ (kind of), Smalltalk
• Program is a collection of classes• Encapsulated data types
• Information + operations bundled together
• Member functions are implemented using imperative statements
• More abstraction from what’s happening at the CPU level
• E.g., what happens at class instantiation time
• Traditional view: objects interact through message passing
TVF / RIT 20195 CS243: Evolution
Language Paradigms: Functional
• Examples: Lisp, Scheme, Haskell, FP, Scala
• Computation is a series of function applications• “Function” in the mathematical sense
• Produce reliable output from input
• No “state”
• Even more abstraction!
TVF / RIT 20195 CS243: Evolution
// First program: hello.c//// To compile source into an object file:// gcc std=c99 Wall Wextra pedantic c hello.c//// To link object file into an executable:// gcc std=c99 o hello hello.o//// To run:// ./hello
#include <stdio.h> // printf
int main( void ) { printf("Hello world!\n"); return 0;}
Example C Program: Hello, World!
% gcc std=c99 Wall Wextra pedantic c hello.c% gcc std=c99 o hello hello.o% ./helloHello world!%
TVF / RIT 20195 CS243: Evolution
// printints.c//// Print the values of two local variables, x and y,// to standard output.
#include <stdio.h> // printf
int main( void ) { int x = 10; int y = 20; printf("x = %d, y = %d\n", x, y);
return 0;}
Example C Program: Print Integers
% gcc std=c99 c printints.c% gcc std=c99 o printints printints.o% ./printintsx = 10, y = 20%
TVF / RIT 20195 CS243: Evolution
// Uses the preprocessor directive #define to define// a macro CONSTANT. Any future occurrence of CONSTANT// (not in a string) in the source code is replaced with // the literal 5, prior to compilation.//// To see the preprocessed code, prior to compilation,// compile as:// gcc E define.c
#include <stdio.h> // printf
#define CONSTANT 5// const int CONSTANT = 5;
int main( void ) { int x = 10 * CONSTANT; int y = 20 CONSTANT; printf("x = %d, y = %d, CONSTANT = %d\n", x, y, CONSTANT);}
Example C Program: Print Integers II
% gcc std=c99 c define.c% gcc std=c99 o define define.o% ./definex = 50, y = 15, CONSTANT = 5%
TVF / RIT 20195 CS243: Evolution
Example C Program: Print Integers II
• Use of Wall option:
% gcc Wall c define.cdefine.c: In function 'main':define.c:20:1: warning: control reaches end of nonvoid function [Wreturntype]% gcc o define define.o% ./definex = 50, y = 15, CONSTANT = 5%
TVF / RIT 20195 CS243: Evolution
// constants.h// defines names to represent constant values in c code.
#define INCHES_PER_CENTIMETER 0.394#define CENTIMETERS_PER_INCH (1 / INCHES_PER_CENTIMETER)
#define QUARTS_PER_LITER 1.057#define LITERS_PER_QUART (1 / QUARTS_PER_LITER)
#define OUNCES_PER_GRAM 0.035#define GRAMS_PER_OUNCE (1 / OUNCES_PER_GRAM)
Example C Program: Use of Header Files (1/2)
TVF / RIT 20195 CS243: Evolution
// convert.c//// Convert several literals from inches to centimeters using// preprocessor definitions in constants.h
#include <stdio.h> // printf#include "constants.h" // CENTIMETERS_PER_INCH
// A function that converts inches into centimeters// and displays the resultvoid convert( int inches ) {
printf("%d inches = %f centimeters\n", inches,inches * CENTIMETERS_PER_INCH);
}
int main( void ) {convert(10);convert(163);convert(5971);
}
Example C Program: Use of Header Files (2/2)
% gcc std=c99 c convert.c% gcc std=c99 o convert convert.o% ./convert10 inches = 25.380711 centimeters163 inches = 413.705584 centimeters5971 inches = 15154.822335 centimeters%
TVF / RIT 20195 CS243: Evolution
// Example program that contains implementation in multiple// source files://// compute_areas.c the main program, uses polygons.h for// the prototypes of area formula functions// polygons.h prototypes the functions in polygons.c// polygons.c implementation of polygon functions//// To compile source files into object files:// gcc std=c99 c compute_areas.c// gcc std=c99 c polygons.c//// To link object files into executable:// gcc o compute_areas compute_areas.o polygons.o lm//// To execute the result:// ./compute_areas
#include <stdio.h> // printf#include "polygons.h" // getArea, getCircumference
Example C Program: Multiple Source Files (1/6)
TVF / RIT 20195 CS243: Evolution
// A function that computes/displays the area of a squarevoid square( int side ) { printf( "Area of square with side length %d: %d\n", side, squareArea( side ) );}
// A function that computes/displays the area of a rectanglevoid rectangle( int length, int width ) { printf( "Area of rectangle with length %d and width %d: %d\n", length, width, rectangleArea( length, width ) );}
// A function that computes/displays the area of a trianglevoid triangle( double side1, double side2, double side3 ) { printf( "Area of triangle with side lengths “ “%0.2lf, %0.2lf, %0.2lf: %lf\n", side1, side2, side3, triangleArea( side1, side2, side3 ) );}
Example C Program: Multiple Source Files (2/6)
TVF / RIT 20195 CS243: Evolution
Example C Program: Multiple Source Files (3/6)
int main( void ) { square( 3 ); rectangle( 3, 7 ); triangle( 3.0, 4.0, 5.0 ); triangle( 24, 24, 24 );
return 0;}
TVF / RIT 20195 CS243: Evolution
Example C Program: Multiple Source Files (4/6)
// polygons.h//int rectangleArea( int len, int wid );
int squareArea( int sideLen );
double triangleArea( double a, double b, double c );
TVF / RIT 20195 CS243: Evolution
// polygons.c//#include <math.h> // sqrt#include "polygons.h"
// NOTE: Some distances are floating point and some are not;// this is just to illustrate handling both types.
int rectangleArea( int len, int wid ) { return len * wid;}
int squareArea( int sideLen ) { return rectangleArea( sideLen, sideLen );}
double triangleArea( double a, double b, double c ) { double s = ( a + b + c ) / 2; // "Semiperimeter" return sqrt( s * ( s a ) * ( s b ) * ( s c ) );}
Example C Program: Multiple Source Files (5/6)
TVF / RIT 20195 CS243: Evolution
Example C Program: Multiple Source Files (6/6)% gcc std=c99 c polygons.c% gcc std=c99 c compute_areas.c% gcc std=c99 o compute_areas compute_areas.o polygons.o lm% ./compute_areasArea of square with side length 3: 9Area of rectangle with side length 3 and width 7: 21Area of triangle with side lengths 3.00, 4.00, 5.00: 6.000000Area of triangle with side lengths 24.00, 24.00, 24.00: 249.415316%
TVF / RIT 20195 CS243: Evolution
What’s Next?
• More formal intro to C
• Intro to the OS and OS services
• Pointers
• Dynamic storage
• Program maintenance tools
• Parallel processing
• What we won’t cover: device I/O, networking, virtual memory