Next: , Previous: Introduction, Up: Top


Hello, world! and Other Simple Programs

Ctalk assumes that you are familiar with the C language, and that you have an understanding of basic object oriented programming concepts, which we described in the previous chapter.

This chapter presents a few simple programs that demonstrate how the Ctalk language works. Later chapters will describe the more advanced features of the language in greater detail.

Printing "Hello, world!"

To learn how to write and compile a Ctalk application, this chapter starts with the obligatory Hello, world! program.

Here is the Hello, world! program listing.

     int main () {
     
       String new helloObject;
     
       helloObject = "\"Hello, world!\"";
     
       printf ("%s\n", helloObject value);
     
       exit (0);
     }

Save this listing in a file called hello.c, and then build it with the ctcc command.

     $ ctcc hello.c

Then, you can run the program with this command, and the program will print the output.

     $ ./hello
     "Hello, world!"

Printing Parts of a String

Here is another simple program, ctpath.c. You can find the source file in the programs subdirectory of the Ctalk source code distribution.

This program prints the directory path that you supply as an argument, one directory to a line.

     int main (int argc, char **argv) {
     
       String new path;
       Array new paths;
       Integer new nItems;
       Integer new i;
       Character new separator;
     
       if (argc != 2) {
         printf ("Usage: ctpath <path>\n");
         exit (1);
       }
     
       path = argv[1];
     
       separator = '/';
     
       nItems = path split separator, paths;
     
       for (i = 0; i < nItems; i = i + 1)
         printf ("%s\n", paths at i);
     
       exit (0);
     }

Although there is a lot of code for initializing variables and checking the command line arguments, ctpath does the actual work in three lines of code.

     nItems = path split separator, paths;
     
     for (i = 0; i < nItems; i = i + 1)
       printf ("%s\n", paths at i);

The method split (class String) splits the string path at each occurrence of separator and places the result in the array, paths. The next two lines print each element of the array paths with the statement, paths at i.

As with the previous example, you can compile the program using the following commands.

     $ ctcc ctpath.c

When you run the program, the output should look like this, depending on the directory path you provide on the command line.

     $ ./ctpath /home/users/joe
     home
     users
     joe

If you're not certain of how the arguments to main, argc and argv, function (they contain the command line, split into individual strings, and the number of command line strings), then you should go back and study the C language until you are comfortable with the language.

Printing the Time

Here is a program that uses a method to get the local time and store it in an array.

     #include <time.h>
     
     Array instanceMethod getTime (void) {
     
       Time new timeNow;
       Array new currentLocalTime;
     
       timeNow utcTime;
     
       currentLocalTime = timeNow localTime;
     
       self atPut 0, (currentLocalTime at 2);
       self atPut 1, (currentLocalTime at 1);
       self atPut 2, (currentLocalTime at 0);
     
       return NULL;
     }
     
     int main () {
     
       Array new clockTime;
     
       clockTime getTime;
     
       printf ("%02d:%02d:%02d\n", (clockTime at 0), (clockTime at 1), (clockTime at 2));
     }

In the method getTime, self refers to the method's receiver, clockTime, which is declared in main.

The localTime method (Time class), returns an Array that is filled in by the C library's call to localtime(3). The Ctalk Language Reference describes how localTime returns time and date information.

Note that in main, the arguments to printf are enclosed in parentheses, so there is no ambiguity in evaluating the expressions in each argument.