Home arrow C++ arrow Page 2 - 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 - Creating Your First Program
(Page 2 of 8 )

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.


Figure 1-1.  Creating and executing a program

......................................................................................... 

Try It Out: An Example C Program

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 (;).

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 ashello.c. You can use whatever name you like instead ofhello, 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 iscc.

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 yourcccommand. 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.exefile 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.exefile 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
----------------------------------------------------------------------

 

........................................................................................


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