350 likes | 489 Views
Introduction to Programming. 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 • 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>int main(){ printf("Hello World!\n"); return 0;}
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>int main(){ printf("Hello World!\n"); return 0;}
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>int main(){ printf("Hello World!\n"); return 0;}
Our first C program { } enclose a "block". A block is zero or more statements C statements. #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;}
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>int main(){ printf("Hello World!\n"); return 0;} Every statement must end with a ";". Preprocessing directives do not end with a ";" (but must end with a return).
Our first C program The int tells the compiler our main() program will return an integer to the operating system; the return tells what integer value to return #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;}
Variations #1 of first program #include <stdio.h>int main(){ printf("Hello"); printf("there"); printf("World!"); return 0;}
Variations #2 of first program #include <stdio.h>int main(){ printf("Hello\n"); printf("there\n"); printf("World!\n"); return 0;}
Variations #3 of first program #include <stdio.h>int main(){ printf("Hello\nthere\nWorld!\n"); return 0;}
Variations #4 of first program #include <stdio.h>int main(){printf ("Hello\nthere\nWorld!\n");return 0;} Note while this is syntactically correct, it leaves much to be desired in terms of readability.
C program #2 #include <stdio.h>int 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); return 0;}
#include <stdio.h> int 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); return 0; } C program #2 (rev 2)
Four Types of Basic Data • Integer int • Floating point (single precision) float • Double Precision double • Character char
Integer Constants • Any positive or negative number without a decimal point (or other illegal symbol). • Legal 5 -10 +251000 253 -26351 +98 • Illegal2,523 (comma) 6.5 (decimal point)$59 (dollar sign) 5. (decimal point)
Range of Integers (Machine Dependent) unsigned signed • char 0 255 -128 +127 • short int 0 65535 -32768 + 32767short • int 0 to 4294967295 –2147483648 2147483647 longlong int
Floating Point and Double Precision Constants • Any signed or unsigned number with a decimal point • Legal 5. .6 +2.70.0 -6.5 +8. 43.4 • Legal (exponential notation)1.624e3 7.32e-2 6.02e23 1.0e2-4.23e2 +4.0e2 1.23e-4 +11.2e+7 • Illegal $54.23 5 6,349.7025 1e5 5.3e3.2 1.0E5
Floating Point and Double Precision Constants • Range of float (single precision) ± 1.175494351 E – 38 ± 3.402823466 E + 38 • Range of double± 2.2250738585072014 E – 308± 1.7976931348623158 E + 308
Accuracy vs. Precision(Digital Clock) 07:30:45 What is the Precision? What is the Accuracy?
Accuracy vs. Precision • Precision – is how closely you can read a value from a given device • Accuracy – is how close a given device is to some fixed standard
Character Constants • Stored in ASCII or UNICODE • Valid character constants ’A’ ’B’ ’d’ ’z’ ’1’ ’2’’!’ ’+’ ’>’ ’?’ ’ ’ ’#’ • Invalid character constants’VIALL’ ’\’ ’CR’ ’LF’ ’’’ ’’’’ ’”’ ”Q”
Arithmetic Operations 3+7 18 - 3 12.62 + 9.8 .08*12.3 12.0/ 2.0 10/5 10/3 10 % 3 12 % 5 Note – Spaces may be used freely between a constant and an operator. Spaces may not be used within a constant.
Results of Arithmetic Operations 3+7 10 18 - 3 15 12.62 + 9.8 22.42 .08*12.3 0.984 12.0/ 2.0 6.0 10/5 2 10/3 3 (not 3.333…) 10 % 3 1 12 % 5 2