26
C Programming course Exercise 1

C Programming course

  • Upload
    zareh

  • View
    46

  • Download
    0

Embed Size (px)

DESCRIPTION

C Programming course. Exercise 1. Technical details. Teaching assistant: Olga Sorkine TA Home page: http://www.cs.tau.ac.il/~sorkine /courses/cprog06/ Lectures homepage: http://www.cs.tau.ac.il/~armon/prog06a.htm. Technical details. My e-mail: [email protected] - PowerPoint PPT Presentation

Citation preview

Page 1: C Programming course

C Programming course

Exercise 1

Page 2: C Programming course

Technical details

Teaching assistant: Olga Sorkine

TA Home page: http://www.cs.tau.ac.il/~sorkine/courses/cprog06/

Lectures homepage: http://www.cs.tau.ac.il/~armon/prog06a.htm

Page 3: C Programming course

Technical details

My e-mail: [email protected]

Office hours: Tuesday 11:00 – 12:00Schreiber building, room 002 (Computer Graphics lab)

Office phone: 03-6405360

Page 4: C Programming course

Technical details

We will be working with Visual Studio 6.0 programming environment.

You can practice/do your homework in rooms 1, 9, 10 (open 8:00 – 20:00 weekdays and till 12:00 Fridays)

If you prefer to work at home, you can install lcc (I will explain next time).

Page 5: C Programming course

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 6: C Programming course

Homework

Weekly homework assignments The assignments are individual Submit a hardcopy The assignments are worth 20% of the

final grade. The rest – a written exam.

Page 7: C Programming course

Recitations (= ‘tirgul’)

Review and sometimes enhance the material given at the lectures

Practice Of course, Q&A

Page 8: C Programming course

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 9: C Programming course

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 10: C Programming course

What numbers does a computer work with?

Instructions. Addresses. Data:

Integer numbers Real numbers Text All sorts of other things

Page 11: C Programming course

Basic computer model

Page 12: C Programming course

What is a computer program?

A sequence of processor instructions designed to achieve a specific purpose.

The instructions are executed sequentially.

Each instruction has a numerical code.

Page 13: C Programming course

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 course

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 course

Computer languages (getting closer to human languages)

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

Interpreted languages – Java, Perl, MATLAB The program is translated into machine language line by

line during execution

Compiled languages – C, C++, Pascal, Fortran… The program is translated into machine language before

execution

Page 16: C Programming course

High level languages vs. machine languages.

Actually, binary instructions.

Page 17: C Programming course

C is a procedural language

It enables the user (= the programmer) 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 course

Why different languages?

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

Page 19: C Programming course

How do we compile?

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

There are many compilers on the market.

In class, we will work with Microsoft Visual C++ 6.0 (a.k.a. Visual Studio 6.0)

Page 20: C Programming course

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 course

Compilation – more details

Source1.c

Compilation

Source1.obj

Machine language with “holes”

myprog.exe

Executable

Source2.c

Compilation

Source2.obj

Machine language with “holes”

Linker

Page 22: C Programming course

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 23: C Programming course

Now, let’s get to work!!!Help using Microsoft Visual C++

Page 24: C Programming course

In-class assignments

1. Write, compile and run the “Hello World” program.

2. Slightly modify your program so that it prints your first name in one line, and your second name the next line.

Page 25: C Programming course

A name printing program

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

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

Page 26: C Programming course

See you next time