Chapter 1

A Quick Start for the Impatient

In This Chapter

arrow Getting the Code::Blocks IDE

arrow Setting up your first project

arrow Typing in code

arrow Building and running

arrow Quitting Code::Blocks

You’re most likely eager to get started programming in C. I shan’t waste your time.

What You Need in Order to Program

To take ultimate control over a computer, tablet, cell phone, gaming console, or whatever, you need a few software tools. The good news is that at this point in the 21st century, all those tools are free and easily obtained from the Internet. You just need to know what’s required and where to get it.

Obtaining programming tools

The two most important things you need in order to begin your programming adventure are

check.png A computer

check.png Access to the Internet

The computer is your primary tool for writing and compiling code. Yes, even if you’re writing a game for the Xbox, you need a computer to be able to code. The computer can be a PC or a Macintosh. The PC can run Windows or Linux.

Internet access is necessary to obtain the rest of your programming tools. You need a text editor to write the programming code and a compiler to translate the code into a program. The compiler generally comes with other tools you need, such as a linker and a debugger. All these tools are found at no cost on the Internet.

Don’t freak! The terms compiler, linker, and debugger are all defined in Chapter 2.

Acquiring an Integrated Development Environment (IDE)

It’s completely admirable for you to being your programming journey by foraging for a text editor and a compiler and other tools. Using separate programs at the command line in a terminal window was the way I learned how to program, back in the dark ages. It’s a process that can still be done, but it’s quaint.

The smooth, professional way to craft code today is to obtain an Integrated Development Environment — called an IDE by the cool kids. It combines all the tools you need for programming into one compact, terrifying, and intimidating unit.

You use the IDE to write code, build programs, debug programs, and sift in all sorts of magic. Though it’s nostalgic to use a separate text editor or compiler, all the pros use an IDE. That’s what I recommend for use in this book as well.

Obtaining an IDE also solves the myriad problems of setting up a compiler, configuring the text editor, and getting all those disparate elements to work together. Having an IDE is the best way to get started with programming — which I can guess is something you’re really eager to do.

Behold the Code::Blocks IDE

You’ll find the Internet spackled with various IDEs, and they’re all pretty good. To be consistent, this book uses the Code::Blocks IDE, which works on Windows, Mac OS X, and Linux. It comes with everything you need.

If you already have an IDE, great! I’m certain that it does things similarly to Code::Blocks, though this book’s illustrations and examples, especially in the earlier chapters, are specific to Code::Blocks.

Installing Code::Blocks

Obtain Code::Blocks from the Internet at this website:

www.codeblocks.org

This website will doubtless be altered over time, so the following steps for installing the IDE may change subtly:

1. Use your computer’s web browser to visit the Code::Blocks website.

2. Enter the Downloads area.

Download a binary or executable version of Code::Blocks. It must be specific to your computer’s operating system. Further, find the release that includes a C compiler, such as the common MinGW compiler.

3. Click the link to display the binary or executable installation for Code::Blocks.

As this book goes to press, the link is labeled Download the Binary Release.

4. Choose your computer’s operating system or scroll to the portion of the screen that lists options for that operating system.

You may find sections (or pages) for Windows, Linux, and Mac OS X.

5. Click the link that downloads the compiler and IDE for your computer’s operating system.

The Windows version of the IDE and compiler is named in this fashion:

codeblocks-xx.yymingw-setup.exe

The xx and yy represent Code::Block's major and minor release numbers.

In Linux, you can choose the 32-bit or 64-bit version, depending on your distribution, or distro, and the file format you want. I recommend a stable release.

Mac OS X users can choose whether to download a .dmg, or disk image, file or the traditional zip file archive.

6. Extract the Code::Blocks installation program from the archive.

Despite the For Dummies title of this book, I'll assume that you can successfully work with .zip, .gz, .dmg, and other file formats for whatever operating system you use.

7. Run the installation program.

Heed the directions on the screen. Perform a default installation; you don’t need to customize anything at this point.

In Windows, ensure that you’re installing the MinGW compiler suite. If you don’t see that option presented in the Choose Components window, you’ve downloaded the wrong version of Code::Blocks. Go back to Step 5.

8. Finish the installation by running Code:Blocks.

On my computer, a prompt appeared, asking whether I wanted to run Code::Blocks. I clicked the Yes button. If you don’t see this prompt, use the computer’s operating system to start Code::Blocks just as you’d start any program.

9. Close the installation window.

Even though you may see Code::Blocks splashed all over the screen, you may still need to wrap up the installation by closing the installation window.

The following section offers an overview of the Code::Blocks program interface.

Touring the Code::Blocks workspace

If Code::Blocks hasn’t started, go ahead and start it. It starts just like any other program does: Locate its icon on the Start button menu, or you may also find the Code::Blocks shortcut icon on the desktop, which is the easiest way to start the IDE in Windows 8.

Figure 1-1 illustrates the Code::Blocks workspace, which is the official name of the massive mosaic of windows you see on the screen.

The details in Figure 1-1 are rather small, but what you need to find are the main areas, which are called out in the figure:

Toolbars: These messy strips, adorned with various command buttons, cling to the top of the Code::Blocks window. There are eight toolbars, which you can rearrange, show, or hide. Don’t mess with them until you get comfy with the interface.

Management: The window on the left side of the workspace features four tabs, though you may not see all four at one time. The window provides a handy oversight of your programming endeavors.

Status bar: At the bottom of the screen, you see information about the project and editor and about other activities that take place in Code::Blocks.

Editor: The big window in the center-right area of the screen is where you type code.

Logs: The bottom of the screen features a window with many, many tabs. Each tab displays information about your programming projects. The tab you use most often is named Build Log.

The View menu controls the visibility of every item displayed in the window. Choose the proper command, such as Manager, from the View menu to show or hide that item. Control toolbars by using the View⇒Toolbars submenu.

9781118737637-fg0101.eps

Figure 1-1:The Code::Blocks workspace.

remember.eps The most important thing to remember about the Code::Blocks interface is not to let it befuddle you. An IDE such as Code::Blocks can be highly intimidating, even when you consider yourself an old hand at programming. Don’t worry: You’ll soon feel right at home.

check.png Maximize the Code::Blocks program window so that it fills the screen. You need all that real estate.

check.png Each of the various areas on the screen — Management, Editor, Logs — can be resized: Position the mouse pointer between two areas. When the pointer changes to a double-arrow thingy, you can drag the mouse to change an area’s size.

check.png The Editor and Logs areas feature tabbed interfaces. Each window displays multiple “sheets” of information. Switch between the sheets by choosing a different tab.

Your First Project

In traditional computer programming, you used a separate text editor, compiler, and linker. Action took place at the command prompt, where you typed the commands to edit, compile, and link. It was a linear process, and it worked well for small projects. With the advent of modern operating systems and programming for mobile devices and gaming consoles, that linear method becomes highly inefficient.

The modern IDE still has the elements of an editor, a compiler, a linker, a debugger, and other programming tools. It sports features necessary to create graphical programs and to craft complex projects. Because of that, the IDE is geared toward working with projects, not merely with individual programs.

This section explains the process of creating a project using the Code::Blocks IDE.

Creating a new project

The examples presented in this book are all console applications, which means that they run in Text mode in a terminal window. I feel that’s the best way to teach basic programming concepts without overwhelming you with a large, complex, graphical beast of a program. So even though an IDE is capable of more, you use it in this book to create simple, console-based programs. Here’s how it works:

1. Start Code::Blocks.

You see the Start Here screen, which displays the Code::Blocks logo and a few links. If you don’t see the Start Here screen, choose File⇒Close Workspace.

2. Click the Create a New Project link.

The New from Template dialog box appears, as shown in Figure 1-2.

3. Choose Console Application and then click the Go button.

The Console Application Wizard appears.

tip.eps You can place a check mark by the item Skip This Page Next Time to skip over the wizard’s first screen.

4. Click the Next button.

5. Choose C as the language you want to use, and then click the Next button.

C is quite different from C++ — you can do things in one language that aren’t allowed in the other.

9781118737637-fg0102.eps

Figure 1-2: New projects start here.

6. Type ex0101 as the project title.

All the code in this book follows this same project title convention: the letters ex (for exercise) followed by a two-digit chapter number and a two-digit project number.

When you set the project title, the project’s filename is automatically filled in.

7. Click the ... (Browse) button to the right of the text box titled Folder to Create Project In.

I recommend that you create and use a special folder for all projects in this book.

8. Use the Make New Folder button in the Browse for Folder dialog box to create a project folder.

If you already have a programming projects folder, create a new subfolder named BegC4D for projects in this book. That folder name is based on this book's title, Beginning Programming with C For Dummies.

If you don't have a programming projects folder, under your account's Main or Home folder, create a subfolder named prog and then create another folder, named c. Finally, create a subfolder named BegC4D and choose it for saving the project.

On my Windows 7 computer, projects for this book are saved in this folder:

C:\Users\Dan\prog\c\BegC4D\

9. Click the OK button to select the folder and close the dialog box.

10. Click the Next button.

The next screen (the last one) allows you to select a compiler and choose whether to create Debug or Release versions of your code, or both.

The compiler selection is fine; the GNU GCC Compiler (or whatever is shown in the window) is the one you want.

11. Remove the check mark by Create Debug Configuration.

You create this configuration only when you need to debug, or fix, a programming predicament that puzzles you. Chapter 25 covers that situation.

12. Click the Finish button.

Code::Blocks builds a skeleton of your project, which you may not yet see in the program’s window. For a simple command-line project, the skeleton is a source code file — a plain-text file that contains the C programming code to help start your project.

Continue reading in the next section.

technicalstuff.eps A console application is one of the simplest programs you can create. If you were building a Windows program, Code::Blocks would create other elements for you and display tools for designing the window, crafting an icon, and working with other required bits and pieces. The IDE makes it easy to work with all that stuff.

Examining the source code

When Code::Blocks conjures up a new project, it creates a smattering of basic elements to get you going. For a console application, that includes only one item: the main.c source code file.

A source code file is a plain-text file that contains programming code. Chapter 2 explains the details, but for now, know that Code::Blocks creates this file automatically whenever you start a new project. It even gives you some sample code to start with, though most of the time you delete the skeleton and start over by writing your own code.

Projects in the IDE are listed in the Management panel on the left side of the screen (refer to Figure 1-1). If you don’t see this panel, press the Shift+F2 keyboard shortcut or choose View⇒Manager from the menu.

Figure 1-3 illustrates the Management panel with the Projects tab selected. The current project is shown opened, but you can see other projects as well.

9781118737637-fg0103.eps

Figure 1-3: Projects in the IDE.

For a new console application project, the source code file is named main.c. It's stored in the project's Sources folder. If you don't see the source code file (refer to Figure 1-3), open the Sources folder to reveal that item.

Double-click the main.c item in the Sources folder to open that file in the editor window. The prewritten code skeleton is shown in Listing 1-1. It's actually code for a real, live C program that you can build, which is covered in the next section.

Listing 1-1: The Code::Blocks Skeleton

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    printf("Hello world!\n");

    return 0;

}

Chapter 2 covers the specifics of what’s going on in Listing 1-1. For now, you can admire the code’s colorful text and pretty formatting.

check.png The Editor window displays line numbers. If you don’t see them, choose Edit⇒Editor Tweaks⇒Show Line Numbers. This book assumes that you can see the line numbers for reference purposes.

check.png remember.eps Line numbers aren’t part of your C code. They’re used for editing; but also when errors occur, they reference line numbers in the code.

check.png You can set the tab indent by choosing Edit⇒Editor Tweaks⇒Tab Size and then picking a size. I prefer four spaces per indent.

check.png Text coloring is set by choosing Edit⇒Highlight Mode⇒C/C++. To remove the highlights, choose the Plain Text item from the Highlight Mode submenu. (It’s the first item.)

Building and running the project

To create a program in the Code::Blocks IDE, you must build the project. This single step does several things, the details of which I avoid discussing until Chapter 2. If you’ve already started your first project, ex0101, and it’s open and displayed in Code::Blocks, you’re ready to build. Heed these steps:

1. Ensure that the project you want to build is activated in the Management window.

Activated projects appear in bold text. If you have more than one project shown in the Projects window, activate the one you want to work with by right-clicking the project name (by the Code::Blocks icon) and choosing the Activate Project command.

2. Choose BuildBuild from the menu.

The Build Log tab in the Logs part of the window displays the results of building the project. You see a few lines of text, the details of which I cover in Chapter 2.

Because you didn’t mess with the source code skeleton, the project compiles with no errors; you see the text within the summary that indicates zero errors and zero warnings. Good. When errors do appear, which is often, you go about fixing them. The error messages help in that regard, which is a topic covered in Chapter 3.

Building a project is only half the job. The other half is running the project, which means executing the completed program from within the IDE.

To run the current project, choose Build⇒Run from the menu. You see the terminal window appear, listing the program’s output, plus some superfluous text, as shown in Figure 1-4.

9781118737637-fg0104.eps

Figure 1-4: Running a program.

Press the Enter key to close the command prompt window.

tip.eps And now, for the shortcut: You can build and run a project using a single command: Choose Build⇒Build and Run.

check.png The keyboard shortcuts for Build, Run, and Build and Run are Ctrl+F9, Ctrl+F10, and F9, respectively. No need to memorize those shortcuts — they’re listed on the menu.

check.png Command buttons to build and run projects are found on the Compiler toolbar. You’ll find a yellow gear icon for Build, a green arrow for Run, and a combination of the two for the Build and Run command.

check.png technicalstuff.eps The program output appears in the top part of the command prompt window (refer to Figure 1-4). The last two lines are generated by the IDE when the program is run. The text shows a value returned from the program to the operating system, a zero, and how long the program took to run (9 milliseconds). The Press any key to continue prompt means that you can press the Enter key to close the window.

Saving and closing

When you’re done with a project, or even after you’ve changed a minor thing, you should save. In an IDE, however, you have several things to save, such as the source code file, the workspace, and the project. Commands are found on the File menu to save each of those items individually.

A handy way to save everything all at once is to use the Save Everything command. This command is found on the File menu, and its handy keyboard shortcut is Alt+Shift+S (or Shift+Command+S on the Macintosh).

If you haven’t yet saved the project, do so now.

You can also close the current project by choosing File⇒Close Project.

check.png Quit Code::Blocks now if you desire to take a break.

check.png Don’t worry! Code::Blocks reminds you when files or projects loom unsaved. Save; then quit.

check.png When the Save Everything command is unavailable, consider that everything is up-to-date and has been saved already.

check.png The standard Save command in Code::Blocks — either File⇒Save or Ctrl+S — is used to save files, such as your C language source code file. I strongly encourage you to save your source code files as you edit them.