Output
Now, let’s get into some actual coding! First, we will be going over how to display information to the user.
printf()
The printf() function is one that allows you to print, or display, information to the user. Anything and everything you include inside a printf() statement will be displayed to the console upon running your program. Below is an example of using this statement:
printf(“Hello World!”);
There are a couple components that make up the above statement that are worth discussing in this section, the first being what goes inside the parentheses. As you can see, there is a set of quotation marks in the parentheses and the words Hello World! are contained within these quotation marks ("").
Whenever you want to display text to the user, you should use quotation marks around the text. Doing this will display the text to the console exactly how you typed it in the quotation marks. This means the same letters you capitalized in your code will be capitalized in the display and any punctuation you include will also be displayed on the screen.
An important thing to note here is that the quotation marks themselves do not show up on the screen with your text. The other component is the semicolon at the end of the statement. This semicolon lets the compiler know that it is the end of that line of code. It signifies an end to a thought, much like a period signifies an end to a sentence. Without the semicolon, your code will not work, so make sure you remember to place it in there.
Practice
Cooking in C
Congratulations! You’ve just learned your first method in C. Now, let’s put it to the test. Your challenge is to print a recipe into the console. An example output is:
My famous Peanut Butter Cookies! First, preheat your oven to 350 degrees fahrenheit. Then, mix in 1 cup of peanut, 1 cup of sugar, and 1 egg Bake for 10 minutes then serve!
Solution Code Here:
#include <stdio.h> int main(void) { printf("My famous Peanut Butter Cookies!\n\n"); printf("First, preheat your oven to 350 degrees fahrenheit.\n"); printf("Then, mix in 1 cup of peanut, 1 cup of sugar, and 1 egg\n"); printf("Bake for 10 minutes then serve!"); }
Sing a Song!
Print out the lyrics to your favorite song!
Happy Birthday To You Happy Birthday To You Happy Birthday Dear C Student Happy Birthday To You
Solution Code Here:
#include <stdio.h> int main(void) { printf("Happy Birthday To You\n"); printf("Happy Birthday To You\n"); printf("Happy Birthday Dear C Student\n"); printf("Happy Birthday To You"); }
Copyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.