7. Compiling & Running Programs

7.1. The C Language

Create a file with .c as suffix, e.g., mine.c and place in it the following C program.

/* this is a comment */
#include <stdio.h>    /* bring in I/O library */
main ( )
   {
     int i;
     i = 8;
     i = i + 1;
     printf("This is a C program. %d\n", i);
   }

To compile and link using GNU's C compiler, type:

[student@hostname ~]$ gcc mine.c -o mine

To run:

[student@hostname ~]$ mine

7.2. The C++ Language

Create a file with .cc as suffix, e.g., mine.cc and place in it the following C++ program.

// this is a comment 
#include <iostream>    // bring in I/O library 
using namespace std;
main ( )
   {
     int i;
     i = 8;
     i = i + 1;
     cout <<  "This is a C++ program. " << i << "\n";
   }

To compile and link using GNU's C++ compiler, type:

[student@hostname ~]$ g++ mine.cc -o mine

To run:

[student@hostname ~]$ mine

7.3. The Java Language

Create a file with .java as suffix, e.g., test.java and place in it the following Java program.

// this is a comment
class test {
    public static void main(String[] args) {
        int i;
        i = 8;
        i = i + 1;
        System.out.print("This is a Java program. ");
        System.out.print(i);
        System.out.println("");
    }
}

To compile using the Java compiler, type:

[student@hostname ~]$ javac test.java

To run:

[student@hostname ~]$ java test