1 Agenda Administration Background Our first C program Working environment Exercise Memory and...

Post on 12-Jan-2016

216 views 2 download

Transcript of 1 Agenda Administration Background Our first C program Working environment Exercise Memory and...

1

Agenda

Administration Background Our first C program Working environment Exercise Memory and Variables

2

Administration Teaching assistant: Assaf Zaritsky e-mail:assafzar@post.tau.ac.il Course’s home page: http://

www.cs.tau.ac.il/~assafzar Office hours: Tuesdays, 15:10-16:10,

Shenkar-Physics Building, room 406, or scheduled via email/phone

Office phone: 03-6409759

3

Grades

Assignments: 20% Exam: 80%

4

Web Site

Contact information Announcements All relevant material (homework,

solutions, code examples, slides, etc…)

5

Homework

Weekly homework assignments Programming assignment Each assignment is due in one week 20% of final grade Computer lab 06, open: 8:00 –

20:00, use email/disk-on-key See submission guidelines for details

6

Submission Guidelines Submission in singles! Hard-copy submission during practice or to

my mail box (252) in the Schreiber building (second floor, in front of the elevator)

Include example execution output Do not forget your ID Extensions Should work on Microsoft Dev Studio Pay careful attention to the guidelines

7

Agenda

Administration Background Our first C program Working environment Memory and Variables

8

Basic Computer Model

CPU (central processing unit)Input Output

Memory

Mouse, keyboard, hard disk…

Printer, screen, hard disk…

9

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

10

Machine Language Computers understand only 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 “translated” into machine language

11

Computer Languages

Assembly – machine language with some text codes (still inconvenient).

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

machine language before execution

12

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.

13

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

14

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

15

Agenda

Administration Background Our first C program Working environment Exercise Memory and Variables

16

C Program Template

#include <stdio.h>int main(){ // Program’s “body” return 0;}

17

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(){ printf(“Hello, world!\n”); return 0;}

18

Agenda

Administration Background Our first C program Working environment Exercise Memory and Variables

19

Using Microsoft Visual c++

20

(Free) Visual Studio Express

Free work environment Can be used from home Download and usage details can

be found here

21

Agenda

Administration Background Our first C program Working environment Exercise Memory and Variables

22

Exercise

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

23

A name printing program

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

#include <stdio.h>int main(){ printf(“Assaf\nZaritsky\n”); return 0;}

24

Agenda

Administration Background Our first C program Working environment Exercise Memory and Variables

25

Memory

26

Memory (Cont.)

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

27

What are variables? A named area in the computer memory,

intended to contain values of a certain kind (integers, real numbers, etc.)

Contain the data your program works with

Can be used to store data to be used elsewhere in the program

In short – they are the only way to manipulate data

28

Declaring Variables

int num; int num1, num2; num = -1; // assignment int x = 9; // declaration +

assignment int y = x + num; // y equals 8

29

Declaring Variables in C

Before using a variable, one must declare it

The declaration first introduces the variable type, then its name

When a variable is declared, its value is undefined