Introduction, motivation, tools, identifiers, main
, and printf
. Hello, world!
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!
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 |
|
File » New » Project
Project type: Win32 » Win32 console application
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
.
In the project wizard, hit Next.
Check the box called “empty project”.
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.
Right-click source files, choose Add » New Item and then Visual C++ » Code » CPP File. Give it a name, such as hello.cpp
Now you get a blank window on the right. Type your code there.
Save with control-S. Run without debugging using control-F5.
The debug menu may not have the choice run without debugging, but the keystroke control-F5 still works.
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).
Open Macintosh HD » Developer » Applications » Xcode.
Choose Create new Xcode project.
In the dialog, choose Mac OS X » Application » Command Line Tool. Set Type to C++ (with stdc++ library) and click choose.
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.
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.
Clear the initial code there and type or paste your own. Save it.
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).
Click the ‘Build and Run’ icon on the tool bar.
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.
Here are some of the concepts we covered about the sample program today. They are probably incomplete.
include
means to bring in functionality (called a library) from somewhere else. In this case, stdio.h
refers to the standard input/output library, which is the home of printf
.
main()
is the name of a function, or procedure, which is a sequence of steps to execute. The parentheses indicate that it’s a function, which is why we need them even though, in this case, they’re empty. The name main
is special, because it is used as the “entry point” of the program — it all starts here. Later on we’ll have additional functions with other names.
The curly brackets {}
(also called braces) indicate the beginning and end of a block of code. They show the extent of the main
procedure.
The printf
is how we do formatted output. It is a function (you can tell because of the parentheses) from the standard input/output library.
The double quotes indicate a string or sequence of characters. Because they are provided to printf
, these are the characters that will appear on the screen when it runs.
One of the characters is special: it consists of the sequence backslash, then lowercase ‘n’: \n
This is called a new line character. It tells the output to go on to the next line, much like hitting return or enter in a word processor.
Finally, return
is a keyword (there are no parentheses, so it cannot be a function) that means we are finished with the current function. At this point, because the function we’re talking about is main
, the program ends. If you were to include extra code after return
, it would not be executed.
Note also the semi-colons. There are semi-colons on statements within the block (between the curly braces), but not on the curly braces themselves, and not on the main()
declaration, and not on the #include
. If you forget a semi-colon, the compiler will definitely let you know. However, if you put one where it is not supposed to go, it could happen that the compiler won’t notice but the program also won’t work right! We’ll see examples of that insidious problem later.
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.
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 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 |
|
©2011 Christopher League · some rights reserved · CC by-sa