Techworld

An introduction to Ch: Implementing a temperature converter in Ch

Ch is a combined C shell and IDE

Ch is very high-level language environment developed by SoftIntegration, Inc. for Windows, Linux, Solaris, and MacOS X systems. Ch incorporates both a command interpreter (or shell) and an integrated development environment (ChIDE). Ch, unlike other shells, such as the Bourne-Again Shell (bash), the Korn Shell (ksh), or the C Shell (csh), uses the syntax of the C programming language and incorporates a C virtual machine as a shell. This makes Ch a useful tool for learning C programming and for prototyping C programs. This article introduces some of Ch’s features by showing how to write a small Ch program for converting between the Fahrenheit and Celsius temperature scales.

Installing and Starting Ch

Since most Windows, Unix, and MacOS X systems do not come with Ch preinstalled, Ch needs to be downloaded and registered from SoftIntegration’s website. As of this writing, the registration page is located at Softintegration.com.

Once Ch is successfully registered, SoftIntegration will send an email with instructions for downloading and installing Ch. After installing Ch on Windows, Ch’s icon should appear on the desktop. Similarly, on MacOS X, after Ch is installed, its icon should appear in the Dock. Clicking on the Ch icon starts Ch. When Ch starts up it will print out a message similar to the following:

Ch
Standard edition, version 7.0.0.15161
Copyright (C) SoftIntegration, Inc. 2001-2012
http://www.softintegration.com

/>

The > is called the Ch prompt. The / preceding the prompt is the current working directory for Ch. Generally, most familiar commands, such as cd, ls, and cp, work as expected in Ch. In addition to executing such familiar commands, Ch includes the ability to execute statements written in the C language. For example, executing the following statement in Ch:

/> printf("Hello World!")

displays the output:

Hello World! 
/>

Here Ch has executed the printf C language function, which is used to print out formatted strings. Once printf finished, Ch displays its prompt (>) again, to indicate that it is ready to execute additional commands or C statements.

A First Ch Program

A basic Ch program consists of a file containing C statements and/or Ch commands. Files containing Ch programs usually end with the .c or .ch suffix. The .c suffix is preferred for Ch programs that contain only C statements, while the .ch suffix is preferred for Ch programs that contain both C statements and Ch commands.

To convert the printf statement from the previous example into a Ch program, create a file named hello_world.ch using an editor, such as vim, emacs, Notepad+, or TextWrangler, that contains the following statements:

#include <stdio.h>

void main () {
	printf("Hello World!");
}

The first line of this program—#include <stdio.h>—tells Ch to include or incorporate the contents of file <stdio.h> into the program. The file <stdio.h> includes descriptions of functions, such as the printf function, that can be used to obtain information (input) or print out information (output). In addition the input and output functions described in the <stdio.h> file, the C programing language usually comes several additional files that include descriptions of many other useful functions. The functions these files is sometimes referred to as a library of functions. The examples that follow will describe some of the common files and functions in this library in more detail.

The next line of the program—void main()—defines the main function of the program. When a program is executed, Ch starts by executing the main function. In the example above, the main function includes all the statements between the opening curly bracket ({) following main() and the corresponding closing curly bracket (}) on the last line of the program. In this program, the main function contains just the printf statement. The semicolon (;) at the end of the printf statement indicates the statement is complete. Generally, semicolons are required at the end of statements to indicate where a statement ends.

Assuming the file named “hello_world.ch” containing this program is located in the current directory, the program can be executed as follows:

/> ch ./hello_world.ch

It should display the following output:

Hello World!/>

Unlike in the previous example, notice that the Ch prompt (>) is displayed on the same line as the program’s output. When Ch is running interactively and accepting commands one at a time, it prints its prompt (>) on a new line. But when Ch is not running interactively, such as when it is executing a program, it prints its prompt immediately following the program’s output. To make Ch to print its prompt on a new line, a newline character (\n) needs to be added to the end of the string that the program is printing out:

printf("Hello World!\n");

With this change, the program displays the following output:

Hello World!
/>

Command Line Arguments

When Ch executes a program, additional information can be provided to the program using command line arguments. For example, hello_world.ch can be executed as follows:

/> ch ./hello_world.ch hello world

The strings hello and world following hello_world.ch are the command line arguments that are being supplied to the program. Right now the program will ignore any command line arguments that are supplied to it. To allow the program to handle command line arguments, a few changes need to be made to its main function.

First, the main function’s definition has to be changed to accept command line variables. This can be accomplished by changing the main function’s definition as follows:

void main (int argc, string_t argv[]) {

Here, two arguments, int argc and string_t argv[], have been added to the main function. Adding these two arguments gives the main function the ability to accept and process any command line arguments that are supplied to the program. The first argument, int argc, supplies the program the total number of command line arguments that were specified when the program was executed. The second argument, string_t argv[], supplies the program with the command line arguments.

The int keyword that comes before argc defines the argc variable as an integer variable. An integer variable is a variable that can hold a single positive or negative whole number, such as 3 or -57.

The argument string_t argv[] uses the Ch string_t keyword to define a string variable named argv to hold the command line arguments that were specified when the program was executed. A string variable is a variable can hold strings, such as hello and world. The square brackets ([]) following argv further define argv as an array variable. An array variable is a variable that can hold a collection of values instead of just a single value. Thus, string_t argv[] defines argv as a variable that can hold a collection of strings. The first item in this collection can be retrieved using the notation argv[0]. Similarly, the second item can be retrieved as argv[1], and so on.

Second, the main function needs to be changed to process the command line arguments. For example, the printf statement can be modified to print out the first element of the argv array as follows:

printf("%s\n", argv[0]);

Here the string "Hello World!\n" has been replaced with the string "%s\n" and argv[0] has been added to the printf statement. The string "%s\n", which is called the format statement, tells printf how it should format what it prints out. Specifying "%s\n" as the format statement tells printf that it should print out a string followed by a newline character (\n). In addition to %s, printf understands many additional format specifiers, such as %d for printing integers. The argument argv[0] following the format statement tells printf that the string it should print out is the first element of the argv array.

With these changes, the complete program should look like the following:

#include <stdio.h>

void main (int argc, string_t argv[]) {
printf("%s\n", argv[0]);
}

More about: Inc., Linux
References show all
Comments are now closed.
Related Coverage
Related Whitepapers
Latest Stories
Community Comments
Tags: programming language, programming, C
Whitepapers
All whitepapers

Top Whitepapers

Twitter Feed

Featured Whitepapers