Вы находитесь на странице: 1из 20

CS 231: Computer Programming in C Course Introduction

September 7 , 2006 Ms. L. Massawe

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg1

Topics to be covered
Introduction to program development process Data Types Conditions, Logical Expressions, and selection of Control structures Functions Arrays and their applications Pointers Structures, Unions and Enumerated types
7
th

Sept 2006

CS 231/ L.Massawe Lec 1. pg2

Prerequisite: Delivery Mode: Tutorial Groups:


CIT Monday TE Tuesday EE Thursday EPE Thursday ID Friday

CS172 2hrsLecture + 1hrTutorial 5 Tutorial groups


15:00 to 15:55 14:00 to 14:55 14:00 to 14:55 16:00 to 16:55 14:00 to 14:55

Assessment:

2 Test and 1 University exam

1st test at 6th week 20% 2nd test at 13th week 20% UE 60%
CS 231/ L.Massawe Lec 1. pg3

th

Sept 2006

Course materials
Lecture notes and Tutorial questions will be distributed to group representatives at the end of every lecture. During tutorial make sure you have a copy of tutorial questions. Recommended Reference/Textbooks:
1. The C Programming Language by B. W. Kernighan and D. M. Ritchie
2. 3. 4. C/C++ Programming with Borland C/C++ Compiler C for Scientists and Engineers by Richard Johnsonbaugh and Martin Karin. C: Complete reference

There are lot of this course related material on the internet

http://www.le.ac.uk/cc/tutorials/c/
CS 231/ L.Massawe Lec 1. pg4

th

Sept 2006

Lecture 1 Introduction to Programming and Program Development Process

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg5

1.0 Introduction to Programming


Software: refers to computer programs written by the programmer as well as those supplied by the system. Hardware: refer to the machinery that executes the instructions. System software includes text editors, compilers and the operating system. Text editors are used to generate text, e.g. used to write programs and to prepare electronic mail messages. A compiler is used to translate programs such as C, Pascal, and FORTRAN into instructions that can be executed by the CPU. Operating system is a set of programs that manages and allocates the resources of the computer. E.g. If the program contains the command to print data, the operating system service the request by allocating the printer and channeling the data to be output to it. Examples of operating system are MS-DOS, windows, UNIX etc.
7
th

Sept 2006

CS 231/ L.Massawe Lec 1. pg6

Programming Languages
Machine Language

Sequence of instructions encoded as bit strings which are directly executed by a computer hardware. Machine instructions are typically quite primitive e.g. copying data from one storage location to another, adding a value stored in one location to the value stored in another location, comparing two values etc. In machine language everything i.e. instructions, data, variables are represented by binary numbers e.g. 010100110111101011. These programs are difficult to write, read and maintain. In 1940s and 1950s, all programs were written in machine language.
CS 231/ L.Massawe Lec 1. pg7

th

Sept 2006

Programming Languages
Assembler Language
Assembler language is a major improvement over machine language in which bit-string instructions are replaced by symbolic instructions. In assembler language, each instruction is identified by short name rather than numbers, and variables can be identified by names rather than numbers. E.g. the load instruction 01111010 which moves the contents of register R4 to register R1, is replaced by symbolic instruction MV R1, R4 Programs written in assembler language require a special program called assembler, to translate assembly language instruction into machine instruction.

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg8

Programming Languages
High-level languages
Majority of the programs written today are written in languages that more closely resemble human language. These languages are called high-level languages e.g. C, Pascal, FORTRAN, COBOL etc.. Like assembler language, high-level language uses symbolic instructions. High level language allows programmer to use much more complex instructions as a result one high-level instruction may translate into many machine instructions. Programs written in high-level language are easier to read and maintain.

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg9

High-level Languages
Every high-level language requires a compiler to translate instructions in the high-level programming language into low-level instructions which can be executed by a computer. A compiler is similar to an assembler but much more complex There is one-to-one correspondence between assembly language instruction and machine language instruction. E.g. 01111010 corresponds to MV R1, R4 In contrast, a single instruction in high-level language can produce many machine instructions.

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg10

High-level language
E.g. Consider the simple C statement which assigns the value of b plus c minus 2 to a, where a, b, and c are variables.
a=b+c2

In assembly language this could be written:


LOAD b, R0 LOAD c, R1 ADD R0, R1 SUB 2, R1 STORE R1, a

Portability : A single program written in high level language can be executed in different computer by simply re-compiling the program using different compilers.
CS 231/ L.Massawe Lec 1. pg11

th

Sept 2006

High-level language
Readability: Their relative closeness to human languages makes them not only easier to write but also easier to read as well. Maintainability: Because they are more readable, high-level languages are much easier to modify and debug. Efficiency: Writing in a high-level language programmer has little control over how a compiler translates code. Some combination of instructions execute faster than others. However when compiler translates program into machine language they may or may not translate them into the most efficient machine code which result into reduced efficiency.

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg12

Brief History of C
Designed by Dennis Ritchie of Bell Laboratories in 1972. Implemented in PDP-11 in 1973, as a systems programming language for the UNIX operating system. Evolved from B and BCPL BCPL = type-less PL developed by Martin Richards (Camb., 67). B was based on BCPL and was written by Ken Thompson in 1970 for the first UNIX system on a PDP-7. C was formed with low-level programming + structured programming issues in mind. C revised by ANSI(X3J11)to form new standard (1989). Internationalised and became ANSI/ISO standard in 1990. C99 standard revision finalised in 1999.
7
th

Sept 2006

CS 231/ L.Massawe Lec 1. pg13

Applications of C

C has gained popularity in wide-ranging application areas since the days it was used solely for operating systems programming. AI/Database/Graphics applications. Commercial (Business) applications. Embedded/Real-time applications. Engineering/Scientific applications.

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg14

C Essentials
A high-level language consist of one or more files of instructions written in highlevel language called source files. Source files are created using editor in which programmers enter the code. C programs have names which ends with .c or .C for example test.c. The .c or .C are called file extensions. A compiler takes in source file and produce as output a file of machine instruction called object file. Object file has an extension .obj e.g. test.obj Object files are further processed by the linker before it can be executed. Linker takes in several object files and any needed library routine and link them together to produce one executable file with an extension .exe e.g. test.exe.
7
th

Sept 2006

CS 231/ L.Massawe Lec 1. pg15

Program Development
Program development is divided into two major parts:
Problem solving
Define a problem (what are the input and output) Design algorithm to solve a problem Test by hand

Implementation
Coding / writing the algorithm in programming language (Edit the source files) Compile the source files Link object files Test and debug executable program.

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg16

A First C Program

We will use a simple C program example to begin our study of C, and to highlight the following: Function main() #include Functions printf() and scanf()

th

Sept 2006

CS 231/ L.Massawe Lec 1. pg17

The program will print This is my first C program! to the video display. /* This program prints the message This is my first C program!*/ #include<stdio.h> main () { int x; printf(This is my first C program!\n); scanf(%d,&x); }
7
th

Sept 2006

CS 231/ L.Massawe Lec 1. pg18

Comments
/* marks the start of a comment */ marks the end of a comment
Comments are ignored by a compiler

Preprocessor Directive
Request some action before the program is actually translated into machine code i.e. before program is compiled Always begin with a # sign #include preprocessor directive causes the contents of the named file enclosed in <filename> to be inserted precisely where the #include line appears. Such files are called header files with .h extension Header files provides a proper interface or information you need to use runtime routine functions.
To use a runtime routine function you must enter its associated header file in your source file. E.g. printf() is a runtime function which enable you to display data on your terminal however to use it you must include a header file stdio.h in your source file.
CS 231/ L.Massawe Lec 1. pg19

th

Sept 2006

C program consist of one or more functions. A C function is a collection of C language operations. Every executable program must contain a special function called main(), which is where program execution begins. Every function consist of statements enclosed in braces: { }. In our example the program consist of one single function main, made up of one statement:

printf(This is my first C program!\n);


Single statements are terminated by semicolon (;) Except for the special characters such as backslash (\), printf simply copies the characters within the double quotation marks to the video display.
The statement printf(This

is my first C program!\n);

writes This is my first C program!\


\n - newline
CS 231/ L.Massawe Lec 1. pg20

th

Sept 2006

Вам также может понравиться