47
CS 2130 Lecture 3 The C Programming Language

CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Embed Size (px)

Citation preview

Page 1: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

CS 2130

Lecture 3

The C Programming Language

Page 2: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Oh, Say Can You C?

Page 3: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Oh, Say Can You C?

• The C Programming Language is– Simple

• Not too many keywords

auto break case char

const continue default do

double else enum extern

float for goto if

int long register return

short signed sizeof static

struct switch typedef union

unsigned void volatile while

Page 4: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Oh, Say Can You C?

• The C Programming Language is– Simple

• Not too many keywords• Fairly simple syntax• Concise: Doesn't require much typing

Page 5: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Oh, Say Can You C?

• The C Programming Language is– Simple– Useless

• Doesn't have any I/O commands• Doesn't have strings• Doesn't have useful math functions (use Fortran!)

Page 6: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Oh, Say Can You C?

• The C Programming Language is– Simple– Useless– Complex/Complicated

• Requires libraries• Libraries can grow in size/complexity

– Search Google for "C Libraries"

• Libraries are available by platform• Non-standardization...always a problem• There is an ANSI C Library

Page 7: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

But how hard can it be?

Page 8: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

A First Try C Program

#include <stdio.h>

main()

{

printf(“Hello, world!\n”);

return 0;

}

Page 9: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

A First Try C Program

#include <stdio.h>

main()

{

printf(“Hello, world!\n”);

return 0;

}

?

Page 10: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Recall

• Modules can (and often should be) compiled at different times.

• Suppose we are compiling module A:

int x = 7;

int y = 42;

float z;

...

z = someFunction(x, y);

• Is the function call okay?

Page 11: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Originally...

• C just hoped for the best• With ANSI C things improved• We can write:

float someFunction(int a, int b)

{

/* What it does */

}• and we write a prototype:

float someFunction(int a, int b);

Page 12: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

The Compiler Uses the Prototype...

...to error check the function calls for parameter type and number as well as return value.

• The prototypes can be– Just placed at the beginning of the file

OR– Placed in special files called header files (.h)

• The #include statement is "including" one in our example (more later)

Page 13: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Header Files

• Contain prototypes• Not libraries

• Some common header files:

stdio.h

stdlib.h

unistd.h

• Weiss Appendix D is your friend!

Page 14: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Back to live action!

#include <stdio.h>

main()

{

printf(“Hello, world!\n”);

return 0;

}

OK?

Page 15: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

main returns a value

#include <stdio.h>

int main()

{

printf(“Hello, world!\n”);

return 0;

}

To where and why?

Page 16: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

main returns a value

#include <stdio.h>

int main()

{

printf(“Hello, world!\n”);

return 0;

}

0 means okay, right?But wait, 0 is false so maybe it's a bad result?

0 means okay, right?But wait, 0 is false so maybe it's a bad result?

Page 17: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

A First Try C Program

#include <stdio.h>

#include <stdlib.h>

int main()

{

printf(“Hello, world!\n”);

return EXIT_SUCCESS;

}

OK?

Weiss Appendix D is your friend!Weiss Appendix D is your friend!

Page 18: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

printf prototype

int printf( const char *Format, ... );

What's this?

Page 19: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

A First Try C Program

#include <stdio.h>

#include <stdlib.h>

int main()

{

(void) printf(“Hello, world!\n”);

return EXIT_SUCCESS;

}

Page 20: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Why the (void)?

• printf(3) wants to return something• The C compiler will assume that you are just throwing

the return value away• lint will reject it• You will do poorly• Next thing you know...you're serving fries at

McDonalds

Page 21: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

But seriously...

• Major objective of course?– Teach you how to write small test programs that

will be graded and discarded?

– Teach you how to write programs that are part of large systems that control machines or devices that may affect human life, large amounts of capital, the well being of thousands of employees and/or stockholders?

Page 22: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Easier

To learn how to do it the right way right from the start!

Law of Primacy: "That which is learned first is learned best"

Page 23: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Date: December, 1999• Program: Mars Polar Lander• Cost: $185 million• Error: Signaling problem in the landing legs caused

by one line of missing computer code• Result: Lander lost, presumed crash-landed

Page 24: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Date: July 22, 1962• Program: Mariner I, Atlas-Agena rocket.• Cost: $18.5 million• Error: Used raw value instead of average• Result: Rocket blown up by range safety officer

Page 25: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?• Date: June and early July of 1991 • Program: Telephone switching software DSC

Communication• Cost: Unknown (but a bunch)• Error: After 13 weeks of successful testing changed 3

lines out of several million.• Result: A series of outages affected telephone users

in Los Angeles, San Francisco, Washington, D.C., Virginia, W. Virginia, Baltimore, and Greensboro, N.C.

• Comment: They knew what that change did, and they were confident that it did nothing else.(2) And presumably, the customer wanted it now.

Page 26: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Date: July 20, 1969• Program: Apollo, first manned landing on moon.• Cost: Almost...• Error: Programmers left debugging messages in code• Result: Sloppy last-minute changes caused a system

that was not even in use to start generating "alarms"

• Comment: Mission controller had been warned of "alarms". Had 19 seconds to make correct decision!

Page 27: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Date: February 25, 1991• Program: Patriot Missile System/Gulf War• Cost: Killed 28 Wounded 98 American Soldiers• Error: Tracking radar had a cumulative error bug

which would be fixed if system was shutdown every day.

• Result: Guidance system "lost" incoming "Scud" missle

Page 28: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Comment: The bug was noticed almost as soon as the Gulf War began. By February 25th, it had actually been fixed, but the programmers at Raytheon also wanted to fix other bugs deemed more critical. By the time all the bugs had been fixed--and a new version of the software had been copied onto tape-- the tape had been sent to Ft. McGuire Air Force Base--and then flown to Riyadh--and then trucked to Dhahran--and then loaded into the Patriot installation--well, by that time it was February 26th, and the dead were already dead, and the war was just about over.

Page 29: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Date: November 20, 1985• Program: Bank of New York Program to track

government securities transactions.• Cost: $5 million• Error: Latest transaction continuously overwriting last

transaction Lost $32 BILLION. With effort had that down to only $23.6 BILLION by end of the day. $5 million was interest to cover missing funds for 2 days!!!

• Result: Bank lost confidence of investors.

• Comment: Disruption of econometric models.

Page 30: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Date: Spring 1992• Program: Canadian Vote-by-Phone System• Cost: No manual backup...system response too slow.

Some people couldn't vote. Some people voted more than once.

• Error: Bad assumptions about system performance. No backup plan.

• Result: Chaos and embarrassment for political party and TPC

Page 31: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Date: June 1985 - January 1987• Program: Therac 25. Computer controlled radiation

therapy machine.• Cost: 6 patients died horribly painful deaths• Error: Machine had two modes: Electrons, X-rays.

Software bug caused machine to run at power setting 100 times too high.

• Result: Patients physically felt pain from beam. Rapidly developed radiation sickness and died agonizing deaths over the course of several months.

• Comment: First model to rely strictly on computer control instead of mechanical interlocks.

Page 32: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

What can go wrong?

• Date: Mid 70's• Program: NASA satellites• Cost: 10 year delay in knowing of problem• Error: Data processing program ignored values that

were very low.• Result: Holes in Ozone layers were not discovered

until 1986.

Page 33: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

And now a "positive" outcome

• Date: July, 1997• Program: Mars Pathfinder mission• Error: System periodically reset itself, cause unknown• Solution: JPL engineers had fortuitously left the

RTOS debugger/interpreter enabled in the software when it was installed. This allowed them to test and debug the mission software in situ. The fault was isolated, and a short C program was written and uploaded to the spacecraft. This program, when interpreted, fixed the problem

• Result: No more resets occurred

Page 34: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

And now a "positive" outcome

• Software designers had sacrificed "correct" software behavior for the sake of expediency and to meet mission deadlines (sound familiar?)

• Diagnosing the problem without direct access to the running system would have proved impossible

• Leaving the debugger installed and enabled saved the project

• Note: JPL engineers later confessed that a few unexplained resets had occurred during initial testing. The resets were not reproducible or explainable, and did not occur in what were considered to be "mission critical" parts of the software. They were eventually dismissed as the result of "hardware glitches".

Page 35: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Bug Free Software

• We want bug free software

• Is it possible?

• Two strategies

Page 36: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Bug Free Software

• We want bug free software

• Is it possible?

• Two strategies

ErrorError

Page 37: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Strategy One

• You write software

• Testing department rigorously tests software applying specifications

Page 38: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Strategy 2

• You write software• You build in code to handle every possible thing that

might go wrong• You test code knowing how code works. This

includes testing for special cases, etc.• You request the compiler warn you of every possible

problem (plus you use lint)

• Testing department rigorously tests software applying specifications

Page 39: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Bottom Line

• We want you to program like you're an anal retentive paranoid delusional programmer.

• Not just for this course!

Page 40: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Style

• Everything Returns Something

a = b = c = 0;

• So which is better?

a = b = c = 0;

OR

a = 0;

b = 0;

c = 0;

Page 41: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

No such thing...

...as a program that is 100% correct.

Page 42: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Eiselt’s Lemma

UnderstandabilityC

orr

ect

ness Lose Lose!

+

+

-

-

Potential disaster

Win Win!

Can be fixed

• More important for a program to be understandable than correct

Page 43: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Style

• Pick a style• Use it consistently

• Program appearance should reflect clarity of thought

• Precedence– Need to look it up– Use parentheses

Page 44: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Capital Crimes

• Doesn’t compile• Dumps core• Spurious output

• No mercy rule

• Remember:

gcc -Wall -O2 -ansi -pedantic(Note: TAs call this "gcc -Peter" - ask them why!)

plus

lint free where possible (acme)

Page 45: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Things to Know

• Short circuit evaluation• break and continue statements• ? : (for optimum performance, but use sparingly)• goto statement

• Call by Value (Pass by Value)

Page 46: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?

Questions?

Page 47: CS 2130 Lecture 3 The C Programming Language. Oh, Say Can You C?