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

C IS A POWERFUL AND COMPACT computer language that allows you to write programs that specify exactly what you want your computer to do. Youíre in charge: you create a program, which is just a set of instructions, and your computer will follow those instructions.

Programming in C isnít difficult, as youíre about to find out. Iím going to teach you all the fundamentals of C programming in an enjoyable and easy-to-understand way, and by the end of this chapter, youíll have written your first few C programs. Itís as easy as that!

In this chapter youíll learn

  1. How to create C programs
  2. How C programs are organized
  3. How to write your own program to display text on the screen

Creating C Programs

There are four fundamental stages, or processes, in the creation of any C program:

  1. Editing
  2. Compiling
  3. Linking
  4. Executing

Youíll soon know all these processes like the back of your hand (youíll be doing them so easily and so often), but first letís consider what each process is and how it contributes to the creation of a C program.

Editing

This is the process of creating and editing C source codeóthe name given to the program instructions you write. Some C compilers come with a specific editor that can provide a lot of assistance in managing your programs. In fact, an editor often provides a complete environment for writing, managing, developing, and testing your programs. This is sometimes called an integrated development environment, or IDE.

You can also use other editors to create your source files, but they must store the code as plain text without any extra formatting data embedded in it. In general, if you have a compiler system with an editor included, then it will provide a lot of features that make it easier to write and organize your source programs. There will usually be automatic facilities for laying out the program text appropriately and color highlighting for important language elements, which not only makes your code more readable, but also provides a clear indicator when you make errors in keying in such words.

If youíre working in UNIX, then the most common text editor is the vi editor. Alternately, you might prefer to use theemacs editor.

From a PC, you could use one the many freeware and shareware programming editors. These will often provide a lot of help in ensuring your code is correct with syntax highlighting and autoindenting of your code. Donít use word processors such as Microsoft Word, as they arenít suitable for producing program code.

Compiling

The compiler converts your source code into machine language, and detects and reports errors in the compilation process. The input to this stage is the file you produced during your editing, which is usually referred to as a source file.

The compiler can detect a wide range of errors that are due to invalid or unrecognized program code, as well as structural errors where, for example, part of a program can never be executed. The output from the compiler is known as object code and is stored in files called object files, which usually have names with the extension.obj. The compiler can detect several different kinds of errors during the translation process, and most of these will prevent the object file from being created.


NOTE  
In UNIX, object files have the extension.o.

The result of a successful compilation is a file with the same name that you used for the source file, but with the.objextension.

 If youíre working in UNIX, then the standard command to compile your C programs will be cc. You can use it like this:

cc -c myprog.c

wheremyprog.c is the program you want to compile. Note that if you omit the-oflag, your program will automatically be linked as well. If youíre using the GNUís Not UNIX (GNU) compiler, you should type

gcc -c myprog.c.

The result of a successful compilation will be an object file.

Most C compilers will have a standard compile option, whether itís from the command line (such ascc myprog.c) or a menu option from within an IDE (where youíll find a Compile menu option).

Linking

The linker combines the various modules generated by the compiler from source code files, adds required code modules from program libraries supplied as part of C, and welds everything into an executable whole. The linker can also detect and report errorsófor example, if part of your program is missing or a nonexistent library component is referenced.

In practice, if your program is of any significant size, it will consist of several separate source code files, which can then be linked. A large program may be difficult to write in one session. By breaking it up into a number of smaller source files, you can make the development of the program a whole lot easier. The source files can be compiled separately, which makes eliminating simple typographical errors a bit easier. Furthermore, the whole program can usually be developed incrementally. Each source file will have its own file name, and the set of source files that make up the program will usually be integrated under a project name, which is used to refer to the whole program.

Program libraries support and extend the C language by providing routines to carry out operations that arenít part of the language. For example, libraries contain routines that support operations such as performing input and output, calculating a square root, comparing two character strings, or obtaining date and time information.

A failure during the linking phase means that, once again, you have to go back and edit your source code. Success, on the other hand, will produce an executable file. In a Microsoft Windows environment, this executable file will have the.exeextension; in UNIX, there will be no such extension, but the file will be of an executable type.

In UNIX, the modules that are to be linked are given together with thecccommand, for example:

cc myprog.c mod1.c mod2.o

These three modules will be linked. Notice that the last module here has the extension.o. This is because it has previously been compiled: the.oextension tells the compiler that the module is waiting to be linked and doesnít need to be compiled again. The output of this stage is a file calleda.out, which you should then rename to something more meaningful.

An alternative form to this is as follows:

cc -o myprog myprog.c mod.c mod2.o

This will compile and link the modulemyprog.cand create an executable file calledmyprog(defined straight after the-oflag).

Many C compilers also have a Build option, which will compile and link your program in one step. This option will usually be found, within an IDE, in the Compile menu; alternatively, it may have a menu of its own.

Executing

The execution stage is when you run your program, having completed all the previous processes successfully. Unfortunately, this stage can also generate a wide variety of error conditions, ranging from producing the wrong output to sitting there and doing nothing, perhaps crashing your computer for good measure. In all cases, itís back to the editing process to check your source code.

Now for the good news: this is the stage where, at last, you get to see your computer doing exactly what you told it to do!

In UNIX and DOS, to execute a program you just enter the name of the file that has been compiled and linked.

In most IDEs, youíll find an appropriate menu command that allows you to Run or Execute your compiled program. This option may have a menu of its own, or you may find it under the Compile menu option.

In Windows, you can use Windows Explorer to locate the.exefile of your program, and then you can double-click the file.

The processes of editing, compiling, linking, and executing are essentially the same for developing programs in any environment and with any compiled language. Figure 1-1 summarizes how you would typically pass through each of these processes as you create your own C programs.


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-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials