Top Banner
Programming The Development Environment and Your First C Program
38

Programming The Development Environment and Your First C Program.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming The Development Environment and Your First C Program.

Programming

The Development Environmentand Your First C Program

Page 2: Programming The Development Environment and 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

Page 3: Programming The Development Environment and Your First C Program.

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.

Page 4: Programming The Development Environment and Your First C Program.

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)

Page 5: Programming The Development Environment and Your First C Program.

Basic Computer Model

Page 6: Programming The Development Environment and Your First C Program.

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 7: Programming The Development Environment and Your First C Program.

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 8: Programming The Development Environment and Your First C Program.

Giving Meaning to Numbers

Instructions Addresses Data:

Integer numbersReal numbersText ...

Page 9: Programming The Development Environment and Your First C Program.

A Computer Program

A sequence of instructions designed to achieve a specific purpose.

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

Page 10: Programming The Development Environment and Your First C Program.

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!

Page 11: Programming The Development Environment and Your First C Program.

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 12: Programming The Development Environment and Your First C Program.

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

Page 13: Programming The Development Environment and Your First C Program.

Programming Languages Hierarchy

Actually, binary instructions.

Page 14: Programming The Development Environment and Your First C Program.

Why Different Languages?

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

Page 15: Programming The Development Environment and Your First C Program.

The Compiler

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

In class we will work with Microsoft’s compiler

Page 16: Programming The Development Environment and Your First C Program.

Creating an Executable

Compile Link

sourcecode

objectfile

.exe

Error Error

Link

Error

Page 17: Programming The Development Environment and Your First C 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

Page 18: Programming The Development Environment and Your First C Program.

The Development Environment

Visual C++ Integrated Development Environment (IDE)

Text Editor Compiler Linker Debugger

Page 19: Programming The Development Environment and Your First C Program.

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

Page 20: Programming The Development Environment and Your First C Program.

Our First C Program

/* HelloWorld – An example program */

#include <stdio.h>

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

Page 21: Programming The Development Environment and Your First C Program.

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

Page 22: Programming The Development Environment and Your First C Program.

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 “ ”

Page 23: Programming The Development Environment and Your First C Program.

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.

Page 24: Programming The Development Environment and Your First C Program.

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

Page 25: Programming The Development Environment and Your First C Program.

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.

Page 26: Programming The Development Environment and Your First C 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

Page 27: Programming The Development Environment and Your First C Program.

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\""

Page 28: Programming The Development Environment and Your First C Program.

In-class Assignment

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

Page 29: Programming The Development Environment and Your First C 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

Page 30: Programming The Development Environment and Your First C Program.

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

Page 31: Programming The Development Environment and Your First C Program.

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

Page 32: Programming The Development Environment and Your First C Program.

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

Page 33: Programming The Development Environment and Your First C Program.

Understanding Errors

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

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

Solution:What are you waiting for, add it!

Page 34: Programming The Development Environment and Your First C Program.

The Debugger

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

Page 35: Programming The Development Environment and Your First C 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.

Page 36: Programming The Development Environment and Your First C Program.

Running in debug mode

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

Runs until the first breakpoint

Page 37: Programming The Development Environment and Your First C Program.

Stepping

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

position in the program.

Page 38: Programming The Development Environment and Your First C 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”.