Practices
  Home arrow Practices arrow Page 3 - Basic Ideas
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PRACTICES

Basic Ideas
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2005-03-23

    Table of Contents:
  • Basic Ideas
  • Interpreted vs. Compiled Program Execution
  • A Simple C Program
  • Names Using Extended Character Sets
  • C Statements and Statement Blocks
  • Creating an Executable from Your Source Files
  • C Source Characters
  • Whitespace in Statements
  • Procedural and Object-Oriented Programming

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Basic Ideas - A Simple C Program


    (Page 3 of 9 )

    Let’s take a look at a very simple C++ program and find out what its constituents are. You don’t need to enter this code right now; it’s just here so that you can get a feel for what goes into making up a program. I don’t go into all the details at the moment either, as everything that appears here will be explored at length in later chapters. Figure 1-2 illustrates a simple C++ program.


    Figure 1-2.  A simple C++ program

    The program shown in Figure 1-2 displays the following message:

    ============================================================
    The best place to start is at the beginning
    ============================================================

    This isn’t a very useful program, but it serves to demonstrate a few points. The program consists of a single function, main(). A function is a self-contained block of code that’s referenced by a name, main in this case. There may be a lot of other code in a program, but every C++ application consists of at least the function main(). There can be only one function called main() within a program, and execution of a C++ program always starts with the first statement in main().

    The first line of the function is

    int main()

    which identifies that this is the start of a function with the name main. The int at the beginning indicates that this function will return an integer value when it finishes executing. Because it’s the function main(), the value will be received by the operating system that calls it in the first place.

    This function main() contains two executable statements, each on a separate line:

     cout << "The best place to start is at the beginning";
     return 0;

    These two statements are executed in sequence. In general, the statements in a function are always executed sequentially, unless there’s a statement that specifically alters the sequence of execution. You’ll see what sorts of statements can do that in Chapter 4.

    In C++, input and output are preferably performed using streams. If you want to output something from a program, you put it into an output stream, and when you want something to be input, you get it from an input stream. A stream is thus an abstract representation of a source of data, or a data sink. When your program executes, each stream is tied to a specific device that is the source of data in the case of an input stream and the destination for data in the case of an output stream. The advantage of having an abstract representation of a source or sink for data is that the programming is the same regardless of what the stream actually represents. You can read data from a disk file in essentially the same way as you read from the keyboard, for instance. The standard output and input streams in C++ are called cout and cin, and by default they correspond to your computer’s screen and keyboard, respectively.

    The first line of code in main() outputs the character string “The best place to start is at the beginning” to your screen by placing it in the output stream, cout, using the insertion operator,<<. When we come to write programs that involve input, you’ll see its partner, the extraction operator, >>.

    A header contains code defining a set of standard facilities that you can include in a program source file when required. The facilities provided by the C++ standard library are stored in headers, but headers aren’t exclusively for that. You’ll create your own header files containing your own code. The name cout referred to in this program is defined in the header iostream. This is a standard header that provides the definitions necessary for you to use the standard input and output facilities in C++. If your program didn’t include the following line:

    #include <iostream>

    then it wouldn’t compile, because the <iostream> header contains the definition of cout, and without it the compiler can’t know what cout is. This line is an example of what is called a preprocessing directive, which you’ll investigate in depth later in the book. The effect of the #include is to insert the contents of the <iostream> header into your program source file at the point where the directive appears. This is done before your program is compiled.


    TIP  Note that there are no spaces between the angled brackets and the standard header name.With many compilers, spaces are significant between the two angled brackets, < and >; if you insert any spaces here, the program may not compile.

    The second and final statement in the body of the function name is

     return 0;

    This ends the program and returns control to your operating system. It also returns the value zero to the operating system. Other values can be returned to indicate different end conditions for the program and can be used by the operating system to determine if the program executed successfully. Typically, zero indicates a normal end to a program, and any nonzero value indicates an abnormal end. However, whether or not a nonzero return value can be acted upon will depend on the operating system concerned.

    Names

    Lots of things in a C++ program have names that are used to refer to them. Such names are also referred to as identifiers. There are five kinds of things that you’ll give names to in your C++ programs:

    • Functions are self-contained, named blocks of executable code. Chapter 8 goes into detail on how to define these.

    • Variables are named areas in memory that you use to store items of data. You’ll start with these in Chapter 2.

    • Types are names for the kinds of data that you can store. The type int, for example, is used for integers (whole numbers). You’ll see something on these in Chapter 2 and more in subsequent chapters, particularly Chapter 11.

    • Labels provide a means of referring to a particular statement. These are rarely used, but you’ll look at them in action in Chapter 4.

    • Namespaces are a way of gathering a set of named items in your program under a single name. If that sounds confusing, don’t worry—I’ll say more about them shortly, and you’ll look at them again in Chapter 10.

    In C++, you can construct a name using the upper- and lowercase Latin letters a to z and A to Z, the underscore character (_), and the digits 0 to 9. The ANSI standard for C++ also permits Universal Character Set (UCS) characters to be included in a name for reasons I cover in a moment.

    The ANSI standard allows names to be of any length, but typically a particular compiler will impose some sort of length limit. However, this is normally sufficiently large (several thousand characters) that it doesn’t represent a serious constraint.

    Whitespace is the term used in C++ to refer to spaces, vertical and horizontal tabs, and newline and form-feed characters. You must not put whitespace characters in the middle of a name. If you do, the single name won’t be seen by the compiler as such; it will be seen as two or more names, and therefore it won’t be processed correctly. Another restriction is that names may not begin with a digit.

    Here are some examples of legal names:

    value2  Mephistopheles  BettyMay  Earth_Weight  PI

    Here are some names that aren’t legal:

    8Ball   Mary-Ann   Betty+May    Earth-Weight   2PI


    CAUTION
    Note that names that contain a double underscore (_ _) or start with an underscore followed by an uppercase letter are reserved for use by the C++ standard library, so you shouldn’t choose such names for use in your programs.Your compiler probably won’t check for this, so you’ll only find out that you have a conflicting name when things go wrong!

    This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN  1590592271). Check it out at your favorite bookstore today. Buy this book now.

    More Practices Articles
    More By Apress Publishing


     

       

    PRACTICES ARTICLES

    - More Techniques for Finding Things
    - Finding Things
    - Finishing the System`s Outlines
    - The System in So Many Words
    - Basic Data Types and Calculations
    - What`s the Address? Pointers
    - Design with ArgoUML
    - Pragmatic Guidelines: Diagrams That Work
    - Five-Step UML: OOAD for Short Attention Span...
    - Five-Step UML: OOAD for Short Attention Span...
    - Introducing UML: Object-Oriented Analysis an...
    - Class and Object Diagrams
    - Class Relationships
    - Classes
    - Basic Ideas




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway