Lab 1-Getting Started With VC2005

Embed Size (px)

Citation preview

  • 8/11/2019 Lab 1-Getting Started With VC2005

    1/13

    C Programming LabUniversiti Tenaga Nasional

    Prepared by MAF 2009 1

    1 Starting Visual Studio 2005

    Creating a new Project

    1. From STARTmenu, 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).

    CreateProject

  • 8/11/2019 Lab 1-Getting Started With VC2005

    2/13

    C Programming LabUniversiti 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 Documentor Desktopfor easy access later by browsing the

    location. Click OK.

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

    Figure 3

    7. Tick Empty Projectfor the Additional options: (refer Figure 4). Then click Finish.

    (a)

    (b)

    (c)

    (d)

    (e)

  • 8/11/2019 Lab 1-Getting Started With VC2005

    3/13

    C Programming LabUniversiti 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

  • 8/11/2019 Lab 1-Getting Started With VC2005

    4/13

    C Programming LabUniversiti 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 NOTchange 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

    EditorFiles and

    ClassesInformation

    Message Window: Compilation & Error Message

    (a)

    (b)

  • 8/11/2019 Lab 1-Getting Started With VC2005

    5/13

    C Programming LabUniversiti 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 Editorwindow.

    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+F7to compile the program.

    #include

    int main(void){

    /* printing out greeting */

    printf("Hello everybody!\n");

    printf("This is my first C program file. \n");

    return 0;}

  • 8/11/2019 Lab 1-Getting Started With VC2005

    6/13

    C Programming LabUniversiti 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 F7to link the program.

  • 8/11/2019 Lab 1-Getting Started With VC2005

    7/13

    C Programming LabUniversiti 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

  • 8/11/2019 Lab 1-Getting Started With VC2005

    8/13

    C Programming LabUniversiti 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 useprintf function.

    Dealing with Compilation Error (syntax error)

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

  • 8/11/2019 Lab 1-Getting Started With VC2005

    9/13

    C Programming LabUniversiti 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

    int main(void){

    printf(Hello everybody!\n);

    printf(This is my first C program file.\n);}

    printf(Hello everybody!\n);

    - notice that you should removethe symbol after the \n.

    Number of errors in thefile

    First error

  • 8/11/2019 Lab 1-Getting Started With VC2005

    10/13

    C Programming LabUniversiti 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 thelocation of the error

  • 8/11/2019 Lab 1-Getting Started With VC2005

    11/13

    C Programming LabUniversiti 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 Removebutton (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 Deletebutton, you are going to delete the filepermanently. 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)

  • 8/11/2019 Lab 1-Getting Started With VC2005

    12/13

    C Programming LabUniversiti 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

  • 8/11/2019 Lab 1-Getting Started With VC2005

    13/13

    C Programming LabUniversiti 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 labsession \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

    int main()

    {

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

    return 0;}