13 September

Introduction, motivation, tools, identifiers, main, and printf. Hello, world!

Motivation

Programming a computer is a tremendously valuable skill, but it takes a lot of practice and patience. Try to work a little every day (and a lot some days!) and don’t get discouraged if it starts to feel difficult. Although some people will make

In particular don’t get discouraged by mistakes. Mistakes are how we learn. Remember that compiler errors are routine – sure, there’s a mistake, but it doesn’t reflect badly on you. Just systematically start finding and fixing it. Professional programmers with decades of experiences see hundreds of errors every single day.

If you feel like you’re getting lost, do not hesitate to seek help from me. I try to be very accessible, in office hours, by appointment, or over IM. Because everything we learn builds on everything that came before, any problem you’re having will get better only with additional practice and expert assistance. It will not get better on its own!

Getting “hello world” to run

Here we will keep instructions on getting a simple program to run on various compiler systems. For Windows users, I’m recommending Visual C++ Express Edition. In the lab, we have Visual Studio 2008. For the Mac, get Apple’s XCode developer tools. Other configuratinos are possible. If you find directions for some system not listed here, do send them to me.

Here is the sample program we’ll use. You can also start with this for Assignment 1. Do not include the line numbers when you copy and paste it; they are only for reference and not part of the program.

1
2
3
4
5
6
7
#include <stdio.h>

int main()
{
printf("Hello, world!\n");
return 0;
}

Microsoft Visual Studio 2008

  1. File » New » Project

  2. Project type: Win32 » Win32 console application

  3. Set location to save it (desktop or my documents or your flash drive are good places) and set names to the same, such as hello.

  4. In the project wizard, hit Next.

  5. Check the box called “empty project”.

  6. You should see a project explorer along the left side. You can open and close the sections using the plus/minus signs. Underneath the project name, you should see Header files, Resource files, and Source files.

  7. Right-click source files, choose Add » New Item and then Visual C++ » Code » CPP File. Give it a name, such as hello.cpp

  8. Now you get a blank window on the right. Type your code there.

  9. Save with control-S. Run without debugging using control-F5.

Microsoft Visual C++ Express Edition

  1. New project
  2. Win32 Console Application
  3. Name: cs102hello
  4. Location: set to Desktop or thumb drive or something.
  5. Ok to set the above
  6. Application wizard: hit Next
  7. Click “empty project” check box.
  8. Finish application wizard.
  9. In solution explorer, right-click on Source Files
  10. Add > New Item
  11. C++ File (.cpp)
  12. Name: cs102hello.cpp
  13. Add

The debug menu may not have the choice run without debugging, but the keystroke control-F5 still works.

Apple Xcode

Note: the latest version of Xcode you get from the Apple developer web site will required the latest version of Mac OS X (10.6, Snow Leopard).

  1. Open Macintosh HD » Developer » Applications » Xcode.

  2. Choose Create new Xcode project.

  3. In the dialog, choose Mac OS X » Application » Command Line Tool. Set Type to C++ (with stdc++ library) and click choose.

  4. It will pop up a file dialog for where you want to save the project. I suggest creating a new folder CS102 on your Desktop, and then store this projec by name (such as hello) under that folder.

  5. This creates for you the file CS102 » hello » main.cpp. The Xcode window will have this file (and a few others) listed across the center/top. Open it.

  6. Clear the initial code there and type or paste your own. Save it.

  7. Now choose Run » Console from the menu (or command-shift-R). This opens up a ‘console’ window where your output will appear (in bold face).

  8. Click the ‘Build and Run’ icon on the tool bar.

  9. To change the font or make it bigger, go to Xcode menu » Preferences. Scroll across to Fonts & Colors. highlight all the categories below and double click on any font name. It brings up a font dialog. Use the size slider all the way on the right to increase the size.

Dissecting the sample program

Here are some of the concepts we covered about the sample program today. They are probably incomplete.

Identifier rules

Identifiers are how we name things in a program. main and printf are examples of identifiers. They both name functions: one defined in this program, and one included from stdio.h.

Identifiers must start with a letter (upper or lower case) or an underscore character (shift-hyphen). Following the first character, they can contain letters, numbers, or underscore. They may not contain spaces. However, we often use the underscore to separate words that are part of the same identifier, for readability, as in: quiz_2_average or parking_ticket_amount.

Identifiers are case-sensitive, which means that it matters whether upper- or lower-case letters are used. Quiz and QUIZ and quiz and qUiZ are all valid identifiers but they are different identifiers. You cannot type Quiz in one place and then expect quiz to work in another.

Fonts

Because programming languages are picky, it’s important to configure a good font, and make it big enough, so that characters like 0 (zero), O (capital O), 1 (one), l (lowercase l), ; (semi-colon), and : (colon) are sufficiently distinguishable.

In Visual Studio, you can configure this under Tools » Options » Fonts & Colors. It’s also recommended to use a fixed-width (monospace) font (they were the bold ones in Visual Studio). A reasonable choice is “Consolas”.

Syntax

Syntax refers to the form a program takes, and semantics refers to the meaning (what it does). There are lots of ways to change the syntax without affecting the semantics: add or reduce the spacing, put statements all on one line or spread it out, indent more or less.

However, not all of these changes are equally good. Since we write programs for humans to read, not just for computers to execute, you want to write your programs to maximize readability. A little extra space is almost always better. Put separate ideas on separate lines. Proper indentation is crucial: any statement inside a block (the curly braces) should be indented using the tab key (or a few spaces, that’s up to you). The braces themselves should align with each other vertically.

Comments also improve the readability of your program. They are ignored by the compiler, so they can contain plain English, or even C code that you don’t want to execute right now. There are two kinds: single-line and multi-line:

1
2
3
4
5
6
7
8
9
/* This is a
multi-line comment
that is started by slash-star,
and ended with star-slash.
Code can come right after: */ printf("This is code\n");

printf("Still code.\n"); // This is single-line comment.
// Another single-line. Starts with double-slash,
// ends at end of line.

©2011 Christopher League · some rights reserved · CC by-sa