• Source
    1. /*
    2. 1. Print the program name.
    3. 2. Print the 3rd character of the program name.
    4. 3. Ask the user to enter a character, save it with getchar(),
    5. then display it back to the user with putchar().
    6. 4. Ask the user to enter a string, save it with scanf(),
    7. then display it back to the user with printf().
    8. NOTE: In the Output, "k" and "hello" are the echoes. You don't see the
    9. input since it's entered automatically. If you typed it out manually,
    10. you will see "k" and "hello" twice in your terminal.
    11. */
    12.  
    13. #include <stdio.h>
    14.  
    15. int
    16. main()
    17. {
    18. int c; // c is an int value representing a character
    19. char string_entered[100]; // string_entered is a array of characters
    20. char *PROGRAM_NAME = "ECHO!"; // string literal for program name
    21.  
    22. printf("%s\n", PROGRAM_NAME); // print name of the program
    23. printf("The 3rd character of the program name is: %c\n", *(PROGRAM_NAME+2)); // access one character of the string, using pointers
    24.  
    25. printf("Enter a character to echo: \n"); // ask user for a character
    26. c = getchar(); // getchar() saves the character's ASCII int value to c
    27. putchar(c); // putchar() prints the chracter back to the user
    28. putchar('\n'); // newline
    29.  
    30. printf("Enter a string to echo: \n"); // ask user for a string
    31. scanf(" %[^\t\n]", string_entered); // scanf() gets a string from user, saves it to string_entered
    32. printf("%s\n", string_entered); // printf() prints the string back to the user
    33.  
    34. printf("Goodbye!\n"); // leave the user happy
    35. return 0; // terminate program (main function)
    36. }
    37.