Download presentation
Presentation is loading. Please wait.
1
Programming The Development Environment and Your First C Program
2
Technical Details Teaching Assistants Mati Shomrat matis@post.tau.ac.ilmatis@post.tau.ac.il By appointment only SE Building, 209 Naama Mayernaamamay@gmail.comnaamamay@gmail.com Sun. 15:00-16:00, by appointment only SE Building, 009 Efrat Mashiachefratmas@gmail.comefratmas@gmail.com Wed. 12-13, by appointment only Schreiber Building, 010 Asaf Himanasafloz@gmail.comasafloz@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 8:00 – 20:00 Friday8:00 – 12:00 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 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.
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 Microsoft’s compiler
16
Creating an Executable Compile Link source code object file.exe Error Link Error
17
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
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 <stdio.h> void main() { printf("Hello, world!\n"); }
21
/* HelloWorld – An example program */ #include 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 /* HelloWorld – An example program */ #include void main() { printf("Hello, world!\n"); } A C statement Print text on the screen. The text is enclosed in “ ”
23
Our First C Program /* HelloWorld – An example program */ #include void main() { printf("Hello, world!\n"); } { } A block of statements. A number of statements that are to be executed sequentially. { statement 1 statement 2... statement n } In this example our block contains a single statement – the call to the printf command.
24
Our First C Program /* HelloWorld – An example program */ #include 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
25
Our First C Program /* HelloWorld – An example program */ #include 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 /* HelloWorld – An example program */ #include void main() { printf("Hello, world!\n"); } Semicolon ; For now, all statements end with a semicolon
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@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 you’re 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 you’re trying to use does not exist Solution: Make sure you spelled the command’s 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 Let’s 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->Start Debug->Go (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”.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.