< Previous |
Basic Output |
Next > |
We're ready to create our first program. Here are the steps.
- Create a new project:
- Start Visual C++ Express 2005 (hereafter referred to as VC++).
- Select File/New/Project
- In the Project types panel, choose General.
- In the Templates panel, choose Empty Project.
- In the Name edit, enter
HelloWorld
. - (Leave Location and Solution Name alone, and leave Create directory for solution checked.)
- Click OK.
- Create a file called
main.c
for your code:- Select Project/Add New Item.
- In the Categories panel, choose Code.
- In the Templates panel, choose C++ File (.cpp).
- In the Name edit, enter
main.c
(note that we are using the C suffix, .c, not the default C++ suffix). - Click Add.
- Type in the following code. Be sure to get the punctuation correct -- notice the semicolons at the end of some lines, use double quotes (not two apostrophes), and be careful to use backslash (\) and not slash (/).
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
Explanations
You'll see these items in the sample programs; they are important to the compiler, but it's not necessary for you to fully understand them at this point. You can skip this list, but here's a quick explanation for the curious:
#include
- this is a compiler directive that lets the compiler know we want it to include a file that has information about something that our program will use.stdio.h
- this file is included because it contains details about theprintf
function that our code uses (stdio is short for "standard input and output").main()
- this is a function, and the special name (main) lets the compiler know that this is where to start running our program. Theint
before it indicates the type of value returned by the function, in this case an integer.
Important stuff...
{ }
- the braces indicate a block of code. This construct is used extensively in C to indicate a group of instructions.printf(...)
- this is a function that will print a formatted string (to "standard" output, i.e. the screen). The code for that function lives in a library that will be accessed as part of the build process.- The parameter or argument to
printf
is a string, which needs to be contained in double quotes. \n
- an escape character that tells the code to display a newline character. (You can't just put a carriage return in the string.) The backslash is a special character, and the character after it indicates what should be displayed. (If you actually want a backslash displayed, put two of them in the string: "\\".)- Each statement must end with a semicolon (
;
). - For readability, convention is to put one statement per line, and to indent the statements within a block (the contents of { }).
Changes
Make the following changes to your program:
- Use multiple
printf
statements to get the same result. - Put "World" on its own line (careful about the space).
- Keep your code "pretty" -- make sure that your statements are all nicely aligned. This is a good habit to get into now!
- Try adding additional text, including new lines. Can you create a blank line?
Exercises
Let's introduce a compiler error to see how it gets reported:
- Remove the semicolon from the end of a
printf
statement. - Hit F7 to build the program.
- Notice that the Output window (at the bottom) reports a compiler error. Double-click on the line reporting the error; this will highlight the line of code that is mentioned in the error report.
- Read the message explaining the problem. In this case, it says there is a missing semicolon; note that the highlighted line is the line after the one that's missing the semicolon.
When you are done working with this project, select File/Close Solution.
Summary
- Create a new project and main.c file for each program.
- Build your program with F7 or Build/Build Solution.
- Check for errors in the Output window.
- Run your program with Ctrl-F5 or Debug/Start Without Debugging.
- Use
printf
to display formatted text. Use "\n" to start a new line of text.
< Previous | Next > |