DOC PREVIEW
UCSD CSE 125 - A Crash Course in C

This preview shows page 1-2-3-20-21-40-41-42 out of 42 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

A Crash Course in C++The goal of this chapter is to cover briefly the most important parts of C++ so that you have a baseof knowledge before embarking on the rest of the book. This chapter is not a comprehensive lessonin the C++ programming language. The very basic points (like what a program is and the differ-ence between = and ==) are not covered. The very esoteric points (remember what a union is? howabout the volatile keyword?) are also omitted. Certain parts of the C language that are less rele-vant in C++ are also left out, as are parts of C++ that get in-depth coverage in later chapters.This chapter aims to cover the parts of C++ that programmers encounter on a daily basis. If you’vebeen away from C++ for a while and you’ve forgotten the syntax for a for loop, you’ll find that inthis chapter. If you’re fairly new to C++ and you don’t understand what a reference variable is,you’ll learn that here as well.If you already have significant experience with C++, skim this chapter to make sure that therearen’t any fundamental parts of the language on which you need to brush up. If you’re new toC++, take the time to read this chapter carefully and make sure that you understand the examples.If you need additional introductory information, consult the titles listed in Appendix B. The Basics of C++The C++ language is often viewed as a “better C” or a “superset of C.” Many of the annoyancesor rough edges of the C language were addressed when C++ was designed. Because C++ is basedon C, much of the syntax you’ll see in this section will look familiar to you if are an experiencedC programmer. The two languages certainly have their differences, though. As evidence, The C++Programming Language by C++ creator Bjarne Stroustrup weighs in at 911 pages, while Kernighanand Ritchie’s The C Programming Language is a scant 274 pages. So if you’re a C programmer, be onthe lookout for new or unfamiliar syntax!04_574841 ch01.qxd 12/15/04 3:39 PM Page 1COPYRIGHTED MATERIALThe Obligatory Hello, WorldIn all its glory, the following code is the simplest C++ program you’re likely to encounter.// helloworld.cpp#include <iostream>int main(int argc, char** argv){std::cout << “Hello, World!” << std::endl;return 0;}This code, as you might expect, prints the message Hello, World! on the screen. It is a simple programand unlikely to win any awards, but it does exhibit several important concepts about the format of aC++ program.CommentsThe first line of the program is a comment, a message that exists for the programmer only and is ignoredby the compiler. In C++, there are two ways to delineate a comment. In the preceding example, twoslashes indicate that whatever follows on that line is a comment.// helloworld.cppThe same behavior (this is to say, none) would be achieved by using a C-style comment, which is alsovalid in C++. C-style comments start with /* and end with */. In this fashion, C-style comments arecapable of spanning multiple lines. The code below shows a C-style comment in action (or, more appro-priately, inaction)./* this is a multiline* C-style comment. The* compiler will ignore* it. */Comments are covered in detail in Chapter 7.Preprocessor DirectivesBuilding a C++ program is a three-step process. First, the code is run through a preprocessor, which recog-nizes metainformation about the code. Next, the code is compiled, or translated into machine-readableobject files. Finally, the individual object files are linked together into a single application. Directives thatare aimed at the preprocessor start with the # character, as in the line #include <iostream> in theprevious example. In this case, an include directive tells the preprocessor to take everything from theiostream header file and make it available to the current file. The most common use of header files is todeclare functions that will be defined elsewhere. Remember, a declaration tells the compiler how a func-tion is called. A definition contains the actual code for the function. The iostream header declares theinput and output mechanisms provided by C++. If the program did not include it, it would be unable toperform its only task of outputting text.2Chapter 104_574841 ch01.qxd 12/15/04 3:39 PM Page 2The table below shows some of the most common preprocessor directives.Preprocessor Directive Functionality Common Uses#include [file] The specified file is inserted into Almost always used to include the code at the location of the header files so that code can directive. make use of functionality thatis defined elsewhere.#define [key] [value] Every occurrence of the specified Often used in C to define a key is replaced with the specified constant value or a macro. C++ value. provides a better mechanismfor constants. Macros are oftendangerous so #define is rarelyused in C++. See Chapter 12for details.#ifdef [key] Code within the ifdef Used most frequently to protect #ifndef [key] (“if defined”) or ifndef against circular includes. Each #endif (“if not defined”) blocks are included file defines a value conditionally included or initially and surrounds the rest omitted based on whether of its code with a #ifndef and the specified value has been #endif so that it won’t be defined with #define. included multiple times.#pragma Varies from compiler to compiler. Because usage of #pragma is notOften allows the programmer to standard across compilers, we display a warning or error if the advocate not using it. directive is reached during preprocessing.The main functionmain() is, of course, where the program starts. An int is returned from main(), indicating the resultstatus of the program. main() takes two parameters: argc gives the number of arguments passed to theprogram, and argv contains those arguments. Note that the first argument is always the name of theprogram itself.I/O StreamsIf you’re new to C++ and coming from a C background, you’re probably wondering what std::cout isand what has been done with trusty old printf(). While printf() can still be used in C++, a muchbetter input/output facility is provided by the streams library. In C, included files usually end in .h, such as <stdio.h>. In C++, the suffix is omit-ted for standard library headers, such as <iostream>. Your favorite standard head-ers from C still exist in C++, but with new names. For example, you can access thefunctionality from <stdio.h> by including <cstdio>.3A Crash Course in C++04_574841 ch01.qxd


View Full Document

UCSD CSE 125 - A Crash Course in C

Download A Crash Course in C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A Crash Course in C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Crash Course in C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?