Code Examples
  Home arrow Code Examples arrow Page 8 - Programming in C
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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? 
CODE EXAMPLES

Programming in C
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2005-03-02

    Table of Contents:
  • Programming in C
  • Linking
  • Try It Out: An Example C Program
  • Dealing with Errors
  • Keywords
  • Developing Programs in C
  • Functions and Modular Programming
  • Try It Out: Exercising What You Know
  • 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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Programming in C - Try It Out: Exercising What You Know


    (Page 8 of 9 )

    Let’s now look at an example that puts into practice what you’ve learned so far. First, have a look at the following code and see whether you can understand what it does without running it. Then type it in; compile, link, and run it; and see what happens.

    /* Program 1.7 A longer program */

    #include <stdio.h>              /* Include the header file for input and output */

    void main()

    {
      printf("Hi there!\n\n\nThis program is a bit");
      printf(" longer than the others.");
      printf("\nBut really it's only more text.\n\n\n\a\a");
      printf("Hey, wait a minute!! What was that???\n\n");
      printf("\t1.\tA bird?\n");
      printf("\t2.\tA plane?\n");
      printf("\t3.\tA control character?\n");
      printf("\n\t\t\b\bAnd how will this look when it prints out?\n\n");

    }

    The output will be as follows:

    Hi there!


    This program is a bit longer than the others.
    But really it's only more text.

     

    Hey, wait a minute!! What was that???

     

     

    1. A bird?

    2. A plane?

    3. A control character?

    And how will this look when it prints out?

    The program looks a little bit complicated, largely because the text strings between parentheses include a lot of escape sequences. Each text string is bounded by a pair of double quotation marks. However, the program is just a succession of calls to the printf() function, and it demonstrates that output to the screen is controlled by what you pass to the printf() function. Let’s look at this program in detail.

    You include the stdio.h file through the preprocessing directive:

    #include <stdio.h>  /* Include the header file for input and output */

    You can see that this is a preprocessing directive because it begins with #. The stdio.h file provides the definitions you need to be able to use the printf() function.

    You then define the start of the function main() and specify that it doesn’t return a value with this line:

    void main()

    The opening brace on the next line indicates that the body of the function follows:

    {

    The next statement calls the standard library function printf() to output Hi there! to your display screen, followed by two blank lines and the phrase This program is a bit.

    printf("Hi there!\n\n\nThis program is a bit");

    The two blank lines are produced by the three \n escape sequences. Each of these starts a new line when the characters are written to the display. The first ends the line containing Hi there!, and the next two produce the two empty lines. The text This program is a bit appears on the fourth line of output. You can see that this one line of code produces a total of four lines of output on the screen.

    The next line of output produced by the next printf() starts at the character position immediately following the last character in the previous output.

    The next statement outputs the text longer than the others. with a space as the first character of the text:

    printf(" longer than the others.");

    This output will simply continue where the last line left off, following the t in bit. This means that you really do need the space at the beginning of the text, otherwise the computer will display This program is a bitlonger than the others, which isn’t what you want.

    The next statement starts its output on a new line, immediately following the previous line, because of the \n at the beginning of the text string between double quotation marks:

    printf("\nBut really it's only more text.\n\n\n\a\a");

    It then displays the text and adds two empty lines (because of the three \n escape sequences) and beeps twice. The next output to the screen will start at the beginning of the line that follows the second empty line produced here.

    The next output is produced by the following statement:

    printf("Hey, wait a minute!! What was that???\n\n");

    This outputs the text and then leaves one empty line. The next output will be on the line following the empty line.

    Each of the next three statements inserts a tab, displays a number, inserts another tab followed by some text, and ends with a new line. This is useful for making your output easier to read:

    printf("\t1.\tA bird?\n"); printf("\t2.\tA plane?\n"); printf("\t3.\tA control character?\n");

    This produces three numbered lines of output.

    The last statement that produces output adds a new line, so that there will be an empty line after the previous output. Two tabs are then sent to the display, followed by two backspaces, which moves your cursor back two spaces from the last tab position. Lastly, the text is displayed, and two newline characters are sent to the display:

    printf("\n\t\t\b\bAnd how will this look when it prints out?\n\n");

    The closing brace marks the end of the function body:

    }

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

    More Code Examples Articles
    More By Apress Publishing


       · Has anyone looked over this article before posting? Both command line examples and...
       · The code was correct, the CMS's editor converted the <stdio.h> into HTML and erased...
     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks

    Click Here




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