Introduction

In this section, we will explore how to use the C programming language on the OpenVMS operating system. C is a powerful and flexible language that is widely used for system programming, and it integrates well with the OpenVMS environment.

Key Concepts

  • C Compiler on OpenVMS: Understanding the available C compilers.
  • Compiling and Linking: Steps to compile and link C programs.
  • Interfacing with OpenVMS System Services: Using system services in C programs.
  • Practical Examples: Sample C programs demonstrating key concepts.
  • Exercises: Hands-on practice to reinforce learning.

C Compiler on OpenVMS

OpenVMS supports several C compilers, with the most commonly used being the HP C compiler. To check if the C compiler is installed on your system, you can use the following command:

$ CC /VERSION

This command will display the version of the C compiler installed on your system.

Compiling and Linking C Programs

Basic Compilation

To compile a C program, use the CC command followed by the source file name. For example, to compile a file named hello.c:

$ CC hello.c

This command generates an object file named hello.obj.

Linking

After compiling, you need to link the object file to create an executable. Use the LINK command:

$ LINK hello

This command creates an executable file named hello.exe.

Running the Executable

To run the executable, simply type its name:

$ RUN hello

Example: Hello World Program

Here is a simple "Hello, World!" program in C:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Save this code in a file named hello.c, then compile, link, and run it using the commands provided above.

Interfacing with OpenVMS System Services

OpenVMS provides a rich set of system services that can be accessed from C programs. These services are available through the sys$ library.

Example: Using System Services

Here is an example of a C program that uses the sys$getjpiw system service to get information about the current process:

#include <stdio.h>
#include <descrip.h>
#include <jpidef.h>
#include <ssdef.h>
#include <starlet.h>

int main() {
    unsigned long status;
    unsigned long pid = -1; // Current process
    unsigned long item = JPI$_PID;
    unsigned long value;
    struct _generic_64 itemlist[2];

    itemlist[0].length = sizeof(value);
    itemlist[0].code = item;
    itemlist[0].buffer = &value;
    itemlist[0].retlen = 0;
    itemlist[1].length = 0;
    itemlist[1].code = 0;

    status = sys$getjpiw(0, &pid, 0, itemlist, 0, 0, 0);

    if (status == SS$_NORMAL) {
        printf("Process ID: %lu\n", value);
    } else {
        printf("Error: %lu\n", status);
    }

    return 0;
}

Explanation

  • Headers: The program includes necessary headers for system services.
  • Variables: It defines variables for the process ID, item code, and value.
  • Item List: It sets up an item list to specify the information to retrieve.
  • System Service Call: It calls sys$getjpiw to get the process ID.
  • Error Handling: It checks the status and prints the process ID or an error message.

Practical Exercises

Exercise 1: Simple Arithmetic

Write a C program that takes two integers as input and prints their sum, difference, product, and quotient.

Solution:

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    printf("Sum: %d\n", a + b);
    printf("Difference: %d\n", a - b);
    printf("Product: %d\n", a * b);
    printf("Quotient: %d\n", a / b);

    return 0;
}

Exercise 2: File I/O

Write a C program that reads a text file and prints its contents to the console.

Solution:

#include <stdio.h>

int main() {
    FILE *file;
    char filename[100], ch;

    printf("Enter the filename: ");
    scanf("%s", filename);

    file = fopen(filename, "r");
    if (file == NULL) {
        printf("Could not open file %s\n", filename);
        return 1;
    }

    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }

    fclose(file);
    return 0;
}

Common Mistakes and Tips

  • Forgetting to Link: Ensure you link the object file to create an executable.
  • Incorrect File Paths: Double-check file paths when opening files.
  • Error Handling: Always check the return values of system calls and handle errors appropriately.

Conclusion

In this section, we covered the basics of using C on OpenVMS, including compiling and linking programs, interfacing with system services, and practical examples. By practicing the exercises, you should gain a solid understanding of how to develop C programs in the OpenVMS environment. In the next section, we will explore using Fortran on OpenVMS.

OpenVMS Programming Course

Module 1: Introduction to OpenVMS

Module 2: Basic OpenVMS Commands

Module 3: OpenVMS File System

Module 4: Scripting with DCL

Module 5: OpenVMS System Management

Module 6: Networking on OpenVMS

Module 7: Advanced OpenVMS Programming

Module 8: OpenVMS Clustering

Module 9: OpenVMS Security

Module 10: Troubleshooting and Optimization

© Copyright 2024. All rights reserved