Home arrow C++ arrow Page 7 - Programming in 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
  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

Programming in C - Functions and Modular Programming
(Page 7 of 8 )


The word “function” has appeared a few times so far in this chapter with reference to main(), printf(), function body, and so on. Let’s explore in a little more depth what functions are and why they’re important.

Most programming languages, including C, provide a way of breaking up a program into segments, each of which can be written more or less independently of the others. In C these segments are called functions. The program code in the body of one function is completely insulated from that of other functions. A function will have a specific interface to the outside world in terms of how information is transferred to it and how results generated by the function are transmitted back from it. This interface is specified in the first line of the function, where the function name appears.

Figure 1-3 shows a simple example of a program to analyze baseball scores that is composed of four modules.

Figure 1-3.  Modular programming

Each of the four modules does a specific, well-defined job. Overall control of the sequence of operations in the program is managed by one module,main(). There is a module to read and check the input data and another module to do the analysis. Once the data has been read in and analyzed, a fourth module has the task of outputting the team and player rankings.

Segmenting a program into manageable chunks is a very important aspect to programming, so let’s go over the reasons for doing this:

  • Dividing the program into a number of separate functions allows each function to be written and tested separately. This greatly simplifies the process of getting the total program to work.
  • Several separate functions are easier to handle and understand than one huge function.
  • Libraries are just sets of functions that people tend to use all the time. Because they’ve been prewritten and pretested, you know they’ll work, so you can use them without worrying about their code details. This will accelerate your program development, by allowing you to concentrate on your own code, and it’s a fundamental part of the philosophy of C. The richness of the libraries greatly amplifies the power of the language.
  • You can accumulate your own libraries of functions that are applicable to the sorts of programs that you’re interested in. If you find yourself writing a particular function frequently, you can write a generalized version of it to suit your needs and build this into your own library. Then, whenever you need to use that particular function, you can simply use your library version.
  • In the development of very large programs, which can vary from a few thousand to millions of lines of code, development can be undertaken by teams of programmers, with each team working with a defined subgroup of the functions that make up the whole program.

You’ll learn about C functions in greater detail in Chapter 8. Because the structure of a C program is inherently functional, you’ve already encountered one of the standard library functions in one of this chapter’s earliest examples: the functionprintf().


Try It Out: Exercising What You Know

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
A plane?
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 theprintf()function, and it demonstrates that output to the screen is controlled by what you pass to theprintf()function. Let’s look at this program in detail.

You include thestdio.hfile 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#. Thestdio.hfile provides the definitions you need to be able to use theprintf()function.

You then define the start of the functionmain()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 functionprintf()to outputHi there!to your display screen, followed by two blank lines and the phraseThis program is a bit.

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

The two blank lines are produced by the three\nescape sequences. Each of these starts a new line when the characters are written to the display. The first ends the line containingHi there!, and the next two produce the two empty lines. The textThis program is a bitappears 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 nextprintf()starts at the character position immediately following the last character in the previous output. The next statement outputs the textlonger 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 thetinbit. This means that you really do need the space at the beginning of the text, otherwise the computer will displayThis 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\nat 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\nescape 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:



blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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