C++
  Home arrow C++ arrow Page 5 - 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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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 / 34
    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

    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

    Programming in C - The Body of a Function


    (Page 5 of 8 )

    The general structure of the functionmain()is illustrated in Figure 1-2.


    Figure 1-2.  Structure of the function main()

    The function body is the bit between the opening and closing braces that follow the line where the function name appears. The function body contains all the statements that define what the function does. The example’s functionmain()has a very simple function body consisting of just one statement:

     

    {

    /* This marks the beginning of main() */

     

    printf("Beware the Ides of March!");

    /* This line displays a quotation

    */

    }

     

    /* This marks the end of main()

    */

    Every function must have a body, although the body can be empty and just consist of the two braces without any statements between them. In this case, the function will do nothing.

    You may wonder where the use is for a function that does nothing. Actually, this can be very useful when you’re developing a program that will have many functions. You can declare the set of (empty) functions that you think you’ll need to write to solve the problem at hand, which should give you an idea of the programming that needs to be done, and then gradually create the program code for each function. This technique helps you to build your program in a logical and gradual manner.


    NOTE 
    You can see that I’ve aligned the braces one below the other. I’ve done this to make it clear where the block of statements that the braces enclose starts and finishes. Statements between braces are usually indented by a fixed amount—usually two or more spaces so that the braces stand out. This is good programming style, as the statements within a block can be readily identified.

    Outputting Information

    The body of the example’s functionmain()includes only one statement, which calls theprintf()function:

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

    As I said,printf()is a standard library function, and it outputs information to the display screen based on what appears between the parentheses that immediately follow the function name. In this case, the call to the function displays a simple piece of Shakespearean advice that appears between the double quotes. Notice that this line does end with a semicolon.

    Arguments

    Items enclosed between the parentheses following a function name, as with theprintf()function, are called arguments. When there is more than one argument to a function, they must be separated by commas.

    If you don’t like the quotation in this example’s argument, you could display something else by simply including your own choice of words, within double quotes, between the parentheses. For instance, you might prefer a line from Macbeth:

    printf("Out, damned Spot! Out I say!");

    Try using this in the example. When you’ve modified the source code, you need to compile and link the program again before executing it.


    NOTE 
    As with all executable statements in C (as opposed to defining or directive statements) theprintf()line must have a semicolon at the end. As you’ve seen, a very common error, particularly when you first start programming in C, is to forget the semicolon.

    Control Characters

    You could alter the program to display two sentences on separate lines. Try typing in the following code:

    /* Program 1.4 Another Simple C Program - Displaying a Quotation */
    #include <stdio.h>
    void main()
    {
     
    printf("\nMy formula for success?\nRise early, work late, strike oil.");
    }

    The output from this program looks like this:

    ----------------------------------------------------------------------
    My formula for success?
    Rise early, work late, strike oil.
    --------------------------------------------

    Look at theprintf()statement. At the beginning of the text and after the first sentence, you insert the characters\n. The combination\nactually represents one character: a newline character.

    The backslash (\) is of special significance in a text string. It indicates the start of an escape sequence. Escape sequences are used to insert characters in a string that would otherwise be impossible to specify, such as tab and newline, or would confuse the compiler, such as a double quote that you use to delimit a string. The character following the backslash indicates what character the escape sequence represents. In this case, it’snfor newline, but there are plenty of other possibilities. Obviously, if a backslash is of special significance, you need a way to specify a backslash in a text string. To do this, you simply use two backslashes:\\. Similarly, if you actually want to display a double quote character, you can use\".

    Type in the following program:

    /* Program 1.5 Another Simple C Program - Displaying Great Quotations */
    #include <stdio.h>
    void main()
    {
      printf("\n\"It is a wise father that knows his own child.\"
             Shakespeare");
    }

    The output displays the following text:

    ----------------------------------------------------------------------
    "It is a wise father that knows his own child." Shakespeare
    ----------------------------------------------------------------------

    You can use the\aescape sequence in an output string to sound a beep to signal something interesting or important. Enter and run the following program:

    /* Program 1.6 A Simple C Program–Important */
    #include <stdio.h>
    void main()
    {
      printf("\nBe careful!!\a");
    }

    The output of this program is sound and vision. Listen closely and you should hear a beep through the speaker in your computer.

    ----------------------------------------------------------------------
    Be careful!!
    --------------------------------------------

    The\asequence represents the “bell” character. Table 1-1 shows a summary of the escape sequences that you can use.

    Table 1-1. Escape Sequences

    Escape Sequence

    Description

    \n

    Represents a newline character

    \r

    Represents a carriage return

    \b

    Represents a backspace

    \f

    Represents a form-feed character

    \t

    Represents a horizontal tab

    \v

    Represents a vertical tab

    \a

    Inserts a bell (alert) character

    \"

    Inserts a double quote (")

    \'

    Inserts a single quote (')

    \\

    Inserts a backslash (\)

    Try displaying different lines of text on the screen and alter the spacing within that text. You can put words on different lines using\n, and you can use\tto space the text. You’ll get lot more practice with these as you progress through the book.

    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
     

    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

    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection
    - Temporary Variables: Chasing Temporaries Away







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