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 similar in nature to most of the other examples so far; a for loop iterates through the available arguments as passed in via the command line. As C doesn’t have an explicit method for joining a series of strings together, like a number of other examples so far, we print out each argument followed by a space. The program finished with a newline.

This is also our first example that needs to be compiled into a native binary. While this does make the code non-portable, restricting it to the architecture we compiled it on, it also means that we don’t need an iterpreter to run it.

ANSI C
#include <stdio.h>

int main(int argc, char* argv[]) {
int i;
for (i = 1; i < argc; i++) {
printf("%s ", argv[i]);
}
printf("\n");
}

Program Source: echo.c

Compiling the example

From the command line:

gcc echoc -o echo_c

Running the example

From the command line:

./echo_c Hello World