C++
  Home arrow C++ arrow Page 3 - 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 - Editing Your First Program


    (Page 3 of 8 )

    You could try altering the same program to display something else on the screen. For example, you might want to try editing the program to read like this:

    /* Program 1.2 Your Second C Program */
    #include<stdio.h>
    void main()
    {
      printf("If at first you don\'t succeed, try, try, try again!");
    }

    The\'sequence in the middle of the text to be displayed is called an escape sequence. Here it’s a special way of including a single quote in the text. Because single quotes are used to indicate where a character constant begins and ends, you need a special way to indicate when you mean a single quote and not the start of a character constant. You’ll learn more about escapes sequences very soon. You can try recompiling the program, relinking it, and running it again once you’ve altered the source. With a following wind, and a bit of luck, you’ve now edited your first program. You’ve written a program using the editor, edited it, and then compiled, linked, and executed it.

    Dealing with Errors

    To err is human, so there’s no need to be embarrassed about making mistakes. Fortunately, computers don’t generally make mistakes themselves and they’re actually very good at indicating where we’ve slipped up. Sooner or later your compiler is going to present you with a list (and sometimes a list that’s longer than you want) of the mistakes that are in your source code. You’ll usually get an indication of the statements that are in error. When this happens, you must return to the editing stage, find out what’s wrong with the incorrect code, and fix it. Keep in mind that one error can result in error messages for subsequent statements that may actually be correct. This usually happens with statements that refer to something that is supposed to be defined by a statement containing an error. Of course, if a statement that defines something has an error, then what was supposed to be defined won’t be.

    Let’s step through what happens when your source code is incorrect by creating an error in a program. Edit your second program example, removing the semicolon (;) at the end of the line withprintf()in it, as shown here:

    /* Program 1.2 Your Second C Program */ #include<stdio.h>
    void main()
    {
      printf("If at first you don\'t succeed, try, try, try again!")
    }

    If you now try to compile this program, you’ll see an error message that will vary slightly depending on which compiler you’re using. A typical error message is as follows:

    ----------------------------------------------------------------------Syntax error : missing ';' before '}'
    HELLO.C - 1 error(s), 0 warning(s)
    ----------------------------------------------------------------------

    Here, the compiler is able to determine precisely what the error is, and where. There really should be a semicolon at the end of thatprintf()line. As you start writing your own programs, you’ll probably get lots of errors during compilation that are caused by simple punctuation mistakes. It’s so easy to forget a comma or a bracket, or to just press the wrong key. Don’t worry about this: lots of experienced programmers make exactly the same mistakes—even after years of practice.

    As I said earlier, just one mistake can sometimes result in a whole stream of abuse from your compiler, as it throws you a multitude of different things that it doesn’t like. Don’t get put off by the number of errors reported. After you consider the messages carefully, the basic approach is to go back and edit your source code to fix what you can, ignoring the errors that you can’t understand. Then try to compile the source file again. With luck, you’ll get fewer errors the next time around.

    To correct your example program, just go back to your editor and reenter the semicolon. Recompile and check for any other errors, and your program is fit to be run again.

    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 3 hosted by Hostway
    Stay green...Green IT