Home arrow C++ arrow Page 3 - Programming in C
C++

Programming in C


If you want to learn the fundamentals of C programming, you've come to the right place. This article is excerpted from the book Beginning C, third edition, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 45
October 27, 2005
TABLE OF CONTENTS:
  1. · Programming in C
  2. · Creating Your First Program
  3. · Editing Your First Program
  4. · Dissecting a Simple Program
  5. · The Body of a Function
  6. · Developing Programs in C
  7. · Functions and Modular Programming
  8. · Common Mistakes

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials