C++ Tutorials


Basic Output

Tutorials > C++ > Basic Output

View Full Source


This tutorial will show you how to produce console output in both C and C++.
Create a new source file in your environment called main.cpp
The code will be separated to show you how to use C and how to use C++. Following tutorials will also use this technique. You should try to stick with one way of doing output for a particular program. Both achieve the same things, but you will need to understand both methods a both are used in the industry.

Contents of main.cpp :

Everytime you want to use library functions from C or C++, you need to include specific header files (.h) to tell the compiler which functions you can use.
The header file is always included using the #include <somefile.h> preprocessor directive. Any line starting with a # is known as a preprocessor directive as these lines are executed before your code is compiled.

The stdlib.h file includes standard C functions.
The stdio.h file includes C functions for standard input and output.
The iostream file includes C++ functions for input and output streams. It does not have a .h extension as using the .h after iostream would indicate using the old stream functions. You may need to add a .h if your file does not compile properly in unix.

#include <stdlib.h>    // for system function
#include <stdio.h>     // for printf function
#include <iostream>  // for cout function

Many C++ functions and classes (explained later) are contained in what is called a namespace. A namespace is used to separate different functions and classes to prevent name ambiguity. Creating your own namespaces will be explained in a future tutorial. The functions needed for C++ input and output are embedded in the std namespace. Whenever specifying what namespace to use, you must use the using namespace keywords.

// namespace containing C++ io functions
using namespace std;

int main()

When doing C output, you would use the printf function. Whenever a function is used, you may have to pass in some parameters.
These are the values shown in the brackets after the function. For now, you can place a single string as a parameter to output to the console.
A string consists of set of characters and is specified in code by enclosing the characters within "".

A \n is used to indicate a newline which places the cursor on the following line.

	// C Output
	printf("C Output\n");

When doing C++ output, you would use the cout object. Parameters are therefore not used. Objects will be explained more when we cover classes in a future tutorial.

The redirection operator << is used to pass strings onto the cout object to output to the console. The endl keyword is used to specify a newline.

	// C++ Output
	cout << "C++ Output\n";
	cout << "More C++ Output" << endl;

The next line shows what it would look like if you did not include the using namespace std line. The :: operator is used as a scope operator to access a specific function, object, etc. in a particular namespace.

	std::cout << "Even more C++ Output" << std::endl;

The system function is used to run a command that you could normally run in a console window. Try this out by typing pause in a windows console window. The code must be changed in unix to something like the sleep command.


	return 0;

Congratulations. You now know how to do basic output in both C and C++.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++ Unix / Linux

< Tutorial 03 - Main Function Tutorial 05 - Data Types >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer