top of page
Printf
The printf and scanf functions are used to input and output data to and from a user. Printf is used for outputting data while scanf is used for inputting data.
Printf can take a given input, a string or a variable, and output it. To output static strings we will use: printf ("Text");
Below is an example of a program printing out two different strings:

Notice that even though we are printing the numbers "1234" it is still seen as a string by the program, below is the output produced:
The code looks like this because instead of automatically adding a new line, the Printf function simply ends the program and sends us back to our command line. We can fix this by manually telling Printf to create a new line.

Printf Escape Characters
To add a new line we will use an escape character. An escape character will let us leave the conventional format of printing what is inside the quotes and allows us to produce specific outputs.



Example:
Printing Variables - Conversion Characters
To output variables we will use: printf ("%conversionChar",variable);
Outputting variables rather than a static string is a little different. Because C does not implement as many protections as other languages while programming we have to be careful and write proper secure code. We must tell the program which type variable we are going to output to prevent abuse of the print statement. To do this we use conversion characters.

Example:

We used the integer conversion character along for the integer variable and the character conversion character for the character variable

bottom of page