Home arrow C++ arrow Page 5 - 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 - The Body of a Function
(Page 5 of 8 )

The general structure of the functionmain()is illustrated in Figure 1-2.


Figure 1-2.  Structure of the function main()

The function body is the bit between the opening and closing braces that follow the line where the function name appears. The function body contains all the statements that define what the function does. The example’s functionmain()has a very simple function body consisting of just one statement:

 

{

/* This marks the beginning of main() */

 

printf("Beware the Ides of March!");

/* This line displays a quotation

*/

}

 

/* This marks the end of main()

*/

Every function must have a body, although the body can be empty and just consist of the two braces without any statements between them. In this case, the function will do nothing.

You may wonder where the use is for a function that does nothing. Actually, this can be very useful when you’re developing a program that will have many functions. You can declare the set of (empty) functions that you think you’ll need to write to solve the problem at hand, which should give you an idea of the programming that needs to be done, and then gradually create the program code for each function. This technique helps you to build your program in a logical and gradual manner.


NOTE 
You can see that I’ve aligned the braces one below the other. I’ve done this to make it clear where the block of statements that the braces enclose starts and finishes. Statements between braces are usually indented by a fixed amount—usually two or more spaces so that the braces stand out. This is good programming style, as the statements within a block can be readily identified.

Outputting Information

The body of the example’s functionmain()includes only one statement, which calls theprintf()function:

printf("Beware the Ides of March!"); /* This line displays a quotation */

As I said,printf()is a standard library function, and it outputs information to the display screen based on what appears between the parentheses that immediately follow the function name. In this case, the call to the function displays a simple piece of Shakespearean advice that appears between the double quotes. Notice that this line does end with a semicolon.

Arguments

Items enclosed between the parentheses following a function name, as with theprintf()function, are called arguments. When there is more than one argument to a function, they must be separated by commas.

If you don’t like the quotation in this example’s argument, you could display something else by simply including your own choice of words, within double quotes, between the parentheses. For instance, you might prefer a line from Macbeth:

printf("Out, damned Spot! Out I say!");

Try using this in the example. When you’ve modified the source code, you need to compile and link the program again before executing it.


NOTE 
As with all executable statements in C (as opposed to defining or directive statements) theprintf()line must have a semicolon at the end. As you’ve seen, a very common error, particularly when you first start programming in C, is to forget the semicolon.

Control Characters

You could alter the program to display two sentences on separate lines. Try typing in the following code:

/* Program 1.4 Another Simple C Program - Displaying a Quotation */
#include <stdio.h>
void main()
{
 
printf("\nMy formula for success?\nRise early, work late, strike oil.");
}

The output from this program looks like this:

----------------------------------------------------------------------
My formula for success?
Rise early, work late, strike oil.
--------------------------------------------

Look at theprintf()statement. At the beginning of the text and after the first sentence, you insert the characters\n. The combination\nactually represents one character: a newline character.

The backslash (\) is of special significance in a text string. It indicates the start of an escape sequence. Escape sequences are used to insert characters in a string that would otherwise be impossible to specify, such as tab and newline, or would confuse the compiler, such as a double quote that you use to delimit a string. The character following the backslash indicates what character the escape sequence represents. In this case, it’snfor newline, but there are plenty of other possibilities. Obviously, if a backslash is of special significance, you need a way to specify a backslash in a text string. To do this, you simply use two backslashes:\\. Similarly, if you actually want to display a double quote character, you can use\".

Type in the following program:

/* Program 1.5 Another Simple C Program - Displaying Great Quotations */
#include <stdio.h>
void main()
{
  printf("\n\"It is a wise father that knows his own child.\"
         Shakespeare");
}

The output displays the following text:

----------------------------------------------------------------------
"It is a wise father that knows his own child." Shakespeare
----------------------------------------------------------------------

You can use the\aescape sequence in an output string to sound a beep to signal something interesting or important. Enter and run the following program:

/* Program 1.6 A Simple C Program–Important */
#include <stdio.h>
void main()
{
  printf("\nBe careful!!\a");
}

The output of this program is sound and vision. Listen closely and you should hear a beep through the speaker in your computer.

----------------------------------------------------------------------
Be careful!!
--------------------------------------------

The\asequence represents the “bell” character. Table 1-1 shows a summary of the escape sequences that you can use.

Table 1-1. Escape Sequences

Escape Sequence

Description

\n

Represents a newline character

\r

Represents a carriage return

\b

Represents a backspace

\f

Represents a form-feed character

\t

Represents a horizontal tab

\v

Represents a vertical tab

\a

Inserts a bell (alert) character

\"

Inserts a double quote (")

\'

Inserts a single quote (')

\\

Inserts a backslash (\)

Try displaying different lines of text on the screen and alter the spacing within that text. You can put words on different lines using\n, and you can use\tto space the text. You’ll get lot more practice with these as you progress through the book.


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