C++
  Home arrow C++ arrow Page 4 - Programming in C
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
C++

Programming in C
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 35
    2005-10-27

    Table of Contents:
  • Programming in C
  • Creating Your First Program
  • Editing Your First Program
  • Dissecting a Simple Program
  • The Body of a Function
  • Developing Programs in C
  • Functions and Modular Programming
  • Common Mistakes

  • 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


    Programming in C - Dissecting a Simple Program


    (Page 4 of 8 )

    Now that you’ve written and compiled your first program, let’s go through another that’s very similar and see what the individual lines of code do. Have a look at this program:

    /* Program 1.3 Another Simple C Program - Displaying a Quotation */
    #include <stdio.h>
    void main()
    {
     
    printf("Beware the Ides of March!");
    }

    This is virtually identical to your first program. Even so, you could do with the practice, so use your editor to enter this example and see what happens when you compile and run it. If you type it in accurately, compile it, and run it, you should get the following output:

    ----------------------------------------------------------------------
    Beware the Ides of March!
    --------------------------------------------

    Comments

    Look at the first line of code in the preceding example:

    /* Program 1.3 Another Simple C Program - Displaying a Quotation */

    This isn’t actually part of the program code, in that it isn’t telling the computer to do anything. It’s simply a comment, and it’s there to remind you, or someone else reading your code, what the program does. Anything between/*and*/is treated as a comment. As soon as your compiler finds/*in your source file, it will simply ignore anything that follows until it finds the matching*/that marks the end of the comment. This may be on the same line, or it can be several lines further on. Whatever is between/*and*/will be completely ignored by the compiler, even if it looks like program code.

    You should try to get into the habit of documenting your programs, using comments as you go along. Your programs will, of course, work without comments, but when you write longer programs you may not remember what they do or how they work. Put in enough comments to ensure that, a month from now, you (and any other programmer) can understand the aim of the program and how it works.

    As I said, comments don’t have to be in a line of their own. A comment is everything between/*and*/, wherever/*and*/are in your code. Let’s add some more comments to the program:

    /* Program 1.3 Another Simple C Program - Displaying a Quotation */

    #include <stdio.h>

    /* This is a preprocessor directive

    */

    void main()

    /* This identifies the function main() */

    {

    /* This marks the beginning of main()

    */

     

    printf("Beware the Ides of March!"); /* This line displays a quotation

    */

    }

    /* This marks the end of main()

    */

     

    You can see that using comments can be a very useful way of explaining what’s going on in the program. You can place comments wherever you want in your program, and you can use them to explain the general objectives of the code as well as the specifics of how the code works. You can also use comments to identify the author of the code and to assert your copyright if you wish.

    Preprocessing Directives

    Look at the following line of code:

    #include <stdio.h>           /* This is a preprocessing directive */

    This isn’t strictly part of the executable program, but it is essential in this case—in fact, the program won’t work without it. The symbol#indicates this is a preprocessing directive, which is an instruction to your compiler to do something before compiling the source code. The compiler handles these directives during an initial preprocessing phase before the compilation process starts. There are quite a few preprocessing directives, and they’re usually placed at the beginning of the program source file.

    In this case, the compiler is instructed to “include” in your program the contents of the filestdio.h. This file is called a header file, because it’s usually included at the head of a program. It defines information about some of the functions that are provided by the standard C library. The header file will contain C source code and other preprocessor directives. In this case, as you’re using theprintf()function from the standard library, you have to include thestdio.hheader file. This is becausestdio.hcontains the information that the compiler needs to understand whatprintf()means, as well as other functions that deal with input and output. All header files in C have file names with the extension.h. You’ll use other C header files later in the book.


    NOTE 
    It’s common practice to write the header file names in the#includedirective in lowercase letters.

    Every C compiler that conforms to the American National Standards Institute (ANSI) standard for the language will have a set of standard header files supplied with it. Header files primarily contain definitions relating to standard library functions that are available with C. Although all ANSI standard C compilers will support the same set of standard library functions and will have the same set of standard header files available, there may be extra library functions provided with a particular compiler that may not be available with other compilers.

    Defining the main() Function

    The next four statements define the functionmain():

    void main()

    /* This identifies the function main() */

    {

    /* This marks the beginning of main()

    */

     

    printf("Beware the Ides of March!"); /* This line displays a quotation

    */

    }

    /* This marks the end of main()

    */

    A function is just a named block of code between braces that carries out some specific set of operations. Every C program consists of one or more functions, and every C program must contain a function called      main()—the reason being that a program will always start execution from the beginning of this function. So imagine that you’ve created, compiled, and linked a file calledprogname.exe. When you execute this program, it causes the functionmain()for the program to be called.

    The first line of the definition for the functionmain()is as follows:

    void main()           /* This identifies the function main() */

    This defines the start of the functionmain(). Notice that there is no semicolon at the end of the line. The first line identifying this as the functionmain()has the keywordvoidat the beginning. What appears here defines the type of value to be returned by the function. The keywordvoidsignifies that the functionmain()returns no value.

    There are circumstances in which you would want to return something frommain()to the operating system—an error code, for example. In those situations, a keyword other thanvoidwould be used. I’ll cover this in a later chapter.

    The parentheses that immediately follow the name of the function,main, enclose a definition of what information is to be transferred tomain()when it starts executing. In this example, however, you can see that there’s nothing between the parentheses, so no information can be transferred. Later, you’ll see how information is transferred tomain()and to other functions in a program. In general, the functionmain()can call other functions that, in turn, may call further functions, and so on. For every function that’s called, you have the opportunity to pass some information to it within the parentheses that follow its name.

    Keywords

    In C, a keyword is a word with special significance, so you shouldn’t use keywords for any other purpose in your program. For this reason, keywords are also referred to as reserved words. In the preceding example,voidis a keyword. C has several keywords, and you’ll become familiar with more of them as you learn more of the language. You’ll find a complete list of C keywords in Appendix C.

    More C++ Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning C, third edition", published by...
       · I need a C program for character stuffing.
       · source code for characterstuffing in c
       · i want a program on character stuffing using files
     

    Buy this book now. This article is excerpted from the book Beginning C, third edition, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out today at your favorite bookstore. Buy this book now.

    C++ ARTICLES

    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class
    - Iostream Library and Basic I/O in C++
    - Introduction to Streams







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT