In: Computer Science
Construct two small C programs, compile and set up two executable files for the two commands world and mars. Both commands simply print out messages on the screen:
world n - print the message hello world on the screen n times
mars n - print the message HELLO MARS on the screen n times
Thanks a lot for your help!!!
world.c
#include <stdio.h> /*typically in /usr/include*/
#include <stdlib.h>
int main (int argc, char* argv [ ])
/*pass command line arguments from shell;
argc-number of parameters and argv[]—the arguments*/
{
int count, i;
if (argc != 2)
{
fprintf(stderr, "usage: %s count\n",
argv[0]);
/* run as- hello world count*/
exit (-1);
/*signal error (return other than 0) to shell*/
}
else
count = atoi (argv[1]);
for ( i = 0; i < count; i++ )
printf("hello world!\n");
return 0;
}
mars.c
#include <stdio.h> /*typically in /usr/include*/
#include <stdlib.h>
int main (int argc, char* argv [ ])
/*pass command line arguments from shell;
argc-numberof parameters and argv[]—the arguments*/
{
int count, i;
if (argc != 2)
{
fprintf(stderr, "usage: %s count\n",
argv[0]);
/* run as- hello world count*/
exit (-1);
/*signal error (return other than 0) to shell*/
}
else
count = atoi (argv[1]);
for ( i = 0; i < count; i++ )
printf("Hello Mars!\n");
return 0;
}