- Using the text editor, create the following "C" program.
This program "translated" is a good first choice whenever
you are faced with a new language or a new system. It gets you
away from the problems of the language and lets you look at the
procedural difficulties of getting a simple program to work.
Please name the source file hello.c.
/* Program to print "hello, world" */
/* L Taber October 13, 1992 PCC */
/* Kernighan & Richie "The C Programming Language" */
/* Copyright 1978 page 6 */
#include <stdio.h>
main()
{
printf("hello, world\n");
}
-
Compile and run the program:
gcc -o hello hello.c (gcc is the C Compiler)
hello (run the program)
hello, world (resulting output)
- The next program looks at the arguments and environment variables
available to the program. Most operating systems pass some information
to programs being run. Arguments are placed on the command line.
The environment is information about system and the user running
the program. Create the following C program,
please name the source file args.c.
/* Program to print arguments to a program */
/* L Taber March 28, 1993 PCC */
#include <stdio.h>
main(argc, argv, env)
int argc;
char * argv[ ];
char * env[ ];
{
int i;
/* Print number of arguments and the first argument */
printf("argc = %d File name = %s\n", argc, argv[0]);
/* Print all remaining arguments - if any */
for ( i=1; i<argc; i++)
printf(" arg[%d] = %s \n" ,i,argv[i]);
printf("\n");
/* Print the environment - if any */
for ( i=0; env[i] != NULL; i++)
printf(" env[%d] = %s\n" ,i,env[i]);
printf("\n");
}
- Compile and run (test) the program.
args "This is number 1" and this is 2 to 7
- Run the program again. Passing arguments to the program as
shown:
args "This is number 1" and this is 2 to 7 | lpr
This time the output should be re-directed to a file the line printer
instead of your terminal.
Turn in
this printout marked with: