Home arrow C++ arrow Page 8 - 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 - Common Mistakes
(Page 8 of 8 )

Mistakes are a fact of life. When you write a computer program in C, the compiler must convert your source code to machine code, so there must be some very strict rules governing how you use the language. Leave out a comma where one is expected, or add a semicolon where you shouldn’t, and the compiler won’t be able to translate your program into machine code.

You’ll be surprised just how easy it is to introduce typographical errors into a program, even after years of practice. If you’re lucky, these errors will be picked up when you compile or link your program. If you’re really unlucky, they can result in your program apparently working fine but producing some intermittent erratic behavior. You can end up spending a lot of time tracking these errors down.

Of course, it’s not only typographical errors that cause problems. You’ll often find that your detailed implementation is just not right. Where you’re dealing with complicated decisions in your program, it’s easy to get the logic wrong. Your program may be quite accurate from a language point of view, and it may compile and run without a problem, but it won’t produce the right answers. These kinds of errors can be the most difficult to find.

Points to Remember

It would be a good idea to review what you’ve gleaned from your first program. You can do this by looking at the overview of the important points in Figure 1-4.

Summary

You’ve reached the end of the first chapter, and you’ve already written a few programs in C. You’ve covered quite a lot of ground, but at a fairly gentle pace. The aim of this chapter was to introduce a few basic ideas rather than teach you a lot about the C programming language. You should be confident about editing, compiling, and running your programs. You probably have only a vague idea about how to construct a C program at this point. It will become much clearer when you’ve learned a bit more about C and written some programs with more meat to them.

In the next chapter, you’ll move on to more complicated things than just producing text output using theprintf()function. You’ll manipulate information and get some rather more interesting results. And by the way, theprintf()function does a whole lot more than just display text strings—as you’ll see soon.

Exercises

The following exercises enable you to try out what you’ve learned in this chapter. If you get stuck, look back over the chapter for help. If you’re still stuck, you can download the solutions from the Downloads area of the Apress website (http://www.apress.com), but that really should be a last resort.


Figure 1-4.  Elements of a simple program

Exercise 1-1. Write a program that will output your name and address using a separate printf()statement for each line of output.

Exercise 1-2. Modify your solution for the previous exercise so that it produces all the output using only one printf() statement.

Exercise 1-3. Write a program to output the following text, exactly as it appears here:

--------------------------------------------------
"It's freezing in here,"  he said coldly.
--------------------------------------------------


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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