4.8 First Shell Script4 Labs4.6 Pascal program compile and run4.7 The "C" Language

4.7 The "C" Language

C is a systems programming language. The UNIX operating system is mostly written in C. Many compilers are written in C. It is also a very portable language. There are C compilers available on most computer systems, from the very small (cross compilers are available for embedded systems) to the largest. For super-computers there is a group working on a vector version of C. C is also one of the languages in IBM's Systems Application Architecture (SAA). If written in IBM's dialect of C they assure customers that the program will run under OS/2, OS/400, VM, and MVS. This lab is an introduction to traditional C. There is a related language called C++. It is an object oriented language.
  1. 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"); 
    }
    
  2. Compile and run the program:
     
    gcc -o hello hello.c           (gcc is the C Compiler) 
    hello                          (run the program) 
    
    hello, world                  (resulting output)
    
  3. 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");
    
    }
    
  4. Compile and run (test) the program.
    args "This is number 1" and this is 2 to 7
    
  5. 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:
Your-Name
Lab 4.7: ``C''
TABER CIS135
Place the lab in the instructor hand-in box in BUS R6E, the "terminal room".
Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site ** The Mad Dr. G.'s home page on phred.

4.8 First Shell Script4 Labs4.6 Pascal program compile and run4.7 The "C" Language