<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 1 Writing and Compiling A First Program - Hello World_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 1: A First Program
Hello World
 Related Resources
• C++ Tutorial - See all lessons
• Product Review - Visual C++
• Compiling a C++ Program From The Command Line
• Compiling with Visual C++
• Learn to compile
 
 From Other Guides
• What is Unix
• Object-Oriented JavaScript
• Anatomy of an XML Document
• How to compare values in Perl and PHP
 
 Elsewhere on the Web
• C FAQ
 

Welcome to About.com's C++ tutorial. The lessons in this tutorial will take you from being a beginner to being able to write real programs in C++.

C++ is a compiled language. The C++ compiler is a program that reads source code, which is the C++ code written by a programmer, and it produces an executable or binary file that in a format that can be read and executed (run) by a computer. The source file is a plain text file containing your code. The executable file consists of machine code, 1's and 0's that are not meant to be understood or read by people, but only by computers.

The best way to learn anything is to jump right in, so let's start by writing a simple C++ hello world program.

First Program

#include <iostream>
using namespace std;
int main()
{
     cout << "Hello World From About\n";
}

Line 1: #include <iostream>
     As part of compilation, the C++ compiler runs a program called the C++ preprocessor. The preprocessor is able to add and remove code from your source file. In this case, the directive #include tells the preprocessor to include code from the file iostream. This file contains declarations for functions that the program needs to use, as well as available classes.

Line 2: using namespace std
     C++ supports the concept of name spaces. Essentially, this allows variables to localized to certain regions of code. The command using namespace std allows all objects and functions from the standard input and output library to be used within this program without explicit qualifications. Namespaces are an advanced concept. For now, all you need to know is that this line allows simple use of the standard library.

Line 3: int main()
     This statement declares the main function. A C++ program can contain many functions but must always have one main function. A function is a self-contained module of code that can accomplish some task. Functions are examined in a later tutorial. The "int" specifies the return type of main to be an integer. An explicit value may be returned using a return statement. In this case, 0 is returned be default.

Line 4: {
     This opening bracket denotes the start of the program.

Line 5: cout << "Hello From About\n";
     cout is a object from a standard C++ library that has a method used to print strings to the standard output, normally your screen. The compiler links code from these standard libraries to the code you have written to produce the final executable. The "\n" is a special format modifier that tells the method to put a line feed at the end of the line. If there were another "cout" in this program, its string would print on the next line.

Line 6: }
     This closing bracket denotes the end of the program.

That's it. To get the most of this series of tutorials, you should get access to both a text editor and a C++ compiler. Some instructions for doing this are in our tutorials on compiling.

John

Previous Articles



Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.