Programming The Development Environment and Your First C Program.

38
Programming The Development Environment and Your First C Program
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of Programming The Development Environment and Your First C Program.

Programming

The Development Environmentand Your First C Program

Technical Details Teaching Assistants

Mati Shomrat [email protected] By appointment only SE Building, 209

Naama Mayer [email protected] Sun. 15:00-16:00, by appointment only SE Building, 009

Efrat Mashiach [email protected] Wed. 12-13, by appointment only Schreiber Building, 010

Asaf Himan [email protected] Wed. 12-13, by appointment only SE Building, 009

Homework

Weekly homework assignments (published online)

Assignments are to be done individually! Submit a hardcopy Follow the submission guidelines Makes for 20% of the final grade Must submit at least 10 assignments Grade based on the best 10.

The Labs

Computer labs hours:Weekdays 8:00 – 20:00 Friday 8:00 – 12:00

If you prefer to work at home, you can use Visual C++ 2008 Express Edition (see instruction on web page)

Basic Computer Model

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

Giving Meaning to Numbers

Instructions Addresses Data:

Integer numbersReal numbersText ...

A Computer Program

A sequence of instructions designed to achieve a specific purpose.

The instructions are executed sequentially. Each instruction has a numerical code.

Types of Instructions

Load data (from an address in the memory) Store data (to an address) Add two numbers Compare two numbers 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.

Programming 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

Programming Languages Hierarchy

Actually, binary instructions.

Why Different Languages?

Many languages were developed with specific applications in mind:Data processingWeb applicationsMathematical calculationsArtificial intelligence

The Compiler

A special program that translates from high-level programming language to machine language.

In class we will work with Microsoft’s compiler

Creating an Executable

Compile Link

sourcecode

objectfile

.exe

Error Error

Link

Error

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

The Development Environment

Visual C++ Integrated Development Environment (IDE)

Text Editor Compiler Linker Debugger

The C Programming Language

Syntax Which instructions are available How to structure a program out of these

instructions Semantics

What is the meaning of the instructions

Our First C Program

/* HelloWorld – An example program */

#include <stdio.h>

void main(){ printf("Hello, world!\n");}

Our First C Program

/* HelloWorld – An example program */

#include <stdio.h>

void main(){ printf("Hello, world!\n");}

Comment

A sequence of characters enclosed between /* and */.May span more than a single line./* This is a multi-line comment */

Used to explain the code to humans and convey more information. See the submission guidelines for example.

Ignored by the compiler

Our First C Program

/* HelloWorld – An example program */

#include <stdio.h>

void main(){ printf("Hello, world!\n");}

A C statement

Print text on the screen.The text is enclosed in “ ”

Our First C Program

/* HelloWorld – An example program */

#include <stdio.h>

void main(){ printf("Hello, world!\n");}

{ }

A block of statements. A number of statements that are to be executed sequentially.

{ statement1

statement2

... statementn

}

In this example our block contains a single statement – the call to the printf command.

Our First C Program

/* HelloWorld – An example program */

#include <stdio.h>

void main(){ printf("Hello, world!\n");}

Define a block of commands called main.

main is a special block – it is where the program starts running.

Every C program must have a single main block

Our First C Program

/* HelloWorld – An example program */

#include <stdio.h>

void main(){ printf("Hello, world!\n");}

Include directive

Inform the compiler we will use commands that interact with standard Input/Output (IO) devices.

We need this instruction because we are using printf in our program.

Our First C Program

/* HelloWorld – An example program */

#include <stdio.h>

void main(){ printf("Hello, world!\n");}

Semicolon ;

For now, all statements end with a semicolon

printf command

Write formatted strings to the console Escape sequences:

\n – new line "first line\nsecond line"

\\ – a single \ "C:\\temp"

\" – a single " "someone : \"something\""

In-class Assignment

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

Steps:

1. Create a new project

2. Add a file to your project

3. Write your program

4. Compile.If there are errors, fix them.

5. Run

Understanding Errors

Error:error LNK2019: unresolved external symbol _WinMain@16 referenced in function _WinMainCRTStartup

Problem:You did not define your project as a console application

Solution:Start over, create a new project, this time remember to define it as a console application

Understanding Errors

Error:

fatal error C1083: Cannot open include file: 'stdioh': No such file or directory

Problem:

The file you’re trying to include does not exist

Solution:

Make sure the file name is spelled correctly

Understanding Errors

Error:warning C4013: 'print' undefined; assuming extern returning int

Problem:The command you’re trying to use does not exist

Solution:Make sure you spelled the command’s name correctly (upper/lower case, omitting letters etc.)

Understanding Errors

Error:error C2146: syntax error : missing ';' before identifier 'print'

Problem:Hmm ..., missing ‘;’?

Solution:What are you waiting for, add it!

The Debugger

Let’s you step through your program Examine what each operation does Find errors in your program

Breakpoints

Break at a certain point in the programExamine this point in more detail

Setting a breakpoint Bring the cursor to desired line right-click and select "Insert/Remove

Breakpoint" or press the F9 key.

Running in debug mode

Build->Start Debug->Go (or F5)

Runs until the first breakpoint

Stepping

Execute a single command at a time Use F10 The yellow arrow indicates the current

position in the program.

Exercise

Write a program that print the text of the hello world program (Note: it does not print ‘Hello world’ but the code for the program)

How to test your program? Run the program and redirect its output to hello.c Create a new project Add hello.c to it by right-click the source files folder

and choosing ‘Add’. Locate the hello.c file and click OK.

Compile and run your program. Now the output should be Hello World”.