Programming - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Programming

Description:

Naama Mayer naamamay_at_gmail.com. Sun. 15:00-16:00, by appointment ... Efrat Mashiach efratmas_at_gmail.com. Wed. 12-13, by appointment only. Schreiber Building, 010 ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 39
Provided by: csta3
Category:
Tags: com | gmail | programming

less

Transcript and Presenter's Notes

Title: Programming


1
Programming
  • The Development Environmentand Your First C
    Program

2
Technical Details
  • Teaching Assistants
  • Mati Shomrat matis_at_post.tau.ac.il
  • By appointment only
  • SE Building, 209
  • Naama Mayer naamamay_at_gmail.com
  • Sun. 1500-1600, by appointment only
  • SE Building, 009
  • Efrat Mashiach efratmas_at_gmail.com
  • Wed. 12-13, by appointment only
  • Schreiber Building, 010
  • Asaf Himan asafloz_at_gmail.com
  • Wed. 12-13, by appointment only
  • SE Building, 009

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

4
The Labs
  • Computer labs hours
  • Weekdays 800 2000
  • Friday 800 1200
  • If you prefer to work at home, you can use Visual
    C 2008 Express Edition (see instruction on web
    page)

5
Basic Computer Model
6
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

7
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

8
Giving Meaning to Numbers
  • Instructions
  • Addresses
  • Data
  • Integer numbers
  • Real numbers
  • Text
  • ...

9
A Computer Program
  • A sequence of instructions designed to achieve a
    specific purpose.
  • The instructions are executed sequentially.
  • Each instruction has a numerical code.

10
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!

11
Machine Language
  • Computers understand only machine language.
  • Every processor has its own machine language.
  • Basically looks like a sequence of 1s and 0s.
  • 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.

12
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

13
Programming Languages Hierarchy
Actually, binary instructions.
14
Why Different Languages?
  • Many languages were developed with specific
    applications in mind
  • Data processing
  • Web applications
  • Mathematical calculations
  • Artificial intelligence

15
The Compiler
  • A special program that translates from high-level
    programming language to machine language.
  • In class we will work with Microsofts compiler

16
Creating an Executable
objectfile
sourcecode
Link
Link
Compile
.exe
Error
Error
Error
17
Compilation More Details
Machine language with holes
Compilation
Executable
Source1.c
Source1.obj
Linker
Machine language with holes
myprog.exe
Compilation
Source2.c
Source2.obj
18
The Development Environment
  • Visual C
  • Integrated Development Environment (IDE)
  • Text Editor
  • Compiler
  • Linker
  • Debugger

19
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

20
Our First C Program
  • / HelloWorld An example program /
  • include ltstdio.hgt
  • void main()
  • printf("Hello, world!\n")

21
Our First C Program
  • / HelloWorld An example program /
  • include ltstdio.hgt
  • 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
22
Our First C Program
A C statement Print text on the screen.The text
is enclosed in
  • / HelloWorld An example program /
  • include ltstdio.hgt
  • void main()
  • printf("Hello, world!\n")

23
Our First C Program
  • / HelloWorld An example program /
  • include ltstdio.hgt
  • 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.
24
Our First C Program
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
  • / HelloWorld An example program /
  • include ltstdio.hgt
  • void main()
  • printf("Hello, world!\n")

25
Our First C Program
  • / HelloWorld An example program /
  • include ltstdio.hgt
  • 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.
26
Our First C Program
Semicolon For now, all statements end with a
semicolon
  • / HelloWorld An example program /
  • include ltstdio.hgt
  • void main()
  • printf("Hello, world!\n")

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

28
In-class Assignment
  1. Write, compile and run the Hello World program.

29
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

30
Understanding Errors
  • Error
  • error LNK2019 unresolved external symbol
    _WinMain_at_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

31
Understanding Errors
  • Error
  • fatal error C1083 Cannot open include file
    'stdioh' No such file or directory
  • Problem
  • The file youre trying to include does not exist
  • Solution
  • Make sure the file name is spelled correctly

32
Understanding Errors
  • Error
  • warning C4013 'print' undefined assuming
    extern returning int
  • Problem
  • The command youre trying to use does not exist
  • Solution
  • Make sure you spelled the commands name
    correctly (upper/lower case, omitting letters
    etc.)

33
Understanding Errors
  • Error
  • error C2146 syntax error missing '' before
    identifier 'print'
  • Problem
  • Hmm ..., missing ?
  • Solution
  • What are you waiting for, add it!

34
The Debugger
  • Lets you step through your program
  • Examine what each operation does
  • Find errors in your program

35
Breakpoints
  • Break at a certain point in the program
  • Examine 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.

36
Running in debug mode
  • Build-gtStart Debug-gtGo (or F5)
  • Runs until the first breakpoint

37
Stepping
  • Execute a single command at a time
  • Use F10
  • The yellow arrow indicates the current position
    in the program.

38
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.
Write a Comment
User Comments (0)
About PowerShow.com