Title: ENG236: C Programming Environment (2)
1ENG236 C Programming Environment (2)
THE HONG KONG POLYTECHNIC UNIVERSITY
Rocky K. C. Chang
2Computer Programming and Basic Software
Engineering
2. A Taste of C
Programming Environment
- Before doing any C programming, first we need
to familiarize with the programming environment. - A programming environment is a software package
that can help us develop and test program codes. - For PC, one of the most popular programming
environments is Microsoft Visual Studio. - It is more popular because of its direct support
to the Microsoft Windows environment.
3Computer Programming and Basic Software
Engineering
2. A Taste of C
Visual Studio .NET
- Visual C is one of the components of the
umbrella product Visual Studio, which also
contains development tools for other programming
languages, e.g. Visual Basic, Visual J, etc. - Visual Studio has been for sale for more than 10
years. - The latest version is Visual Studio .NET. The C
compiler that is incorporated is Visual C .NET. - Additional features include
- Produce managed codes that run in the .NET
runtime - Allow the use of .NET class library.
Second semester
4Computer Programming and Basic Software
Engineering
2. A Taste of C
Visual Studio Solution and Project
- In Visual Studio, each user task is referred to
as a solution to a problem. - A solution refers to the computer resource (such
as memory, disk space, etc.) required for one or
more projects to complete the task. - Every project contains
- executable code and debug information
- files containing the C programs
- classes and objects used in the project
- other related resources such as
- icon images used in the project
- graphical user interface (GUI), e.g. button, menu.
They are all in files
5Computer Programming and Basic Software
Engineering
2. A Taste of C
Files
Solution
Project 1
- A Solution must contain at least one Project
- A Project must contain at least one File
Project 2
Project 3
6Computer Programming and Basic Software
Engineering
2. A Taste of C
Display resources of the solution, such as,
projects and files inside
Source Program Editing
Display compilation results
7Computer Programming and Basic Software
Engineering
2. A Taste of C
Visual Studio - Project Example
- Solution Explorer allows us to clearly see the
different files included in the project
8Computer Programming and Basic Software
Engineering
2. A Taste of C
Visual Studio - Project Example
Program can be typed here
Built result (result of compiling and linking)
shown here
9Computer Programming and Basic Software
Engineering
2. A Taste of C
Build your first project - HelloWorld
- Building a project means to produce target
executable files (.exe files) based on the
program codes. - Project can be built to execute in console or
windows mode - Console mode Use the Command Prompt for
in/output - Windows mode Use the Windows environment for
program execution.
Second semester
10Computer Programming and Basic Software
Engineering
2. A Taste of C
- Command Prompt
- It is a traditional interface between the user
and computer - User types in commands to instruct computer to
work.
11Computer Programming and Basic Software
Engineering
2. A Taste of C
Build your first project - HelloWorld
- Hello.cpp File containing a C program that
prints a message Hello World! on the console
output.
include ltiostreamgt int main() stdcout ltlt
"Hello World!" ltlt stdendl return 0
The returned type of main() must be declared in
Visual Studio .NET 2005
12Computer Programming and Basic Software
Engineering
2. A Taste of C
Step 1
Start the Visual Studio .NET 2005 Click New
Project
13Computer Programming and Basic Software
Engineering
2. A Taste of C
Step 2
Note where you create this project
You can modify the location of the files of your
project.
14Computer Programming and Basic Software
Engineering
2. A Taste of C
Step 3
15Computer Programming and Basic Software
Engineering
2. A Taste of C
Step 4
Right-click Source Files, select Add ? New Item
16Computer Programming and Basic Software
Engineering
2. A Taste of C
Step 5
Choose C File (.cpp). Give a name to the C
file you are going to add
17Computer Programming and Basic Software
Engineering
2. A Taste of C
Step 6
Type the program on p.11 here.
Click Build ? Build Solution See the built result
18Computer Programming and Basic Software
Engineering
2. A Taste of C
Step 7
Execute the program by clicking Debug ? Start
Without Debugging
Do you see the following result? Note The
result is shown in the Command Prompt Console
Application
19Computer Programming and Basic Software
Engineering
2. A Taste of C
What has actually done?
C File Hello.cpp
- In the HelloWorld example, one solution is
created that contains a project called chap2p1. - Inside the project, there is one C program
called Hello.cpp.
Solution
Project chap2p1
20Computer Programming and Basic Software
Engineering
2. A Taste of C
- The C Compiler of Visual Studio compiles the
program Hello.cpp and generates an executable
file chap2p1.exe. - When you click Debug ? Start Without Debugging,
Visual Studio actually runs the executable file
chap2p1.exe for you. - All files generated in this project is stored in
- C\Documents and Settings\Administrator\My
Documents\Visual Studio 2005\Projects\chap2p1
Run Windows Explorer
21Computer Programming and Basic Software
Engineering
2. A Taste of C
Contains all files created after building the
solution
Contains info. about the project. Double-click
this will automatically start the project
The C file you created
22Computer Programming and Basic Software
Engineering
2. A Taste of C
The EXE (executable) file created
23Computer Programming and Basic Software
Engineering
2. A Taste of C
We can always run the program under the command
prompt
24Computer Programming and Basic Software
Engineering
Exercise 2.1
2. A Taste of C
Consider the two programs on the RHS. For each of
the program, construct a new project in Visual
Studio and then build the solution. For each
program, note the error messages when building
the project. Fix the errors and execute the
resulting project. What do you see?
include ltiostreamgt int main() cout ltlt "Hi
World!" ltlt stdendl return
0
include ltiostreamgt int test() stdcout ltlt
"How are you!" ltlt stdendl return 0
25Computer Programming and Basic Software
Engineering
Exercise 2.2
2. A Taste of C
- 1. Open the Command Prompt.
- By using the command cd, change the current
folder to the folder that contains chap2p1.exe
(you may want to use the command dir to show the
content of a folder.) - Run chap2p1.exe by typing its file name and press
Enter. - Compare the result with that you run the program
in Visual Studio. Can you see any difference
coming up? - If no, add the following lines of code before
return 0 of Hello.cpp. - stdcoutltlt"Press Enter to continue."ltltstden
dl - stdcin.get()
- Rebuild the solution. Repeat 1 to 4 and note
any difference.
26Acknowledgment
- The slides are based on the set developed by Dr.
Frank Leung (EIE).