C Programming course

Preview:

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: sorkine@tau.ac.il - PowerPoint PPT Presentation

Citation preview

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: sorkine@tau.ac.il

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

Office phone: 03-6405360

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

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 The assignments are individual Submit a hardcopy The assignments are worth 20% of the

final grade. The rest – a written exam.

Recitations (= ‘tirgul’)

Review and sometimes enhance the material given at the lectures

Practice Of course, Q&A

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

Basic computer model

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.

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, 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

High level languages vs. machine languages.

Actually, binary instructions.

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.

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.

In class, we will work with Microsoft Visual C++ 6.0 (a.k.a. Visual Studio 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

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

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

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

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.

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

See you next time

Recommended