Home arrow C++ arrow Page 4 - 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 - Dissecting a Simple Program
(Page 4 of 8 )

Now that youíve written and compiled your first program, letís go through another thatís very similar and see what the individual lines of code do. Have a look at this program:

/* Program 1.3 Another Simple C Program - Displaying a Quotation */
#include <stdio.h>
void main()
{
 
printf("Beware the Ides of March!");
}

This is virtually identical to your first program. Even so, you could do with the practice, so use your editor to enter this example and see what happens when you compile and run it. If you type it in accurately, compile it, and run it, you should get the following output:

----------------------------------------------------------------------
Beware the Ides of March!
--------------------------------------------

Comments

Look at the first line of code in the preceding example:

/* Program 1.3 Another Simple C Program - Displaying a Quotation */

This isnít actually part of the program code, in that it isnít telling the computer to do anything. Itís simply a comment, and itís there to remind you, or someone else reading your code, what the program does. Anything between/*and*/is treated as a comment. As soon as your compiler finds/*in your source file, it will simply ignore anything that follows until it finds the matching*/that marks the end of the comment. This may be on the same line, or it can be several lines further on. Whatever is between/*and*/will be completely ignored by the compiler, even if it looks like program code.

You should try to get into the habit of documenting your programs, using comments as you go along. Your programs will, of course, work without comments, but when you write longer programs you may not remember what they do or how they work. Put in enough comments to ensure that, a month from now, you (and any other programmer) can understand the aim of the program and how it works.

As I said, comments donít have to be in a line of their own. A comment is everything between/*and*/, wherever/*and*/are in your code. Letís add some more comments to the program:

/* Program 1.3 Another Simple C Program - Displaying a Quotation */

#include <stdio.h>

/* This is a preprocessor directive

*/

void main()

/* This identifies the function main() */

{

/* This marks the beginning of main()

*/

 

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

*/

}

/* This marks the end of main()

*/

 

You can see that using comments can be a very useful way of explaining whatís going on in the program. You can place comments wherever you want in your program, and you can use them to explain the general objectives of the code as well as the specifics of how the code works. You can also use comments to identify the author of the code and to assert your copyright if you wish.

Preprocessing Directives

Look at the following line of code:

#include <stdio.h>           /* This is a preprocessing directive */

This isnít strictly part of the executable program, but it is essential in this caseóin fact, the program wonít work without it. The symbol#indicates this is a preprocessing directive, which is an instruction to your compiler to do something before compiling the source code. The compiler handles these directives during an initial preprocessing phase before the compilation process starts. There are quite a few preprocessing directives, and theyíre usually placed at the beginning of the program source file.

In this case, the compiler is instructed to ďincludeĒ in your program the contents of the filestdio.h. This file is called a header file, because itís usually included at the head of a program. It defines information about some of the functions that are provided by the standard C library. The header file will contain C source code and other preprocessor directives. In this case, as youíre using theprintf()function from the standard library, you have to include thestdio.hheader file. This is becausestdio.hcontains the information that the compiler needs to understand whatprintf()means, as well as other functions that deal with input and output. All header files in C have file names with the extension.h. Youíll use other C header files later in the book.


NOTE 
Itís common practice to write the header file names in the#includedirective in lowercase letters.

Every C compiler that conforms to the American National Standards Institute (ANSI) standard for the language will have a set of standard header files supplied with it. Header files primarily contain definitions relating to standard library functions that are available with C. Although all ANSI standard C compilers will support the same set of standard library functions and will have the same set of standard header files available, there may be extra library functions provided with a particular compiler that may not be available with other compilers.

Defining the main() Function

The next four statements define the functionmain():

void main()

/* This identifies the function main() */

{

/* This marks the beginning of main()

*/

 

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

*/

}

/* This marks the end of main()

*/

A function is just a named block of code between braces that carries out some specific set of operations. Every C program consists of one or more functions, and every C program must contain a function called      main()óthe reason being that a program will always start execution from the beginning of this function. So imagine that youíve created, compiled, and linked a file calledprogname.exe. When you execute this program, it causes the functionmain()for the program to be called.

The first line of the definition for the functionmain()is as follows:

void main()           /* This identifies the function main() */

This defines the start of the functionmain(). Notice that there is no semicolon at the end of the line. The first line identifying this as the functionmain()has the keywordvoidat the beginning. What appears here defines the type of value to be returned by the function. The keywordvoidsignifies that the functionmain()returns no value.

There are circumstances in which you would want to return something frommain()to the operating systemóan error code, for example. In those situations, a keyword other thanvoidwould be used. Iíll cover this in a later chapter.

The parentheses that immediately follow the name of the function,main, enclose a definition of what information is to be transferred tomain()when it starts executing. In this example, however, you can see that thereís nothing between the parentheses, so no information can be transferred. Later, youíll see how information is transferred tomain()and to other functions in a program. In general, the functionmain()can call other functions that, in turn, may call further functions, and so on. For every function thatís called, you have the opportunity to pass some information to it within the parentheses that follow its name.

Keywords

In C, a keyword is a word with special significance, so you shouldnít use keywords for any other purpose in your program. For this reason, keywords are also referred to as reserved words. In the preceding example,voidis a keyword. C has several keywords, and youíll become familiar with more of them as you learn more of the language. Youíll find a complete list of C keywords in Appendix C.


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