Previous Table of Contents Next


Chapter 2
Basic Features of C/C++ Programs

The core features of C and C++ are not significantly harder to learn than those of other languages. Most of C/C++ is almost as basic as, say, the language called Basic. When people have difficulty learning C/C++, it is usually because of pointers—a topic we’ll save for the next chapter.

At the same time, C/C++ has unique features you’ll need to pay special attention to. There’s more to C/C++ than just ending every line with a semicolon. For the most part, C/C++ features do not complicate the language syntax, and in some ways they make it more flexible. With this flexibility comes the power to make more errors, but if you’re careful you’ll often find that you can do more in fewer lines of code.

C and C++ are two distinct languages, but they share the same basic structure. I’ll refer only to C++ from now on, pointing out places where the rules for C are different from those of C++.

Your First C++ Program

The first test of a programming system is to print a simple string. Such a program in C++ is not quite as simple as in Basic, but neither is it very long it:

#include <stdio.h>

void main () {
   printf(“Can you C++ now?”);
}

As you type this program, notice several things. First, capitalization counts in C++, in contrast to many languages, such as FORTRAN and Basic, in which you can randomly type every other character as uppercase if you want to. When you enter keywords and identifiers (for example, a variable or function name) C++ expects you to match upper-case and lowercase letters precisely, and it gets badly confused if you don’t. Another thing to watch for is the punctuation: the semicolon (;) and the braces ({}). You must enter these characters exactly where indicated in the example.

However, there are some things you can be freer about with C++ than you might with other languages. On any given line, you can space things many different ways, and you can even put syntax elements on different lines—C++ doesn’t care. As C++ is concerned, the following program is the same as the one shown previously:

#include <stdio.h>

void
main ()
{
    printf
        (“Can you C++ now?”);
}

This version works because C++ uses the semicolon (;) to determine where the printf statement ends; it usually ignores the end of the physical line (except in the case of directives and comments). On the one hand, you have to do a little more typing, but, on the other hand, the C++ syntax grants you more freedom than Basic does.

After entering this program in a source file, you use a compiler or development environment to compile it, which is the process of translating the program into an executable form. I don’t discuss specific compilers in this book. For information on how to build a program, see your compiler documentation.
Everything in this section applies equally well to the C language, except for one thing: in C, the void keyword in front of main is optional. In C++, it is required. Here, void means that the function (main) does not have a return statement. More about that later.

You can, of course, print your own string instead of “Can you C++ now?” Here’s the general pattern for the program:

#include <stdio.h>
void main () {
   printf(“enter-your-string here”);
}

Enter any text you want in place of enter-your-string-here.

Adding Data Declarations

Programs start to become interesting and useful at the point where they can store and manipulate information. Such programs need a place to put the data: variables.

C++ variable declaration consists of a type name followed by a variable name and a semicolon. The basic (or primitive) types include int, short, long, float, and double, among others. The following example declares two variables of type int.

int variable-name1;
int variable-name2;

The C++ variable-declaration syntax is simple. It doesn’t involve any extra keyword, such as Dim or var.

You can create multiple data declarations on the same line. Separate each variable with a comma. The following example declares three variables of type short (i, j, k) and three variables of type float (x, y, z).

short i, j, k;
float x, y, z;

The basic syntax for data declarations in C++ has another interesting twist. You can initialize variables as they are declared. Doing so gives a variable a starting value but in no way prevents you from changing it later.

To initialize a variable, use the equal sign followed by a value. For example:

int my_var = 0;          // my-var initialized to 0
int your_var = 1;        // your_var initialized to 1
int a, b = 10, c = 12;   // b and c initialized, a is not

Before we proceed, now would be a good time to discuss comments. In C++, a comment consists of all the text starting with the double slashes (//) forward to the end of the tine. A comment is ignored by the compiler. You can put any text in a comment, but people typically use comments to explain a part of their program. When they go back later and look at the source code, the comments help them recall how the program works.

C++ also supports the begin- and end-comment symbols from the C language (/* and */, respectively). Not all C compilers support the C++ comment-to-end-of-line symbol (//), although many of them do.

Armed with the ability to declare and initialize data, we can now create a more interesting program:

#include <stdio.h>

void main () {
   int x = 1;
   int y = 2;

   printf(“The sum of x + y is %d”, x + y);
}

This use of the printf function here requires a little more explanation. The printf function supports formatted output, which means that it can take a numeric argument such as x+y and print the value of this number along with the rest of the string. The format character %d means “Print the value of the next argument.” Here, d stands for decimal integer. Floating-point values are printed using %f.

All the primitive types are variations on just two kinds of data: integer and floating-point. An integer, as you may have learned in school, is a number that cannot hold fractions (but it can be negative). Floating-point values can have a fractional portion. Floating-point numbers are more flexible, but integers are more efficient. If you know that a certain variable will never need to hold a fraction—and there are a great many cases of this, as with a loop counter, for example—declare it as an integer.

The basic characteristics of the int, short, long, float, and double data types are summarized in Table 2.1.


Previous Table of Contents Next