Cs 2130
Download
1 / 67

cs2130pres04cprogrammingwhatcangowrong 67 - PowerPoint PPT Presentation


  • 327 Views
  • Uploaded on

CS 2130. Presentation 04 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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'cs2130pres04cprogrammingwhatcangowrong 67' - Ava


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
Cs 2130

CS 2130

Presentation 04

The C Programming Language



Oh say can you c1
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


Oh say can you c2
Oh, Say Can You C?

  • The C Programming Language is

    • Simple

      • Not too many keywords

      • Fairly simple syntax

      • Concise: Doesn't require much typing


Oh say can you c3
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!)


Oh say can you c4
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



A first try c program
A First Try C Program

#include <stdio.h>

main()

{

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

return 0;

}


A first try c program1

?

A First Try C Program

#include <stdio.h>

main()

{

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

return 0;

}


Recall
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?


Originally
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 can write a prototype:

    float someFunction(int a, int b);


The compiler uses the prototype
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)


Header files
Header Files

  • Contain prototypes

  • Not libraries

  • Some common header files:

    stdio.h

    stdlib.h

    unistd.h

  • Weiss Appendix D is your friend!


Header files1
Header Files

WARNING: Header files (.h) are for function prototypes as well as for certain items such as definitions of new types

EXECUTABLE CODE SHOULD NEVER GO INTO A HEADER FILE!!!


Side note

Error

Side Note

  • While we are mentioning functions...

  • Unlike Java, C does not allow function overloading

    • Java

      public void someFunction(int i) { }

      public void someFunction(float x) { }

    • C

      void someFunction(int i) { }

      void someFunction(float x) { }


Back to live action
Back to live action!

#include <stdio.h>

main()

{

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

return 0;

}

OK?


Main returns a value
main returns a value

#include <stdio.h>

int main()

{

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

return 0;

}

To where and why?


Main returns a value1
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?


A first try c program2
A First Try C Program

#include <stdio.h>

#include <stdlib.h>

intmain()

{

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

return EXIT_SUCCESS;

}

Weiss Appendix D

is your friend!

OK?


Printf prototype
printf prototype

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

What's this?


A first try c program3
A First Try C Program

#include <stdio.h>

#include <stdlib.h>

intmain()

{

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

return EXIT_SUCCESS;

}

Don't Do This!!!


Why the void
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

  • Note: This is not to say that there is anything wrong with people who serve fries at McDonalds

The McDonalds Corporation

in no way endorses anything

on this slide.


But seriously
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?


Easier

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"



June 28, 2002: NEW YORK (Reuters) - Software bugs are not just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

The impact of software errors is enormous because virtually every business in the United States now depends on software for the development, production, distribution, and after-sales support of products and services," NIST Director Arden Bement said in a statement on Friday.

Software users contribute about half the problem, while developers and vendors are to blame for the rest, the study said. The study also found that better testing could expose the bugs and remove bugs at the early development stage could reduce about $22.2 billion of the cost.

"Currently, over half of all errors are not found until 'downstream' in the development process or during post-sale software use," the study said.


Mariner i atlas agena rocket

Mariner I, Atlas-Agena rocket. just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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


Ozone satellites

Ozone Satellites just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong1
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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.


Apollo

Apollo just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong2
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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!


Mars polar lander

Mars Polar Lander just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong3
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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


Mars pathfinder

Mars Pathfinder just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


And now a positive outcome
And now a "positive" outcome just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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


And now a positive outcome1
And now a "positive" outcome just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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".


Canadian voting

Canadian Voting just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong4
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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


Bank of new york

Bank of New York just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong5
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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.


At t switching

AT&T Switching just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong6
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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.


Therac

Therac just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong7
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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.


Patriot

Patriot just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


What can go wrong8
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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


What can go wrong9
What can go wrong? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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.


Bug free software
Bug Free Software just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • We want bug free software

  • Is it possible?

  • Two strategies


Bug free software1
Bug Free Software just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

Error

  • We want bug free software

  • Is it possible?

  • Two strategies

Error


Strategy one
Strategy One just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • You write software

  • Testing department rigorously tests software applying specifications


Strategy 2
Strategy 2 just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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


Bottom line
Bottom Line just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

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

  • Not just for this course!


Fix bugs immediately
Fix Bugs Immediately just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • Don't adopt a "bug list policy"

  • Projects can and will get out of control!


Always test
Always Test!!!! just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • But I just cleaned up a few comments!

  • But I just changed a few variable names!

  • But I just erased one little teeny tiny curly brace!


Reminder

Reminder just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

Don't be in other CS2130 lab sections.


Style
Style just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • Everything Returns Something

    a = b = c = 0;

  • So which is better?

    a = b = c = 0;

    OR

    a = 0;

    b = 0;

    c = 0;


No such thing

No such thing... just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

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


Eiselt s lemma

Understandability just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

-

+

Lose Lose!

Can be fixed

-

Correctness

Potential disaster

Win Win!

+

Eiselt’s Lemma

  • More important for a program to be understandable than correct


Style1
Style just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • Pick a style

  • Use it consistently

  • Program appearance should reflect clarity of thought

  • Precedence

    • Need to look it up

    • Use parentheses


Capital crimes
Capital Crimes just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • 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)


Things to know
Things to Know just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

  • Short circuit evaluation

  • break and continue statements

  • ? : (for optimum performance, but use sparingly)

  • goto statement

  • Call by Value (Pass by Value)


Fortran
Fortran just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

SUBROUTINE SWAP(I,J)

T = I

I = J

J = T

RETURN

END

IA = 7

IB = 42

SWAP(IA, IB)

WRITE(7,1) IA, IB

1 FORMAT(2I10)

pass by reference

42 7


c just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

Void swap(int i, int j)

{

int t;

t = i;

i = j;

j = t;

}

int ia = 7;

int ib = 42;

swap(ia, ib);

printf("%d %d);

pass by value

7 42


Sneak preview of c
Sneak Preview of c just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.

Void swap(int *i, int *j)

{

int t;

t = *i;

*i = *j;

*j = t;

}

int ia = 7;

int ib = 42;

swap(&ia, &ib);

printf("%d %d);

Emulating

pass by value

42 7


Questions

Questions? just annoying or inconvenient. They're expensive. According to a study by the U.S. Department of Commerce's National Institute of Standards and Technology (NIST), the bugs and glitches cost the U.S. economy about $59,500,000,000 (59.5 billion) a year.


ad