1 agenda administration background our first c program working environment exercise memory and...

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

Upload: derek-woods

Post on 12-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

1

Agenda

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

Page 2: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

2

Administration Teaching assistant: Assaf Zaritsky e-mail:[email protected] 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

Page 3: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

3

Grades

Assignments: 20% Exam: 80%

Page 4: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

4

Web Site

Contact information Announcements All relevant material (homework,

solutions, code examples, slides, etc…)

Page 5: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 6: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 7: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

7

Agenda

Administration Background Our first C program Working environment Memory and Variables

Page 8: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

8

Basic Computer Model

CPU (central processing unit)Input Output

Memory

Mouse, keyboard, hard disk…

Printer, screen, hard disk…

Page 9: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 10: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 11: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 12: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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.

Page 13: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 14: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 15: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

15

Agenda

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

Page 16: 1 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;}

Page 17: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 18: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

18

Agenda

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

Page 19: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

19

Using Microsoft Visual c++

Page 20: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

20

(Free) Visual Studio Express

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

be found here

Page 21: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

21

Agenda

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

Page 22: 1 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

Page 23: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 24: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

24

Agenda

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

Page 25: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

25

Memory

Page 26: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 27: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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

Page 28: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

28

Declaring Variables

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

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

Page 29: 1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables

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