YouTip LogoYouTip

C Exercise Example10

## C Programming Exercise - Example 10: Printing Stairs with Smiley Faces In this tutorial, you will learn how to use nested loops and extended ASCII characters in C to print a staircase pattern topped with two smiley faces. This exercise is excellent for mastering loop control structures and understanding character encoding in terminal outputs. --- ### Problem Description Write a C program that prints a staircase pattern. At the very top of the stairs, the program should display two smiley faces. --- ### Algorithm Analysis 1. **Smiley Faces:** In the standard IBM PC Extended ASCII (Code Page 437) character set, the ASCII value `1` represents a white smiley face (`☺`). We can print this using the octal escape sequence `\1` or by casting the integer `1` to a character. 2. **Staircase Pattern:** - We use nested `for` loops to control the rows and columns. - The outer loop variable `i` controls the current row (representing the height of the stairs). - The inner loop variable `j` controls the number of blocks printed in each row. The number of blocks in row `i` is proportional to `i`, creating a step-like pattern. 3. **Staircase Blocks:** The extended ASCII value `219` represents a solid block (`β–ˆ`). Printing two of these blocks together (`%c%c`, 219, 219) creates a visually balanced square step. --- ### Code Implementation Below is the complete C program to achieve this output. ```c /** * Description: Print a staircase pattern topped with two smiley faces. * Website: YouTip (Classic C 100 Examples) */ #include int main() { int i, j; // Print two smiley faces using the ASCII value 1 (\1) printf("\1\1\n"); // Outer loop controls the number of steps (10 rows) for (i = 1; i < 11; i++) { // Inner loop controls the width of each step for (j = 1; j <= i; j++) { // ASCII 219 represents a solid block character 'β–ˆ' // Printing it twice makes the step look more like a square printf("%c%c", 219, 219); } // Move to the next line after printing each step printf("\n"); } return 0; } ``` --- ### Expected Output When compiled and run in an environment that supports Code Page 437 (OEM United States) encoding, the output will look like this: ```text ☺☺ β–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ ``` --- ### Important Considerations & Troubleshooting #### 1. Garbled Characters or Question Marks (`?`) in the Output If you see strange symbols, question marks, or raw numbers instead of smiley faces and solid blocks, it is because your modern terminal uses **UTF-8** encoding by default, whereas this program relies on the legacy **extended ASCII (Code Page 437)**. **Solutions:** * **On Windows (Command Prompt):** Before running the compiled program, switch your active code page to OEM United States by executing the following command in your command prompt: ```cmd chcp 437 ``` * **Modern Cross-Platform Solution (Unicode/UTF-8):** If you want your code to run natively on modern terminals (Linux, macOS, modern Windows Terminal) without changing system locales, you should use UTF-8 wide characters instead: ```c #include #include int main() { // Set the locale to use the system's default UTF-8 encoding setlocale(LC_ALL, ""); // Print Unicode smiley faces (U+263A) printf("\u263A\u263A\n"); for (int i = 1; i < 11; i++) { for (int j = 1; j <= i; j++) { // Print Unicode solid block (U+2588) printf("\u2588\u2588"); } printf("\n"); } return 0; } ```
← C Exercise Example11C Exercise Example9 β†’