Code Examples
  Home arrow Code Examples arrow Page 3 - Programming in C
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
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Programming in C - Try It Out: An Example C Program


    (Page 3 of 9 )

    Run your editor and type in the following program exactly as it’s written. Be careful to input the punctuation exactly as you see here. The brackets used on the fourth and last lines are braces—the curly ones {}, not the square ones [] or the round ones ()—it really does matter. Also, make sure you put the slashes the right way (/), as later you’ll be using the backslash (\) as well. Don’t forget the semicolon (;).

    /* Program 1.1 Your Very First C Program - Displaying Hello World */ #include <stdio.h>

    void main() {

    printf("Hello world"); }

    When you’ve entered the preceding source code, save the program as hello.c. You can use whatever name you like instead of hello, but the extension must be .c. This extension name is the common convention when you write C programs. The extension identifies the contents of the file as C source code. Most compilers will expect the source file to have the extension .c, and if it doesn’t, the compiler may refuse to process it.

    Now you’re ready to compile your program. Exactly how you do this will depend upon which compiler you’re using. If your C compiler allows you to work in an IDE, then you should easily be able to find your way to a menu where you can select the Compile option. In UNIX, the command you would use is cc.

    Next, you’ll link all the pieces necessary to create an executable program. This will add in code from the standard libraries that your program needs. Once again, the precise way to do this will depend upon which compiler system you’re using. If you’re using UNIX, then append any modules that you need to include at the end of your cc command. In an IDE, you’ll find the Link option on one of the menus. Remember that many IDEs offer a Build option that will compile and link your program automatically. If you aren’t working in an IDE, you should again consult your documentation to find out what the command is to run the linker.

    Finally, you can execute your program. Remember that you can do this in several ways. There is the usual method of double-clicking the .exe file from Windows Explorer if you’re using Windows. You could also run your program from the command line. To do so, just start a command-line session, change the current directory to the one that contains the .exe file for your program, and then enter the program name to run it. Within an IDE, you’ll probably also have the option of running your program directly. Alternatively, there may be a Run menu that will compile, link, and execute the program in one go.

    NOTE If you’re working in an IDE, you may need to use the Window menu to change to an Output window, where you can see the results of your program execution.

    If everything worked without producing any error messages, you’ve done it! This is your first program, and you should see the following message on the screen:

    Hello world

    Editing Your First Program

    You could try altering the same program to display something else on the screen. For example, you might want to try editing the program to read like this:

    /* Program 1.2 Your Second C Program */

    #include <stdio.h>

    void main() {

    printf("If at first you don\'t succeed, try, try, try again!");

    }

    The \' sequence in the middle of the text to be displayed is called an escape sequence. Here it’s a special way of including a single quote in the text. Because single quotes are used to indicate where a character constant begins and ends, you need a special way to indicate when you mean a single quote and not the start of a character constant. You’ll learn more about escapes sequences very soon. You can try recompiling the program, relinking it, and running it again once you’ve altered the source. With a following wind, and a bit of luck, you’ve now edited your first program. You’ve written a program using the editor, edited it, and then compiled, linked, and executed it. 

    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





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