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:

printf1.png

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.

printf2.png

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.

printf4.png
printf3.png
printf5.png

Example:

Using \n creates a new line

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.

printf6.png

Example:

printf7.png

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

printf8.png
  • Twitter
  • LinkedIn
  • discord-logo--v2
  • kisspng-github-pages-logo-repository-fork-github-logo-1-magentys-5b69de71b51265

Dragon Eye Intelligence LLC

bottom of page