240 likes | 428 Views
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.
E N D
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.
Introduction to Programming ProcesstheData Input Data Output Results
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.
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
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…”
Our first C program #include <stdio.h>main(){ printf("Hello World!");}
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!");}
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!");}
Our first C program { } enclose a "block". A block is zero or more statements C statements. #include <stdio.h>main(){ printf("Hello World!");}
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).
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
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
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
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
Entering/Running Program(Example – user input underlined) goliath% cc myprog.cgoliath% a.outHello there World!goliath%
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
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
Variations #1 of first program #include <stdio.h>main(){ printf("Hello"); printf("there"); printf("World!");}
Variations #2 of first program #include <stdio.h>main(){ printf("Hello\n"); printf("there\n"); printf("World!\n");}
Variations #3 of first program #include <stdio.h>main(){ printf("Hello\nthere\nWorld!\n");}
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.
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);}
#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)