c programming for engineers teaching assistant: ben sandbank...

25
C Programming for engineers Teaching assistant: Ben Sandbank e-mail:[email protected] Home page: http:// www.cs.tau.ac.il /~sandban Office hours: Wednesday, 16:00- 17:00, room 19 in the basement of the Schreiber building Office phone: 03-6405378

Upload: gary-anthony

Post on 17-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

C Programming for engineers Teaching assistant: Ben Sandbank e-mail:[email protected] Home page: http://www.cs.tau.ac.il

/~sandban Office hours: Wednesday, 16:00-

17:00, room 19 in the basement of the Schreiber building

Office phone: 03-6405378

Page 2: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

C Programming for engineers

Teacher: Amitai Armon Course home page: To be

announced

Page 3: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Recommended books The C Programming Language

(second edition - ANSI C), Brian W. Kernighan and Dennis M. Ritchie, Prentice Hall, Inc., 1988.

A Book on C (third or fourth edition), Al Kelley and Ira Pohl, Addison-Wesley, 1997.

These and other C programming books can be found in the library under 519.83

Page 4: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Homework

Weekly homework assignments Each assignment is due in one week The assignments are worth 20% of

the final grade. The rest – a written exam.

See submission guidelines for details.

Page 5: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Homework (cont.) Each assignment – programming

problems. Submission in singles. The assignments will be handed in in

hard-copy to me during practice or in my box in the Schreiber building (second floor, in front of the elevator).

Pay careful attention to the guidelines.

Page 6: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

What can a computer do?

Strictly speaking, very little - Store and retrieve numbers

very quickly very accurately

Add, subtract, multiply, and divide also fast and accurate

Compare numbers (with 0) Follow a list of instructions

jump around in the list

Page 7: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

What about everything else? More complex math

Combination of atomic operations Interaction with peripheral devices

Output: graphics cards and printers Input: keyboards, mice, joysticks All sorts of specialized devices

Everything done using numbers To the computer everything is numbers

Page 8: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

What numbers does a computer work with?

Instructions. Addresses. Data:

Integer numbers. Real numbers. Text. All sorts of other things.

Page 9: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Data representation inside the computer Bit – a binary digit: 0 or 1.

Bits are always grouped! Byte – a group of 8 bits.

Therefore, a byte can represent up to 28=256 values. The values can range from 0 to 255 or from -128 to

127. The fundamental data unit of a computer.

Word – a group of (usually) 4 (or 8) bytes. 4 bytes = 32 bits. Value range: 0 to 232 -1 (4,294,967,295 ). Or, more often: -231 to 231-1 (-2,147,483,648 to

+2,147,483,647).

Page 10: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Inside the computer

The computer memory is composed of a long list of bits.

Bits are grouped into bytes and words.

Every byte is numbered sequentially.

This number is called an address.

Page 11: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Basic computer model

Page 12: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

What is a computer program? A sequence of processor

instructions designed to achieve a specific purpose

The instructions are executed sequentially. No instruction is executed before the previous has been completed.

Each instruction has a numerical code.

Page 13: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Examples of instructions

Load data (from an address in the memory).

Store data (in an address). Add two numbers. If two numbers are equal, jump to

another part of the program. Instructions are numbers!

Page 14: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Machine language Computers understand only machine

language. Every processor has its own machine

language. Basically looks like a sequence of 1’s and 0’s. Very inconvenient to work with and non

intuitive. All other computer languages were

created for human convenience The computer does not understand C. Must be converted into machine language.

Page 15: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Computer languages (getting closer to human languages) Assembly – machine language with

some text codes (still inconvenient). Interpreted languages – Java, Perl.

The program is translated into machine language line by line during execution.

Compiled languages – C, Pascal, Fortran. The program is translated into machine

language before execution

Page 16: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

High level languages vs. machine languages.

Actually, binary instructions.

Page 17: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

C is a procedural language

It enables the user to create new instructions (procedures) from existing ones.

Instead of re-writing the same code over and over again, write it once and call it when needed.

Page 18: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Why different languages?

Many languages were developed with specific applications in mind: Data processing. Web applications. Mathematical calculations. Artificial intelligence.

Page 19: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

How do we compile?

A special program – the “compiler” – “translates” from computer language to machine language.

There are many compilers on the market.

We will work with Microsoft Visual C++ 6.0.

Page 20: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Let's look at the whole process

Write a program Your favorite text editor

Compile + link the program C compiler will do one of two things:

print error messages and abort (most probably…)

produce an executable program

Run the program

Page 21: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

This is a comment – starts with a /* and ends with a */.Comments are used to explain the program to a human reader, and are ignored by the compiler.

Curly braces indicate the beginning and end of a block of instructions. Specifically in this case – a function.

This is an instruction to the compiler to insert the contents of the file stdio.h to the program prior to compilation.

This file contains information about the printf fuction.

Yet another C statement. This one terminates the program and informs the operating system that it has ended successfully.

This tells the compiler we are about to define a function named main.main is a special function – it is where the program starts running.

This is a C statement. This statement calls a function called printf, which causes text to be printed on the screen.

Note that all C statements end with a semicolon (;).

Our first C program/* HelloWorld – An example program */

#include <stdio.h>int main(void){ printf(“Hello, world!\n”); return 0;}

Page 22: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Now, let’s get to work!!!Help using Microsoft visual c++

Page 23: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

Exercise

Write, compile and run a program that prints your first name in one line, and your second name in another.

Page 24: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

A name printing program

/* This program prints my name on the screen in two lines. */

#include <stdio.h>int main(void){ printf(“Ben\nSandbank!\n”); return 0;}

Page 25: C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.ilsandban@post.tau.ac.il Home page: sandban

C programming for Engineers

LCC compiler – a free c compiler available on the web.

http://www.cs.virginia.edu/~lcc-win32/

Some instructions