Warning: Creating default object from empty value in /home/patricknevindwyer/digilutionary.com/wp-includes/functions.php on line 292
digilutionary.com
Patrick Dwyer

Echo :: C++

in News, Comparative Programming, C++ by patrick


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/patricknevindwyer/digilutionary.com/wp-includes/functions-formatting.php on line 76

This program is part of the Comparative Programming :: Echo set of examples.

The C++ example is nearly identical to the C example, with slight alterations for more common C++ libraries and idioms. The main method has the same signature, the for loop is the same, we even use the same technique of printing out each argument followed by a space. Instead of the printf method from C, we use IO Streams.

Just like our C program, the C++ example compiles to a native binary, sacrificing portability for speed and native architecture.

C++
#include <iostream>

int main(int argc, char* argv[]) {

for (int i = 1; i < argc; i++) {
std::cout << argv[i] << " ";
}

std::cout << std::endl;

}

Program Source: echo.cc

Compiling the example

From the command line:

g++ echo.cc -o echo_cc

Running the example

From the command line:

./echo_cc Hello World