CS 200 - Lab 1

In this lab you will start Microsoft Visual Studio 2005, create a new C++ project, write a very simple program, and execute the program, capturing its output to a window.

Note that although Visual Studio supports the C++ programming language, we will be writing programs in the C programming language, which is a subset of C++.  Therefore, any time you see a reference to "C++", you should understand that you are using only the "C" portion of the language.

Step 1 - Create a new project

The first step is to create a new Visual Studio project.  A project is a collection of source files that can be compiled into an executable.  The executable can then be run.

From the Start menu, choose Start->All Programs->Microsoft Visual Studio 2005->Microsoft Visual Studio 2005.  The main Visual Studio window will appear.

Choose File->New->Project...  The New Project dialog will appear.  Under "Project Types", expand "Visual C++" and click on "Win32".  Choose "Win32 Console Application".  In the "Name:" box, type "lab1".  The dialog should look like this:

New project dialog

Now you will see the Win32 Application Wizard dialog.  Click "Next".  Under "Application Type", choose "Console application".  Then under "Application Settings" click "Empty Project".  The dialog should look like this:

Win32 Application Wizard

Click "Finish".  You have now created a new Project in Visual Studio.

Step 2 - Add a source file

The next step is to add a new C source file to the project.  The main window of Visual Studio should now show some items in the Solution Explorer, like this:

Solution Explorer

Right-click on "Source Files".  Choose Add->New Item...  Under "Templates", choose "C++ File (.cpp)".  Type "Hello" in the "Name:" box.  The dialog should look like this:

Add new item dialog

Click "Add".  The Visual Studio window will now show an empty file, "Hello.cpp".

Step 3 - Write a Program

Type the following lines of text into Hello.cpp.  Make sure you type them exactly as they appear here.  You may separate these lines using blank lines if you wish.

The first line should read:

#include <stdio.h>

Its purpose is to allow your program to use the "iostream" functions and objects.  This will allow your program to read data from the user, and write data to the display.

The second line should read:

int main()

It defines a function called "main".  The "main" function is where execution of the program begins.

The third line consists of a single "{" character:


C uses curly braces "{" and "}" to delimit blocks of code.  Every function in C, including main, has a block of code, the body of the function.

The fourth line should read:

printf("Hello, world!\n");

This line writes the message "Hello, CS 200!" to a window when the program is executed.  Don't forget the semicolon ";" at the end of the line!

The fifth line should read:

return 0;

This line determines the exit code of the program.  Usually, programs will use the exit code 0.  A program that terminates in an unexpected way (e.g., because of an error) may return a non-zero error.  (Again, don't forget the semicolon at the end of the line.)

The sixth, and final, line of the program should read:


This line closes the block of code started by the fourth line, and completes the definition of the main function.

When you are done typing in the program it should look something like this:

Complete program

Step 4 - Compiling and running the program

After you've typed in the program, you're ready to compile and execute it.  Compiling a program translates the code you've written in C (a high-level language) into machine instructions that can be directly executed on the computer's CPU.

In Visual Studio, press F6 to build (compile) your program.  The output window (at the bottom of the Visual Studio window) will look something like this:

Build output

Now, press Control-F5 to execute the program.  A console window should appear displaying the phrase "Hello, CS 200!":

Console window with program output

You've written, compiled, and executed a C program!  Congratulations!

Press any key to dismiss the console window and return to Visual Studio.