A token-pasting operator is used to define a macro with the name pizzaprice. This macro concatenates the two a and b parameters into a single string. In addition to this, a stringize operator is used to define a macro with the name convertIntoStr, which converts the str parameter into a string. A number of variables are defined, such as smallnormal, mediumnormal, largenormal, and smallextra. These variables represent the price of a small-sized normal pizza, a medium-sized normal pizza, a large-sized normal pizza, and a small-sized pizza with extra cheese, respectively. The normal suffix declares that this is the price of a pizza with a regular amount of cheese. The extra suffix indicates that this variable represents the price of a pizza with extra cheese.
The user is prompted to enter what pizza size the customer is ordering. The size entered is assigned to the pizzasize variable. After that, the user is asked whether the pizza is desired with normal cheese or extra cheese and the choice that is entered is assigned to the topping variable.
Next, branching is done on the basis of the value in the topping variable. If the topping is normal, the string in pizzasize is compared to check whether the pizza size is small, medium, or large and, accordingly, the arguments are passed to the pizzaprice macro. For example, if the user has entered small as the pizza size and the topping as extra, the pizzaprice macro is invoked with two parameters (small and extra). The pizzaprice macro, being a token-pasting operator, will concatenate the small and extra strings into smallextra, and hence the value of the smallextra variable will be displayed as the price of the small-sized pizza with extra cheese as a topping.
The pizzasize and topping variables are combined into a concatenated string, and hence will access the value in the respective variable. Finally, the convertIntoStr macro is invoked, which includes a stringize operator to display a Thanks for visiting us string at the end of the bill.
The program is compiled using GCC, as shown in the following screenshot. Because no error appears during compilation, the preconcat.c program is successfully compiled into a .exe file: preconcat.exe. On executing the file, the user will be asked to enter the desired pizza size and toppings and, accordingly, the program will display the price of the pizza, as shown in the following screenshot:
Voilà! We have successfully applied the stringize and token-pasting operators and created custom pizza orders.