Blank: C and Value Essay

Submitted By sfayson
Words: 1932
Pages: 8

COMP122
Week 1 C++ Language Summary

Basic Program Structure

Most C++ programs follow a common layout. While not everything that follows is required by the compiler, these things are commonly found because it makes the program code more readable for people other than the author of the program.

First: /* A comment block should be the first thing in a file! */ /* Comments are ignored by the compiler. They help explain the code.*/ /* It should summarize what the program does. */
/* It should identify who wrote the program and when it was written. */ Proper form of commenting uses

/* everything between the slash-star and * star-slash is a comment. This type of comment * can span multiple lines. */ or // comment from the slash-slash to end of the current line

Second:

Pre-processor directives should follow the comment block. A pre-processor directive in an instruction to the preprocessor (which runs before the compiler) as to where to find useful definitions used by the code in the file. For example, the following two lines tell the preprocessor to bring in all the definitions from the iostream header file provided by the system. This is where the cin and cout input and output streams come from that enable keyboard input and console output.

#include using namespace std;

Third:

Constant definitions to be used throughout the file should be next. Constants are similar to declaring and initializing a variable, but the keyword const must be used. Once defined, the value of the constant can never change.

const double PI = 3.1416;

Fourth:

The basic function-block structure which applies to all functions including main:

ReturnType FunctionName (ParameterType ParameterName, …) { // Start of function body constant declarations; // Constants private to this function variable declarations and initialization; // Variables private to this function

statements; // Whatever is needed to do the job // Statements need semi-colons at the end return value; // Value must be of type ReturnType } // End of function body

Any number of function blocks can appear within a file. One function block cannot appear inside of another function block. More details about functions will be coming in Week 5! For now, the only function you need to be concerned with is the standard main function as follows:

int main( char ** argv, int count)
{
// declare constants and variables to be used inside this function

// implement the statements needed per the steps of the algorithm // return 0 to indicate success
}

Note the position of the curly brackets which indicate the start and end of a block in C and in C++. A standard code formatting rule is to indent one tab stop every time you open a new block. Note the indentation of one tab stop of every line in the above example between the open and closing curly brackets.

Identifiers – an identifier is a name given to a variable or function. Alphabetic characters and numbers and the underscore character ‘_’ can be used to make an identifier. The first character of an identifier cannot be a number. An identifier cannot contain spaces. Upper or lower case makes a difference, so ‘count’ and ‘Count’ are two different identifiers.

Basic Data Types

A data type specifies an amount of memory allocated for storing a piece of information and the range of values which are valid for that data type. The amount of memory allocated for storage of a particular data type is compiler specific and will vary depending on the specifics of the processor a compiler is designed to produce code for. Data types can be integral (whole numbers only) or floating point (includes digits after the decimal point). Data types can be signed (positive and negative) or unsigned (positive only). Some of the basic data types are described below. Storage and ranges are given assuming a typical Pentium class 32-bit processor:…