SunQuest
 
       Code Examples
  Home arrow Code Examples arrow Page 2 - Programming in C
Click Here
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Programming in C
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2005-03-02

    Table of Contents:
  • Programming in C
  • Linking
  • Try It Out: An Example C Program
  • Dealing with Errors
  • Keywords
  • Developing Programs in C
  • Functions and Modular Programming
  • Try It Out: Exercising What You Know
  • Common Mistakes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Programming in C - Linking


    (Page 2 of 9 )

    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 .exe extension; 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 the cc command, 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 .o extension 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 called a.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 module myprog.c and create an executable file called myprog (defined straight after the -o flag).

    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 .exe file 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.

    Creating Your First Program
    Figure 1-1.  Creating and executing a program

    It’s time to create your first program. Let’s step through the processes of creating a simple C program, from entering the program itself to executing the program. Don’t worry if what you type doesn’t mean anything to you at this stage—I’ll explain everything as we go along. 

    This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now.

    More Code Examples Articles
    More By Apress Publishing


       · Has anyone looked over this article before posting? Both command line examples and...
       · The code was correct, the CMS's editor converted the <stdio.h> into HTML and erased...
     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks

    IBM developerWorks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway