User Tools

Site Tools


crawdad:programming:example1

An Initial Example

We start with one of the simplest, yet complete, C++ programs:

#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
  cout << "Welcome to the Crawford group!\n";
 
  return 0;
}

Put the text above into a source file named welcome.cc and compile it with the following commands:

c++ -c welcome.c
c++ welcome.o -o welcome

The first command will generate an object file, welcome.o, while the second will generate the actual executable program, “welcome”. You can execute/run the final program directly on the command line, yielding the following result:

prompt% welcome
Welcome to the Crawford group!

The namespace keyword is used to keep names of functions and other quantities from getting confused in complicated programs. Here we're using the standard (“std”) namespace, which is associated with the standard C++ library. The “\n” indicates a newline character (on Unix systems, a newline+carriage-return). Try removing it and recompiling your program to see what it does.

crawdad/programming/example1.txt · Last modified: 2014/08/29 09:14 by crawdad