Sams Teach Yourself C in 24 Hours (10 page)

BOOK: Sams Teach Yourself C in 24 Hours
5.09Mb size Format: txt, pdf, ePub

from Visual C++ ver-

sion 5.0.

Then, you can open a new file within the IDE by clicking the New File button on the far left side of the toolbar, and type the following text in the space of the new file:

#include

main()

{

printf (“Howdy, neighbor! This is my first C program.\n”);

return 0;

}

03 067231861x CH01 4.10.2000 10:59 AM Page 19

Taking the First Step

19

Figure 1.4 shows the IDE with the text you just typed in. Don’t worry about the meaning of the text. In the next chapter, “Your First C Program,” will explain it to you.

1

FIGURE 1.4

Writing code in the

Visual C++ 5.0 IDE.

Then, you need to save the text as a file. Let’s call the file MyFirstProgram.c. It is also a good idea to create a new directory on your hard drive to store your programming projects, and save the new file there. First, click the Save button on the toolbar. In the Save As dialog box, click the New Folder button and type in a name for your programming folder. Then double-click that folder to open it, type MyFirstProgram.c in the File Name box, and click Save. Note that the extension .c is used to indicate that the file you just saved is a C program file.

Now, you need to click the Build menu and choose the Compile MyFirstProgram.c option. By doing so, you ask the compiler to compile the text you just typed in and saved (see Figure 1.5). At this point, Visual C++ may prompt you to create a new workspace; just click Yes and this will be done automatically. There should be no errors or warnings in the output window after the compiler is run.

Then, click the Build menu again, and this time, choose the Build MyFirstProgram.exe option which will eventually produce an executable file called MyFirstProgram.exe.

Figure 1.6 shows that there are no errors or warnings after MyFirstProgram.exe is built.

03 067231861x CH01 4.10.2000 10:59 AM Page 20

20

Hour 1

FIGURE 1.5

Compiling a C pro-

gram using the IDE.

FIGURE 1.6

Creating a program

executable file.

You are now ready to run the executable file, MyFirstProgram.exe, that you just compiled. You can run it from the menu path: Build, Execute MyFirstProgram.exe. Because the executable file is a console mode application, a DOS prompt window will show up when the executable file is running (see Figure 1.7).

03 067231861x CH01 4.10.2000 10:59 AM Page 21

Taking the First Step

21

FIGURE 1.7

Running a C program.

1

You can tell that the first line in the DOS prompt window shown in Figure 1.7 is the exact line you just typed: “Howdy neighbor! This is my first C program.” This is indeed your first C program output! (Note that the second line shown in the DOS prompt is just a built-in reminder from the DOS prompt window.)

Okay. I just demonstrated how to use Visual C++ compiler to write and compile a C program, and how to make the program executable. For more details, you need to read books such as
Teach Yourself Visual C++ 5 in 21 Days
that focus on teaching you how to use Visual C++ compiler.

Using Borland’s Compiler

In this section, I’m going to show you how to use the C compiler that is bundled with Borland’s C++ package. The procedure of the section is quite similar as the one of the previous section. If you need to learn more details on how to install Borland C++, please follow the instructions that come with the compiler.

I assume that you’ve installed a copy of Borland C++ 5.02 on your computer. To start the compiler, you can click the Start button from your Windows 95 (or Windows 98 or NT) task bar, and choose Programs, Borland C++ 5.02, Borland C++. Or, you can simply run the application file bcw.exe directly from the directory (folder) where you installed the Borland C++ package. Figure 1.8 shows an example of the integrated development environment (IDE) from Borland C++ 5.02.

Then, you can open a new file within the IDE, and type the following text in the space of the newly opened file:

#include

main()

{

printf (“Howdy, neighbor! This is my first C program.\n”);

return 0;

}

03 067231861x CH01 4.10.2000 10:59 AM Page 22

22

Hour 1

FIGURE 1.8

Creating a program

with the Borland C++

IDE.

Figure 1.9 shows the IDE with the text you just typed. Don’t worry about the meaning of the text. The next hour will explain it to you.

FIGURE 1.9

Saving C program text

with Borland’s IDE.

03 067231861x CH01 4.10.2000 10:59 AM Page 23

Taking the First Step

23

Now you need to save the text as a file. Let’s call the file MyFirstProgram.c. Note that the extension .c is used to indicate that the file you just saved is a C program file.

1

Now, you need to click the Project menu and choose the Compile option. By doing so, you ask the compiler to start compiling the text you just typed and saved. Figure 1.10

shows that there are no errors or warnings after MyFirstProgram.c is compiled and MyFirstProgram.exe is created.

FIGURE 1.10

Compiling a C pro-

gram using Borland’s

IDE.

You are now ready to run the executable file, MyFirstProgram.exe, that you just compiled. You can run it by clicking the Run button on the toolbar. Or, you can run MyFirstProgram.exe directly from the directory where you created it. Because the executable file is in fact a DOS application, a DOS prompt window will show up when the executable file is running (see Figure 1.11).

FIGURE 1.11

Running a C program

using Borland’s IDE.

03 067231861x CH01 4.10.2000 10:59 AM Page 24

24

Hour 1

Figure 1.11 displays the output exactly as you just typed it: “Howdy neighbor! This is my first C program.” This is indeed your first C program output!

If you want to learn more details on how to use Borland C++, read a book such as
Teach
Yourself Borland C++ 5 in 21 Days
.

Other books

The Future of the Mind by Michio Kaku
Inked Ever After by Elle Aycart
RATH - Inception by Jeff Olah
Facing the Future by Jerry B. Jenkins, Tim LaHaye
Sophia by D B Reynolds
Cape Cod by Martin, William
With Violets by Elizabeth Robards
Moon River by J. R. Rain