Printing Output to The Screen


This part is going to demonstrate how to print text output to a console window. Your main goal in this section is to memorize the code necessary to print output to the screen (and how the code works in general), since the details and explanation behind the output code below require that certain C topics are already known (these topics will be covered later). The reason for me covering output before anything else is so that you can actually see something happen in upcoming code examples (and any code you might write).

How to Print Text Onto a Console Window

Here's the Code (put it in main.c)


******* If your project has a file called stdafx.h in it, you must add #include "stdafx.h" (yes, "", not <>) to the top of every .c file that you create, or else the code will not compile. Also, open the stdafx.h file and check to make sure that #include <stdio.h> is not already in it. If this line of code is already in stdafx.h, then remove it from your main.c file.

Your output should look similar to this:

Printing Output to the Console Window in Visual Studio

Even though the details of this example will be covered later, it is still useful to at least have an idea of what the code does before moving on. You don't need to understand the explanation for this code thoroughly. Note that the line numbers are not part of the code itself, but are there simply for reference. Most IDEs number lines of code in a sidebar seperate from the actual text.


#include <stdio.h>

Groups of pre-written instructions (called functions) are often grouped into files based on their similiarites. The C language comes with its own library called the C Standard Library, which contains hundreds of these files. stdio.h is one of these files, and it contains most of the Standard Library's functions pertaining to writing the the console, writing data to files, and reading data from files. When a file is included, the compiler (technically a part of the compiler called the preprocessor) pastes the included file where #include <filename> was written. This makes it so that the compiler can just go through the functions you need, and not every single standard library function (and it also organizes the code).

int main()

This declares and defines a function (group of reusable instructions). int tells the compiler that the function is to return (exactly what this means will be explained in a later section of the tutorial) an integer (a whole number, which is a number with no decimal portion, in case you forgot that)


This indicates the beginning of the function.

char a[4];

This initializes a group of 4 characters.

printf("%i", 8);

This calls a function from stdio.h, which prints the integer (hence the "%i") 8 onto the console window.


This sets the group of 4 characters to whatever you type into the console window, after clicking enter. This is done so that the console window does not close immediately after the program starts (the program will running stop once the function returns, and scanf_s(a) stops the program from going further until enter is pressed).

return 0;

This returns the integer value 0.


This indicates the end of the function.