Next: Basic classes, Previous: Introduction, Up: Top [Index]
Hello, world!
and Other Simple ProgramsCtalk 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.
"Hello, world!"
To learn how to write and compile a Ctalk application, this chapter
starts with the usual 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.
(It’s also in the
demos
subdirectory if you have the source code archive.) Then you
can build hello.c
with the ctcc
command.
$ ctcc hello.c -o hello
Then, you can run the program with this command, and the program prints the output.
$ ./hello "Hello, world!"
Here is another simple program, ctpath.c.
You can find the
source file in the demos
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, 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 -o ctpath
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.
You might note also the constructor statement in the example above,
Integer new nItems, i;
This is a shorthand that is specific to constructor methods (i.e., the
methods that are named new
): A new
method can construct
as many objects of the receiver’s class (that is, the class object
before new
), as there are labels given in the argument list.
The expression above is exactly equivalent to this set of expressions:
Integer new nItems; Integer new i;
So anywhere in this manual that you see one or more expressions that
contain new
with the same receiver class, you can use either of
the two forms and achieve the same result.
Here is a program that uses a method to get the local time and store it in an array.
Array instanceMethod getTime (void) { CTime 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 (CTime
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.
The CalendarTime
class achieves the much the same result as the
CTime
class, except that it uses named instance variables to
store elements of the current time:
int main () { CalendarTime new ct; ct utcTime; ct localTime; printf ("%02d:%02d:%02d %04d-%02d-%02d\n", ct hours, ct minutes, ct seconds, (ct year + 1900), ct month, ct dom); }
Depending on the needs of the program, you might find this to be more
efficient than dealing with individual Array
elements.
The program xhello.c
in the demos
subdirectory is too
lengthy to include here, but if you open it in a text editor, it
should look at least a little familiar if you’ve written programs
with the X Window System.
xhello.c
performs the basic steps of creating and displaying a
window, and then waiting for input from the keyboard or mouse.
Building the program is similar to the other examples in this chapter.
You need to include the ‘-x’ option when building
xhello.c
, so that Ctalk links the program with the X Window
System libraries, however.
$ ctcc -x xhello.c -o xhello
And then you can run the program directly.
$ ./xhello
In brief, whenever you move or resize the window, or another window
uncovers the xhello
window, the program redisplays the “Hello,
world!” text in the center of the window.
If you click on the “Close” icon or menu item in the Window’s frame (depending on the type of desktop your system has), the program closes the window and exits.
With some desktops, you might notice that the message flickers as the
program handles events from the display system. That’s because
xhello
responds to a generic set of events so that it works
with many different desktops and window system features and is simple
for beginners to build.
In addition to displaying simple text, Ctalk’s classes provide flexible support of many drawing and user interface capabilities. That lets programs handle complex interactions with the window system. The later chapters of the tutorial discuss these features and how to write programs with them. See Graphics.
Next: Basic classes, Previous: Introduction, Up: Top [Index]