How it works...

The user is asked to specify how many kilometers the vehicle is required to be rented for. The value entered by the user is assigned to the distance variable. Thereafter, the user is asked to specify what kind of car they want to rent: an AC car or a non-AC car. The option entered by the user is assigned to the car_type variable. Three register variables are defined by the names Acperkm, Nonacperkm, and servicetax.

Because the register variables stay closer to the CPU and their access time is very low when compared to accessing content from memory variables, the register variables are used for those values that are frequently required in computation. The three register variables, Acperkm, Nonacperkm, and servicetax, are initialized to 3, 2, and 1, respectively, to indicate that the rate for an AC car is $3 per kilometer and that of a non-AC car is $2 per kilometer. The service tax is assumed to be 1% of the total amount.

String comparison is done to know the type of car that is specified by the user. If the type of car selected is an AC car, the values in the distance variable and the Acperkm register variable are multiplied.

Similarly, if the type of car selected is a non-AC car, the values in the distance and Nonacperkm variables are multiplied together. The result of the multiplication is the total amount to be assigned to the carRent variable. To this total amount, a 1% service tax rate is added to find out the total rent. The total rent of the car for the specified distance and type of car is then displayed on the screen.

The program is compiled using GCC, as shown in the following screenshot. Because no errors appear on compilation, that means the tourvehicle.c program has successfully been compiled into an EXE file, tourvehicle.exe. On executing the file, the user is prompted to enter the number of kilometers for which the car is required on rent. The user will also be asked to specify the type of car that is required on rent. The program then displays the total rent of the car, as shown in the screenshot:

Figure 15.1

Voilà! We have successfully used register variables to speed up processing in C. Now let's move on to the next recipe!