Upload
kelley-isabella-barker
View
214
Download
0
Embed Size (px)
Citation preview
OBJECTIVES
In this lecture you will learn: Basic computer concepts. The different types of programming languages. The influential programming languages. The purpose of the C Standard Library. The elements of a typical C program
development environment. To write simple computer programs in C.
What is a Computer?
Computer Device capable of performing
computations and making logical decisions.
Processing data under the control of sets of instructions called computer programs.
Hardware Various devices comprising a computer. Keyboard, screen, mouse, disks, memory, CD-
ROM, and processing units. Software
Programs that run on a computer.
Computer Organization
Six logical units Input unit: Obtains information from
input devices (keyboard, mouse) Output unit: Outputs information (to
screen, printer, other devices) Memory unit: Rapid access, low capacity,
temporarily storing information Arithmetic and logic unit (ALU):
Performs arithmetic calculations and logic decisions
Central processing unit (CPU): Supervises and coordinates the whole computer
Secondary storage unit Cheap, long-term, high-capacity storage Stores inactive programs
1) Monitor2) Motherboard3) CPU4) Main memory5) Expansion cards6) Power supply unit7) Optical disc drive8) Hard disk drive9) Keyborad10) mouse
Operating Systems
What is the OS? Resource allocator
allows the proper use of resources (hardware, software,data)
provides an environment in which other programs can do useful work
Control program controls the execution of user programs to
prevent errors and improper use of the computer controls the operation of I/O devices
What is the purpose? Manage transitions between jobs Increasing throughput (amount of work
processed)
Operating Systems - A Short History
Simple batch systems Do only one job or task at a time. The programs and data
were submitted in batches with punch cards. IBSYS (1950’s)
Multi-programming (multi-tasking) batch systems
Computer resources are shared by multiple jobs or tasks
IBM’s OS/360 (early 1960’s)
Time-sharing systemsComputer runs a small portion of one user’s job then moves on to service the next userMIT’s CTSS (1962), Multics (1968), UNIX (1969)
Personal computer systems Microprocessor technology evolved to the point that it become
possible to build desktop computers as powerful as the mainframes of the 1970s.
DOS (1980), Windows 3.0 (1990), Linux (1991)
Programming Languages
Machine languages Strings of numbers giving machine
specific instructions Example: +1300042774
+1400593419+1200274027
Assembly languages English-like abbreviations representing
elementary computer operations (translated via assemblers)
Example: LOAD BASEPAYADD OVERPAYSTORE GROSSPAY
Three types of programming languages. ……
…
BASEPAY
OVERPAY
GROSSPAY
2000
500
2500
RAM
Programming Languages
High-level languages Codes similar to everyday English Use mathematical notations Example:
grossPay = basePay + overTimePay Execution of high-level language programs
Complier: to convert high-level language programs into machine language.
Interpreter: to execute high-level language programs directly without compiling into machine language.
Compiling takes a considerable amount of computer time.
Compiled programs execute much faster than interpreted programs.
Three types of programming languages (cont.)
Influential Programming Languages
Fortran (IBM 1950s) Used for scientific and engineering applications require
complex mathematical computations
COBOL (1959) Used for commercial applications that require precise
and efficient manipulation of large amounts of data
Pascal (Prof. Niklaus Wirth, 1971) Designed for teaching structured programming
Ada (DOD, 1970s and early 1980s) Able to perform multitasking
C (late 1970s)Evolved from B (BCPL) by Dennis Ritchie at Bell Lab, 1972Development language of UNIXWidely used to develop modern operating systems.Hardware independent (portable)
Influential Programming Languages
C++ (Bjarne Stroustrup at Bell Labs, 1983-1985) Superset of C, providing object-oriented capabilities Objects: reusable software components that model
items in the real world. Object-oriented programming technique can make
software development much more effective Dominant language in industry and academia
JAVA (Sun Microsystems, 1995)Create Web pages with dynamic and interactive contentDevelop large-scale enterprise applicationsEnhance the functionality of Web serversProvide applications for intelligent consumer devices (such as cell phones, pagers, and personal digital assistants)
C Standard Library
C programs consist of pieces/modules called functions
A programmer can create his/her own functions Pros: the programmer knows exactly how it works Cons: time consuming
Programmers often use the C library functions to avoid re-inventing the wheel
If a pre-made function exists, generally best to use it rather than write your own
Library functions carefully written, efficient, and portable
C standardization Many variation that were similar existed, and were
incompatible across hardware platforms A standard version of C was needed ANSI/ISO C created in 1989 and updated in 1999
Typical C Program Development Environment
Phases of C programming
Edit Preprocess Compile Link Load Execute
openSUSEgeditgcc
A Simple C Program
1 /* Fig. 2.1: fig02_01 .c
2 A first program in C */
3 #i ncl ude <stdio .h>
4
5 /* fu nction ma in beg ins program execution */
6 int main ( void )
7 {
8 prin tf( "Welcom e to C!\n" );
9
10 ret urn 0; /* indica te th at program ended successful ly */
11
12 } /* e nd function ma in */
/* and */ indicate comments – ignored by compiler
#include directive tells C to load a particular file
Left brace declares beginning of main function
Statement tells C to perform an action
return statement ends the function
Right brace declares end of main function
Every C program begins executing at the function main.
Print a line of text Illustrate several important features of the C
language
A Simple C Program 1 /* Fig. 2.1: fig02_01 .c
2 A first program in C */
3 #i ncl ude <stdio .h>
4
5 /* fu nction ma in beg ins program execution */
6 int main ( void )
7 {
8 prin tf( "Welcom e to C!\n" );
9
10 ret urn 0; /* indica te th at program ended successful ly */
11
12 } /* e nd function ma in */
Comments
Text surrounded by /* and */ is ignored by compiler Used to describe program
A Simple C Program 1 /* Fig. 2.1: fig02_01 .c
2 A first program in C */
3 #i ncl ude <stdio .h>
4
5 /* fu nction ma in beg ins program execution */
6 int main ( void )
7 {
8 prin tf( "Welcom e to C!\n" );
9
10 ret urn 0; /* indica te th at program ended successful ly */
11
12 } /* e nd function ma in */
#include <stdio.h>
Preprocessor directive Tells computer to load contents of a certain file
<stdio.h> allows standard input/output operations
A Simple C Program 1 /* Fig. 2.1: fig02_01 .c
2 A first program in C */
3 #i ncl ude <stdio .h>
4
5 /* fu nction ma in beg ins program execution */
6 int main ( void )
7 {
8 prin tf( "Welcom e to C!\n" );
9
10 ret urn 0; /* indica te th at program ended successful ly */
11
12 } /* e nd function ma in */
int main()
C programs contain one or more functions, exactly one of which must be main
Parenthesis used to indicate a function int means that main "returns" an integer value Braces ({ and }) indicate a block
The bodies of all functions must be contained in braces
A Simple C Program 1 /* Fig. 2.1: fig02_01 .c
2 A first program in C */
3 #i ncl ude <stdio .h>
4
5 /* fu nction ma in beg ins program execution */
6 int main ( void )
7 {
8 prin tf( "Welcom e to C!\n" );
9
10 ret urn 0; /* indica te th at program ended successful ly */
11
12 } /* e nd function ma in */
printf( "Welcome to C!\n" );
Entire line called a statement All statements must end with a semicolon (;)
Instructs computer to perform an action prints the string of characters within quotes (" ")
Escape character (\) \n is the newline character
Some Common Escape Sequences
Escape sequence Description
\ n Newline. Position the cursor at the beginning of the next line.
\ t Horizontal tab. Move the cursor to the next tab stop.
\ a Alert. Sound the system bell.
\ \ Backslash. Insert a backslash character in a string.
\ " Double quote. Insert a double -quote character in a string.
A Simple C Program 1 /* Fig. 2.1: fig02_01 .c
2 A first program in C */
3 #i ncl ude <stdio .h>
4
5 /* fu nction ma in beg ins program execution */
6 int main ( void )
7 {
8 prin tf( "Welcom e to C!\n" );
9
10 ret urn 0; /* indica te th at program ended successful ly */
11
12 } /* e nd function ma in */
return 0;
A way to exit a function return 0, in this case, means that the program
terminated normally
A Simple C Program 1 /* Fig. 2.1: fig02_01 .c
2 A first program in C */
3 #i ncl ude <stdio .h>
4
5 /* fu nction ma in beg ins program execution */
6 int main ( void )
7 {
8 prin tf( "Welcom e to C!\n" );
9
10 ret urn 0; /* indica te th at program ended successful ly */
11
12 } /* e nd function ma in */
Right brace }
Indicates end of main has been reached
Good Programming PracticeEvery function should be preceded by a comment describing the purpose of the function.Add a comment to the line containing the right brace “}” that closes every function, including main.The last character printed by a function that displays output should be a newline (\n).
Ensures that the function will leave the screen cursor positioned at the beginning of a new line.Conventions of this nature encourage software reusability—a key goal in software development environments.
Indent the entire body of each function one level of indentation within the braces that define the body of the function.
Emphasizes the functional structure of programsHelps make programs easier to readUniformly apply a fixed size of indent.
Example of Using “\n”
1 /* Fig. 2.3: fig02_03.c
2 Printing on one line with two printf statements */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 printf( "Welcome " );
9 printf( "to C! \ n" );
10
11 return 0; /* indicate that program ended successfully */
12
13 } /* end function main */ Welcome to C!
printf statement starts printing from where the last statement ended, so the text is printed on one line.
output
Example of Using “\n”
1 /* Fig. 2.4: fig02_04 .c
2 Print ing mu ltiple line s with a sing le prin tf */
3 #i ncl ude <stdio .h>
4
5 /* fu nction ma in beg ins program execution */
6 int main ( void )
7 {
8 prin tf( "Welcome\nto\nC!\n" );
9
10 ret urn 0; /* indica te th at program ended successful ly */
11
12 } /* e nd function ma in */ Welcom e to C!
Newline characters move the cursor to the next line
output