Vous êtes sur la page 1sur 13

C Programming Lab

Universiti Tenaga Nasional



Prepared by MAF 2009 1

1 Starting Visual Studio 2005


Creating a new Project

1. From START menu, select:
Programs -> Microsoft Visual Studio 2005 -> Microsoft Visual Studio 2005

2. You are going to see this page (refer Figure 1).

Figure 1

3. Go to Create: ->Click Project.

4. New Project window will pop-out (refer Figure 2).
Create
Project
C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 2

Figure 2

5. Choose Win32 for Project types: (refer Figure 2-(a)). For Templates: choose Win32
Console Application (refer Figure 2- (b)). In Name: text box, type MyProject (refer
Figure 2- (c)). Choose the location where you want to save your file it is advisable for
you to save your file in My Document or Desktop for easy access later by browsing the
location. Click OK.

6. In the next window, click Next (refer Figure 3).

Figure 3

7. Tick Empty Project for the Additional options: (refer Figure 4). Then click Finish.
(a)
(b)
(c)
(d)
(e)
C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 3

Figure 4

8. Now you have a project called MyProject.


Writing a File

1. After you create a project, you need to create a source file in order to write your C
program.
2. In the Solution Explorer, right-click on Source File->Add->New Item (refer Figure 5).


Figure 5

Tick here
C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 4
3. Add New Item Window will appear (refer Figure 6). Click on C++ File (.cpp) refer
Figure 6 (a). In the Name: textbox, type Lab1 (refer Figure 6 (b)). DO NOT change the
location of the file because automatically, the file will be saved in the same directory as
the Project (in your case it is MyProject)

Figure 6

4. You will see 3 windows Files and Classes Information, Editor and Output. Files and
Classes Information window will tell you the structure of you project. Editor window is
the place where you put/type your codes. Message window will tell you the message
from the compilation and building process. Refer Figure 7.


Figure 7
Editor
Files and
Classes
Information
Message Window: Compilation & Error Message
(a)
(b)
C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 5
5. You are now ready to write your C program. You can copy and paste the program below
to you Editor window.












6. Your screen should look something like this (Figure 8):


Figure 8

7. To compile the program, from the menu bar (Figure 9), select Build->Compile.
Alternatively, you can press Ctrl+F7 to compile the program.
#include <stdio.h>

int main(void)
{
/* printing out greeting */
printf("Hello everybody!\n");
printf("This is my first C program file. \n");

return 0;
}
C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 6

Figure 9

8. Your Message window will tell the output of the compilation process. For this example
you will see that you have 0 errors and 0 warning (refer Figure 10).


Figure 10

9. Since you do not have any error you can link the program by building it. To link the
program, from the menu bar (Figure 11), select Build->Build Solution. Alternatively, you
can press F7 to link the program.

C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 7

Figure 11

10. Your Message window will tell the output of the linking process. For this example you
will see that you have 0 errors and 0 warning (refer Figure 12).


Figure 12

C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 8
11. To run (execute) the program, from the menu bar, select Debug->Start Without
Debugging. Alternatively, you can press Ctrl+F5 (refer Figure 13). Try running the above
program and you should have produce something like the following Output window
(Figure 14)


Figure 13


Figure 14

12. As you can see, only lines that start with function printf produces output in the Output
window. Therefore, whatever text that you want to appear in the Output window, you
should use printf function.

Dealing with Compilation Error (syntax error)

1. Now, please modify the code to be similar to the following:

C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 9


2. Compile the program (you should already know how to do this..)

3. You will notice that you have error message in your message window like the following
(Figure 15):


Figure 15
4. Double click anywhere on the first error line.

5. An arrow will appear on the line where the error is located (refer Figure 16).
#include <stdio.h>

int main(void)
{
printf(Hello everybody!\n);
printf(This is my first C program file.\n);
}

printf(Hello everybody!\n);

- notice that you should remove
the symbol after the \n.
Number of errors in the
file
First error
C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 10

Figure 16

6. Now correct the error and recompile your program.


Attaching / Detaching a file from the workspace

There will be times when you need to create many files during your lab hours. It will be a
waste of time if you need to create a project for every one of your files. Instead, it is possible
to create one project, and you attach or detach a file to the project depending on the needs.


Arrow indicating the
location of the error
C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 11
Detaching a file

Right-click (highlight) the file that you want to detach (in this case, it should be your
Lab1.cpp). Choose Remove (Figure 17). If you clicked the Remove button (Figure 18), you
are not erasing and trashing all the data in the file. Instead, you are removing that particular
.cpp file from the project. Else if you clicked Delete button, you are going to delete the file
permanently. Therefore always be careful.


Figure 17

Figure 18

Attaching a file

Right-click on the Project name, in your case, it is MyProject folder, then choose Add>
Existing Item (Figure 19)

C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 12

Figure 19

A pop up window appear asking you to select the file(s) that you want to attach to the
workspace. Select the file (in your case it is Lab1.cpp), then press the OK button (Figure 20).
The file will be put under Source Files. Click on the + sign, and then double click on Lab1.cpp
to view the file.


Figure 20
C Programming Lab
Universiti Tenaga Nasional

Prepared by MAF 2009 13
Exercises.

1. Without closing the workspace, detach Lab1.cpp

2. Create a NEW source file. Save it as lab1a.c

3. Type the following program


4. Save your program.

5. Compile the program. After that, check what are the files listed in the directory (in
your case it should be a folder called MyProject located at either MyDocument or
Desktop (depends on where you save your project). Write them down.

6. Build the executable file. After that, check what are the files listed in the directory
now. Write them down.

7. Run the program. What is the output?

8. Go back to the program and replace the line printf (****Welcome to my C lab
session \n****); with the following lines

printf (****Welcome );
printf (to my C lab session \n****);

then repeat step 2 until 5. What is the output now?
#include <stdio.h>

int main()
{
printf ("****Welcome to my C lab session \n****");

return 0;
}

Vous aimerez peut-être aussi