phy 107 – programming for science. history of c dennis ritchie developed c from 1969 – 1973 ...
TRANSCRIPT
![Page 1: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/1.jpg)
LECTURE 2:PROGRAMMING BASICS
PHY 107 – Programming For Science
![Page 2: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/2.jpg)
History of C
Dennis Ritchie developed C from 1969 – 1973
Based upon B (& other) earlier languages Since its creation, language grown
organically Tradition of adding features beyond standard
as desired
![Page 3: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/3.jpg)
History of C++
Bjarne Stroustrup created to add “objects” Also included many other improvements to
language Name is inside joke: "++" increases value
by 1 in C Updated for quick growth
ISO standard adopted in 1998 Recently updated to C++ 201x
![Page 4: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/4.jpg)
C Versus C++
C++ is designed to be as compatible with C as
possible, thereby providing a
smooth transition from C
![Page 5: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/5.jpg)
C Versus C++
C++
C
![Page 6: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/6.jpg)
C Versus C++
C
![Page 7: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/7.jpg)
C Versus C++
Latest definition of C added most C++ features Classes & objects excepted, these only
found in C++ For this reason, also not a part of PHY 107 Differences now minimal and easily
avoided Once objects removed, C++ just
“looser” C Removes annoying restrictions that had
been in C Since makes life easier, often supported in
C anyway
![Page 8: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/8.jpg)
![Page 9: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/9.jpg)
Computers are VERY, VERY stupid
![Page 10: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/10.jpg)
Computers have no common-sense They will only do what you tell them to
do NOT what you want them to do, which
often differs
![Page 11: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/11.jpg)
Computers have no common-sense They will only do what you tell them to
do NOT what you want them to do, which
often differs
![Page 12: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/12.jpg)
Computers have no common-sense They will only do what you tell them to do NOT what you want them to do, which
often differs
While this is true for everything computer does Programming highlights exactly how this
happens As you will see, C/C++ does nothing to
prevent mistakes
![Page 13: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/13.jpg)
Case-Sensitivity
Example of computers being very literal And language not helping by fixing what you
say
main, Main, & MAiN treated as totally different Case of the letters matters, not just the
words Might want difference; C won’t change Main
to main Can help prevent easy mistakes from
swapping names With just a little practice, becomes second
nature
![Page 14: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/14.jpg)
“Whitespace”
One (very small) way C/C++ actually helps you Treats all whitespace equally – spaces,
enters, & tabs Whether 1 or 1000000000000 – all will be
ignored Cannot use in symbol, whitespace
splits words Treats these as different “: :” and “::”
Spaces between words needed, but not counted Wecansplitwordsbutthecomputercannot
![Page 15: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/15.jpg)
“Whitespace”
One (very small) way C/C++ actually helps you Treats all whitespace equally – spaces,
enters, & tabs Whether 1 or 1000000000000 – all will be
ignored Cannot use in symbol, whitespace
splits words Treats these as different “: :” and “::”
Spaces between words needed, but not counted Wecansplitwordsbutthecomputercannot
![Page 16: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/16.jpg)
Your First C Program
#include <stdio.h>#include <stdlib.h>
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ printf(“Hello world!”); return 0; // This comment goes to the line’s end}
![Page 17: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/17.jpg)
#include Statements
#include <stdio.h>#include <stdlib.h>
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ printf(“Hello world!”); return 0; // This comment goes to the line’s end}
Nearly every C file begins with these lines May add more #include to include other
files Contents of included file usable as if it
were here Easy way to copy ideas across multiple
files Programs can use two types of #include
statements Include system file using #include <filename>
#include “filename” includes a file you wrote
![Page 18: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/18.jpg)
Watch Me Pull a Rabbit
#include <stdio.h>#include <stdlib.h>
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ printf(“Hello world!”); return 0; // This comment goes to the line’s end}
For now, automatically start each file with this line Details are unimportant – consider it
magic
![Page 19: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/19.jpg)
Watch Me Pull a Rabbit
#include <stdio.h>#include <stdlib.h>
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ printf(“Hello world!”); return 0; // This comment goes to the line’s end}
For now, automatically start each file with this line Details are unimportant – consider it
magic
![Page 20: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/20.jpg)
Your First C Program
#include <stdio.h>#include <stdlib.h>
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ printf(“Hello world!”); return 0; // This comment goes to the line’s end}
![Page 21: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/21.jpg)
main Function
#include <stdio.h>#include <stdlib.h>
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ printf(“Hello world!”); return 0; // This comment goes to the line’s end}
All C++ programs contain function called main Tells computer where to start running
program Code inside the braces will be what is
executed For the moment, consider this more “magic”
![Page 22: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/22.jpg)
main Function
#include <stdio.h>#include <stdlib.h>
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ printf(“Hello world!”); return 0; // This comment goes to the line’s end}
All C++ programs contain function called main Tells computer where to start running
program Code inside the braces will be what is
executed For the moment, consider this more “magic”
![Page 23: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/23.jpg)
Comments
Vital for writing and maintaining any program Not required to run program - only for
human eyes Computer simply ignores anything in a
comment Use to describe code in simple English
Sie konnen auch auf Deutsch schreiben o U c%d wrte n txt msg
Should be used liberally I add comments where cannot see what
code does Impossible to have too many comments, if
readable
![Page 24: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/24.jpg)
Comments in C/C++
Double slash comments continue to line’s enda = a – 4; // Hi, Mom!// This entire line is a comment!
/* … */ comments can be on one or more linesa = a - /* Hi, Mom! */ 4;/* This comment takes an entire line. *//* This is a really long comment that * goes on to multiple lines. The stars on * lines 2 and on are optional, but * makes things easier to read. */
![Page 25: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/25.jpg)
Your Turn
Work on activity in groups until 11:45 Each group will submit 1 copy at end Professor chooses the copy; so must work
together
![Page 26: PHY 107 – Programming For Science. History of C Dennis Ritchie developed C from 1969 – 1973 Based upon B (& other) earlier languages Since its creation,](https://reader030.vdocument.in/reader030/viewer/2022032805/56649edf5503460f94bef87a/html5/thumbnails/26.jpg)
For Next Lecture
Will have lab tomorrow, so bring flash drive Have not done much, but we can start to see system
Read pages 34-37, 57-59, 242-243 for Friday What is a data type? What are variables? How do variables differ from numbers and letters?
Week #1 assignment on D2L & due Tuesday This week different – relies on tomorrow’s lab