An introduction to Ch: Implementing a temperature converter in Ch

Ch is a combined C shell and IDE

Executing this program with the command line arguments “hello” and “world” gives the following result:

/> ch ./hello_world.ch hello world
./hello_world.ch

The output that appears is ./hello_world.ch rather than hello, which was the first command line argument. This is because the first entry of the argv array, argv[0], is always the name of the program being executed. The first command line argument is always stored in argv[1]. The second command line argument is stored in argv[2], and so on. Thus, to print out the first command line argument, the printf statement needs to be changed as follows:

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

With this change, the program now prints out the first command line argument “hello”:

/> ch ./hello_world.ch hello world
hello

Loops

To print out the second command line argument, another "%s" format specifier along with the second command line argument, argv[2], could be added to the printf statement as follows:

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

But this approach is not practical for processing a large number of command line arguments. A better approach is to use one of the C language loop statements. A loop statement allows a set of statements to be executed repeatedly. Ch supports all of the standard C language loop statements, while, for, and do-while, along with a foreach loop. The do-while and foreach loops are similar to the while and for loops, so, for the sake of brevity, the examples that follow will focus on the while and for loops.

The while loop provides a convenient way to print out each of the command line arguments. Its syntax is as follows:

while (condition) {
first statement;
second statement;
…
last statement;
}

The while loop executes each statement, while the specified condition is true. Since the variable argc holds the number of command line arguments, the while loop can be used to print out each of the command line arguments as follows:

int i = 1;
while (i < argc) {
printf("Argument No. %d was '%s'\n", i, argv[i]);
i = i + 1;
}

Here, a new integer variable named i is created and is assigned a value of 1 (int i = 1). The single equal sign (=), in C and Ch, is known as the assignment operator and is used to set the value of a variable. Next, a while loop is then used to print out each of the command line arguments. The condition for this while loop is that the value of the variable i must be less than the value of argc (i < argc). While that condition is true, a printf statement is used to print out a particular command line argument (argv[i]). After printing out the command line argument, the value of i is increased (or incremented) by 1 (i = i + 1) so that the next time the while loop is executed, the next command line argument is printed out. With this while loop the program will look like the following:

#include <stdio.h>

void main (int argc, string_t argv[]) {
int i = 1;
while (i < argc) {
printf("Argument No. %d was '%s'\n", i, argv[i]);
i = i + 1;
}
}

Executing the program now displays the following result:

/> ch ./hello_world.ch hello world
Argument No. 1 was 'hello'
Argument No. 2 was 'world'

Another type of loop that is frequently used is the for loop. This loop can be used to execute a set of statements for a particular number of times (each execution of the statements in a loop is called an iteration). The for loop’s syntax is as follows:

for (initializer; condition; increment) {
first statement;
second statement;
…
last statement;
}

The first time the for loop executes it executes the initializer, which can be used to set (or initialize) the values of any variables that are used in the for loop. Next, the for loop checks if the specified condition is true. If the condition is true, then each statement is executed. Finally the for loop executes the increment statement. After executing the increment statement, the for loop will check if the condition is true. If the condition is true, it will executing each statement again. The for loop will continue executing the increment statement, executing each of the statements as long as the condition remains true.

To see how the for loop operates, let’s reimplement the while loop in our program using a for loop:

int i;
for (i = 1; i < argc; i = i+1) {
printf("Argument No. %d was '%s'\n", i, argv[i]);
}

As with the while loop, a new integer variable i (int i) is created. But, instead of initializing this variable when it was created, the for loop’s initializer statement is used to initialize the variable’s value to 1 (i = 1). Like the while loop, the condition remains the same—as long as the value of i is less than the value of argc (i < argc), the printf statement should be executed. The increment also statement remains the same, since the value of i should be incremented by 1 (i = i+1) after the printf statement is executed. With this for loop the program will look like the following:

#include <stdio.h>

void main (int argc, string_t argv[]) {
int i;
for (i = 1; i < argc; i = i+1) {
printf("Argument No. %d was '%s'\n", i, argv[i]);
}
}

The output from the program remains the same:

/> ch ./hello_world.ch hello world
Argument No. 1 was 'hello'
Argument No. 2 was 'world'

Conditional Statements and Validating Input

The goal of this article is to write a small Ch program for converting between different temperature scales. To accomplish this, the program needs to receive a temperature to convert, what scale that temperature is in, and what scale to covert that temperature to. Command line arguments can be used to obtain all this information from the user. The program can use the first command line argument to receive the temperature to convert, the second command line argument to receive the scale for that temperature, and the third command line argument to receive the scale to convert the temperature to.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags programmingCprogramming language

More about Inc.Linux

Show Comments
[]