tirgul1

advertisement
Agenda






Administration
Background
Our first C program
Working environment
Exercise
Memory and Variables
1
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
2
Grades


Assignments: 20%
Exam: 80%
3
Web Site



Contact information
Announcements
All relevant material (homework,
solutions, code examples, slides, etc…)
4
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
5
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
6
Agenda

Administration

Background



Our first C program
Working environment
Memory and Variables
7
Basic Computer Model
Input
Mouse,
keyboard,
hard disk…
CPU (central
processing unit)
Output
Printer,
screen, hard
disk…
Memory
8
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
9
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
10
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
11
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.
12
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
13
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
14
Agenda

Administration
Background

Our first C program




Working environment
Exercise
Memory and Variables
15
C Program Template
#include <stdio.h>
int main()
{
// Program’s “body”
return 0;
}
16
Our first C Program
/* HelloWorld – An example program */
#include <stdio.h>
int main()
{
printf(“Hello, world!\n”);
return 0;
}
Thistells
is aanCthe
instruction
towe
theare
compiler
the
This
Note
Yet
another
isthat
all
statement.
Ccompiler
C statement.
statements
This
end
one
withto
terminates
a insert
semicolon
the
(;).
This
about
to
define
awith
function
This
is
a
comment
–
starts
with
a
/*
and
ends
a */.
Curly
braces
indicate
the
beginning
and
end
of
a
contents
of
the
file
stdio.h
to
the
program
prior
to
This
program
statement
and
informs
calls a
the
function
operating
called
system
printf,that
which
it
has
causes
named
main.
Comments
are
used
to
explain
the
program
to
a
human
compilation.
block
of
instructions.
text
ended
to
successfully.
be
printed
on the–by
screen.
main
is
a
special
function
it the
is where
the program
reader,
and
are
ignored
compiler.
Specifically
in this
case – a function.
This file
contains
information
about the printf fuction.
starts
running.
17
Agenda

Administration
Background
Our first C program

Working environment




Exercise
Memory and Variables
18
Using Microsoft
Visual c++
19
(Free) Visual Studio Express



Free work environment
Can be used from home
Download and usage details can be
found here
20
Agenda

Administration
Background
Our first C program
Working environment

Exercise

Memory and Variables



21
Exercise
Write, compile and run a program that
prints your first name in one line,
and your second name in another
22
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;
}
23
Agenda

Administration
Background
Our first C program
Working environment
Exercise

Memory and Variables




24
Memory
25
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
26
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
27
Declaring Variables





int num;
int num1, num2;
num = -1; // assignment
int x = 9; // declaration + assignment
int y = x + num; // y equals 8
28
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
29
Download