Computing and Networking

Using C in UNIX


UNIX C

Overview

C is closely associated with the UNIX operating system. UNIX and most of its software are written in this language.

This document contains the following topics:

Creating C Files

C source code can be created in any text editor, for example EMACS or VI. All C files must must end with a '.c' extension.

Example using EMACS to edit a C file:

				gemacs  file-name.c

Compiling And Running

C source code can be compiled into an executable program. The command to compile your C program is:
				cc  file-name.c
					or
				gcc  file-name.c

Technical Note: There are two standard C formats: ANSI and K&R. gcc compilers work with both ANSI and K&R. cc compilers usually use one or the other: ANSI or K&R. For more information type "man cc" or "man gcc" at the UNIX prompt.

The compiler creates a file called a.out, which is the executable version of the C program. After compilation the program it can be run by typing:
				a.out

Compile Options

 -O   Name  the  final executable file (if this option is not used 
      the file created will be called a.out)

				cc -o  out-file-name file-name.c
					or
				gcc -o  out-file-name file-name.c

Example Program

   main()
   {
      float   x, y, z, sum, avg;
                            
      printf("Please enter 3 numbers: ");
      scanf("%f %f %f", &x, &y, &z);
      sum = x + y + z;
      avg = sum / 3.0;
      printf("The average of the three numbers is: %f\n",avg);
    }                  

For More Information

Contact the Information Center at 646-1840

On-line manual pages for the C compiler, C library functions, and other UNIX commands are available. Computing and Networking also maintains a current copy of UNIX manuals including C.


NMSU Home Directory Search Comments Help
Computing & Networking Library
Phonebook

Last Modified: Sunday January 13, 2008
webmaster@nmsu.edu