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

Post on 17-Dec-2015

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

C Programming for engineers Teaching assistant: Ben Sandbank e-mail:sandban@post.tau.ac.il 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

C Programming for engineers

Teacher: Amitai Armon Course home page: To be

announced

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

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.

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.

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

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

What numbers does a computer work with?

Instructions. Addresses. Data:

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

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).

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.

Basic computer model

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.

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!

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.

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

High level languages vs. machine languages.

Actually, binary instructions.

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.

Why different languages?

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

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.

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

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;}

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

Exercise

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

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;}

C programming for Engineers

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

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

Some instructions

top related