1 / 24

Introduction to Programming

Introduction to Programming. Chapter 1. Introduction to Programming. A computer is a machine It must be controlled A program is the tool we use to control a computer A computer program is a sequence of instructions that is used to operate a computer to produce a specific result.

junior
Télécharger la présentation

Introduction to Programming

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Chapter 1

  2. Introduction to Programming • A computer is a machine • It must be controlled • A program is the tool we use to control a computer • A computer program is a sequence of instructions that is used to operate a computer to produce a specific result.

  3. Introduction to Programming ProcesstheData Input Data Output Results

  4. Programming Languages • FORTRAN – (FORmula TRANslation [1957]) Designed for mathematical operations • BASIC – (Beginners All-purpose Symbolic Instruction Code [1960’s]) Designed for small interactive type programs • COBOL – (Common Business Oriented Language [1960’s]) Designed for business applications.

  5. Programming Languages • Pascal – (Named for the mathematician [1970’s]) Designed to teach programming concepts to students • C – (upgrade from B, developed from BCPL [1970’s]). General purpose language

  6. More about C • General purpose language • May be used to create simple interactive programs, or solve highly sophisticated and complex problems • C has many tools built into language; also it is quite easy to “add” tools/procedures to the language • C was designed “…to do work…”

  7. Our first C program #include <stdio.h>main(){ printf("Hello World!");}

  8. Our first C program # indicates pre-processor directive include is the directive stdio.h is the name of the file to "insert" into our program. The <> mean it is part of the c development system #include <stdio.h>main(){ printf("Hello World!");}

  9. Our first C program main is the name of the primary (or main) procedure (this is different from the file name). All ANSI C programs must have a main routine named main The () indicate that main is the name of a procedure. All procedure reference must be followed with () #include <stdio.h>main(){ printf("Hello World!");}

  10. Our first C program { } enclose a "block". A block is zero or more statements C statements. #include <stdio.h>main(){ printf("Hello World!");}

  11. Our first C program printf() is a "built-in" function (actually it's defined in stdio.h). "Hello World!" is the string to print. More formally, this is called the control string or control specifier. #include <stdio.h>main(){ printf("Hello World!");} Every statement must end with a ";". Preprocessing directives do not end with a ";" (but must end with a return).

  12. Entering/Running Program • Use vi to enter program / save program (insure that there is a return after the last }) • At the % prompt, type: cc myprog.cthis will create an executable file a.out • At the % prompt, type a.out

  13. File names • There are few rules for naming of files • Good practice:- Begin file names with letter- Use only letters, digits, _ , . for rest of name. • Remember uppercase/lowercase not equal

  14. File types • There is no such thing as a file extension in Unix. • There are conventional "endings" of file names.c – c program file.o – Object file (half compiled/translated module).txt – text file.out – output file

  15. Legal file names Myprog.cmyprog.cMyProg.cMYPROG.CMYPROG.cpayroll.txtb4time.cx09.21.2001.txtlong_ago_in_a_galaxy_far_far_awayperhaps…somedaydo…you…wish…the…first…Apollo…mission…had…failed

  16. Entering/Running Program(Example – user input underlined) goliath% cc myprog.cgoliath% a.outHello there World!goliath%

  17. Entering/Running Program • If you wish to name the executable file something other than a.out, then: goliath% cc –o myprog myprog.c goliath% myprog Execute myprog Creates output (executable) file myprog from myprog.c

  18. Entering/Running Program • .exe extension is nothing special. The following is permissible, but not needed: goliath% cc –o myprog.exe myprog.c goliath% myprog.exe Execute myprog.exe Creates output (executable) file myprog.exe from myprog.c

  19. Variations #1 of first program #include <stdio.h>main(){ printf("Hello"); printf("there"); printf("World!");}

  20. Variations #2 of first program #include <stdio.h>main(){ printf("Hello\n"); printf("there\n"); printf("World!\n");}

  21. Variations #3 of first program #include <stdio.h>main(){ printf("Hello\nthere\nWorld!\n");}

  22. Variations #4 of first program #include <stdio.h>main(){printf("Hello\nthere\nWorld!\n");} Note while this is syntactically correct, it leaves much to be desired in terms of readability.

  23. C program #2 #include <stdio.h>main(){ printf("%f plus %f equals %f\n",15.0,2.0, 15.0 + 2.0); printf("%f minus %f equals %f\n",15.0,2.0,15.0-2.0); printf("%f times %f equals %f\n",15.0,2.0, 15.0*2.0); printf("%f divided %f equals %f\n",15.0,2.0,15.0/2.0);}

  24. #include <stdio.h> main() { printf("%f plus %f equals %f\n",15.0,2.0, 15.0 + 2.0); printf("%f minus %f equals %f\n",15.0,2.0,15.0-2.0); printf("%f times %f equals %f\n",15.0,2.0, 15.0*2.0); printf("%f divided %f equals %f\n\n",15.0,2.0,15.0/2.0); printf("%d plus %d equals %d\n",15,2, 15 + 2); printf("%d minus %d equals %d\n",15,2,15-2); printf("%d times %d equals %d\n",15,2, 15*2); printf("%d divided %d equals %d\n\n",15,2,15/2); printf("%d divided by %d equals %d\n",1,10,1/10); printf("%d divided by %d equals %d\n",4,10,4/10); printf("%d divided by %d equals %d\n",5,10,5/10); printf("%d divided by %d equals %d\n",6,10,6/10); printf("%d divided by %d equals %d\n",9,10,9/10); printf("%d divided by %d equals %d\n\n",10,10,10/10); printf("5 printed as a float is %f\n",5); printf("2000000 printed as a float is %f\n",2000000); printf("5.0 printed as a decimal integer is %d\n",5.0f); printf("2000000.0 prtd as a decimal intr is %d\n",2000000.0f); } C program #2 (rev 2)

More Related