We have seen one way of doing output so far, by using this printf thing:

printf("Yup, this is some text!");

This is the primary way we’ll be handling output for our programs in this class. But… where does printf come from, and what is it? As it turns out, printf is part of something called the C Standard Library.

The C Standard Library is basically a “starter kit” of sorts, for writing programs. It’s a collection of code you can use across your different programming projects to solve a wide set of problems. The C Standard Library is usually written by the same people who create compilers. If you are compiling your program using Visual Studio, then the implementation of the C Standard Library you’re using is developed and maintained by Microsoft. To use a specific portion of the Standard Library, you must specifically include it in your code:

#include <stdio.h>;

We will use a few different #include statements to grab a few interesting bits of code from the standard library for later use.

Question

What do you think would happen if we tried compiling code with a printf statement, and the #include line above was omitted from the code?

Output text

(for showing text to the user of the program)

//Write text
printf("Yeah, this is some text");

Output

Yeah, this is some text!

Ouput two lines of text

//Using the newline character
printf("Some text\nand some more!");

Output

Some text
and some more!

Output an integer variable, as text

//write a line of text, with a number attached
int x = 5;
printf("My number is %d", x);

Output

My number is 5

Output multiple variables

int y = 10;
float z = 82.44f;
printf("Numbers:%d and %.2f", y, z);

Output

Numbers:10 and 82.44

Scanf

Yes, we’ve seen how to do output… but how do we give our programs INPUT, so we can interact with them? Programs are pretty boring if they can’t listen to any input or commands from the user!

To get input from the user of a program, we can use a function called scanf. It works in a similar fashion to printf, but has a few quirks of its own.

//I've created an integer variable, and set its value to 10
int myNumber = 10;
 
//Now I want the user of the program to set 
//the value of the integer variable myNumber 
scanf("%d", &myNumber);
 
//Above, the program will wait for user input!
 
//Finally, if I print out the value of myNumber, it should 
//be the value the user put into it!
printf("The value entered by the user is: %d", myNumber);

There are a few things to note here. One is that scanf is NOT used for OUTPUT. That means, you ONLY need the format specifier(s) in the double quotes. NOTHING ELSE. Don’t try to print out text using scanf, because that ain’t how scanf works!

There’s one other odd thing to note here. The way we use scanf if pretty similar to printf: parentheses, double quotes, format specifiers, and finally commas with variable names. However, the variable names have the AMPERSAND in front of them! Why is that? Well… that’s some wizard magic I’d rather not explain right now. But it’s enough to know that you MUST use the ampersand in front of variable names when using scanf, but NOT when using printf.

Input

(for getting user input)

//Read integer input from the user of the program
int x = -2784;
scanf("%d", &x);

Result

The value of x is set to whatever integer is entered by the user

//Read float input from the user of the program
float myFloat = 0.0f;
scanf("%f", &myFloat);

Result

The value of myFloat is set to whatever decimal number is entered by the user