Sie sind auf Seite 1von 29

Chapter 1

Computer Systems and C


Programming

1.1 Computer Systems


1.2 Computer Programming
1.3 Programming Languages
1.4 Why C Programming?
1.5 Structure of a C Program
1.6 Development of a C Program

1
1.1 Computer Systems
Computer systems = Hardware + Software
Hardware Physical components/devices
Software Computer programs with
instructions to perform a particular task based
on the hardware

2
Computer Hardware
Components
CPU
- interprets and
executes instructions
Main Memory
- store data and
instructions
- byte = 8 bits
Input Device
- mouse, keyboard, etc.
Output Device
- printer, monitor, etc.
Secondary Storage
- permanent storage
- floppy disks, hard
disks, CDs

3
Computer Memory

4
Computer Software

5
1.2 Computer Programming
Computer Programs
Set of step-by-step instructions
Perform a specific task to solve a problem
Computer programming is to design
and implement computer programs
Examples
Computer games, word processors, web
browsers, database systems, etc.

6
1.3 Programming Languages

7
1.4 Why C Programming?
C is a programming language which is
Powerful
Flexible
Efficient
Portable
has features to enable the creation of well-
structured programs
Use ANSI (American National Standards
Institute) C Standard to resolve
incompatibility problem
Any Disadvantages? Or Advantages??
Free style programming
Pointers
8
1.5 Structure of a C Program
A simple C program has the following
structure:
preprocessor instructions
int main(void)
{
statements
return 0;
}
9
The preprocessor instructions refer to
the instructions to the preprocessor of
the compiler. All preprocessor
instructions start with #.
The #include <filename> instruction tells
the preprocessor to include the file
filename into the text of the program file
Other preprocessor instructions will be
described later
int main(void) is the header of the
program. Every program has this
header
10
The body of the program is enclosed by
the braces { }
A statement is a command to the
computer. A statement may be
a simple statement or
a compound statement.

11
A simple statement is one statement
terminated by a semicolon.
There are four types of simple
statements:
(1) declaration statements (Chapter 3)
(2) assignment statements (Chapter 4)
(3) control statements (Chapter 5 & 6)
(4) function call statements (Chapter 8)

12
A compound statement is a sequence of
one or more statements enclosed in
braces. For example,
{
statement_1;
statement_2;
...
statement_N;
}
Each of the statement_i can be a simple
or a compound statement.
13
return 0 is the last statement in every program.
At this point, just consider this statement as
something required for the logical consistency
of the execution of the program.
You may add comments to the program to
explain what the program is doing, or what a
portion of the program is doing.
a comment of a piece of English text
a comment is enclosed by /* and */
comments may appear anywhere in the program
comments make programs more readable to
yourself and to others
the compiler simply skips the comments when
translating your program
14
Example of a simple program:

#include <stdio.h>
/* a program to print Have a good day
on the screen */
int main(void)
{
printf("Have a good day.\n");
/* message printed */
return 0;
}
15
1.6 Development of a C Program

16
Compilation Process

17
To compile your program, type
$ gcc -ansi prog.c
$ is the command prompt
gcc is the command to call the C compiler
ansi is a compiler option to compile according to
ANSI C standard
prog.c is your program
If your program has no error, the compiler will
call the linker automatically and produce the
executable file a.out.
To compile and name your executable file, type
$ gcc -ansi prog.c -o prog
The -o option tells the linker to call the
executable file prog instead of the default name
a.out.
18
If your program uses some library functions like
the sqrt() function from the math library to
compute the square root of a number, you will
need to tell the compiler the library to use.
The compilation command will become
$ gcc -ansi prog.c -o prog -lm
-l tells the compiler to use a library.
m indicates the math library.
In addition to the change in the gcc command,
you also need to add
#include <math.h>
at the beginning of your program to tell the
preprocessor to include the definition file of the
math library.
19
The Execution of a C Program
To execute your program, just type
$ ./a.out
or if you have given a name to your
executable file, for example, prog, then
just type
$ ./prog
Your program will be executed.

20
ExecutingPrograms

21
Example 1.1:
$ cat ex1_1.c
#include <stdio.h>
/* a program to print Have a good day on the screen */
int main(void)
{
printf("Have a good day.\n");
/* message printed */
return 0;
}

$ gcc ansi ex1_1.c -o ex1_1


$ ./ex1_1
Have a good day.
$
22
Example 1.2:
$ cat ex1_2.c
#include <stdio.h>
#include <math.h>
/* a program to print the square root of 2.0 on the screen */
int main(void)
{
printf("The square root of 2 is %f\n", sqrt(2.0));
/* message printed */
return 0;
}
$ gcc ansi ex1_2.c -o ex1_2 -lm
$ ./ex1_2
The square root of 2 is 1.414214
$
23
Appendix: Operating Systems and
Unix
An operating system is a collection of
programs that coordinates the operation
of computer hardware and software.
The Unix operating system is one of the
most popular operating systems. All the
popular workstations have their versions
of Unix system:
On the Sun or Sparc workstations : SunOS,
Solaris
On Hewlett-Packard workstations: HP-UX
On DEC workstations : ULTRIX, OSF/1
On IBM workstations : AIX
24
The Unix File System
A file system is a collection of files.
A file is usually stored on a disk.
Files can be grouped into directories and
organized into a hierarchical directory
structure.
The top of this hierarchical directory structure
is called the root directory.
The Home directory is the directory assigned
to you.
The path name of a file enables you to
identifies a file uniquely to the Unix file
system.
25
Working with Files
To list the files in a directory:
$ ls
To examine the contents of a file:
(1)
$ cat file1
(2)
$ more file1

26
To make a copy of a file:
$ cp file1 file1.backup
To rename a file:
$ mv file1 newFile1
To remove a file:
$ rm rubbish

27
Working with Directories
To display your working directory:
$ pwd
To create a directory:
$ mkdir hw1
To change directory:
$ cd hw1
$ cd /home/s2015/s20155678/hw2

28
Change to upper level directory
$ cd ..
To copy a file from one directory to
another:
$ cp program.c hw1
To move a file from one directory to
another:
$ mv program.c hw1
To remove a directory:
$ rmdir dir1
29