Output and Input in C

printf and scanf

printf

Gives you the power to print output onto the screen, and is relatively simple to use.

The number of arguments required varies, but the first argument you pass should be a STRING - think of a string as a sequence of characters for now.

Recall that a string must be surrounded by double quote marks.

Here's printf in action:

printf("Hello World!\n");

Notice how the string, "Hello World!\n" is enclosed in double quote marks. The character '\n' is the NEWLINE character and acts like a line break.


Format Specifiers

To print out the value of some variable, you need to embed a format specifier in your text string and pass extra arguments to the printf function. An example:

printf("x equals %d \n", x);

This statement prints out the value of x. Note that the value of x is passed to the printf function. When you pass arguments to functions, you separate each one with a comma - here, "x equals %d \n" is an argument,
so is x.

There are several format specifiers - the one you use should depend on the type of the variable you wish to print out. Here are the common ones:

Format Specifier

Type

%d (or %i)

int

%c

char

%f

float

%lf

double

%s

string

To display a number in scientific notation, use%e.
To display a percent sign, use %%.

Warning!

Don't try to display a decimal number using the integer format specifier, %d, as this displays unexpected values! Similarly, don't use %f for displaying integers. Mixing %d with char variables, or %c with int variables is all right, as shown in this example:

#include <stdio.h>

int main() {
  int a = 72;
  char b = 'A';
  printf("a equals %d \n", a);
  printf("a equals %c \n", a);
  printf("b equals %d \n", b);
  printf("b equals %c \n", b);
}

a equals 72
a equals H
b equals 65
b equals A

The reason why this works is because a character constant is just an integer from 0 to 255.


Two or More Format Specifiers

You could use as many format specifiers as you want with printf - just as long as you pass the correct number of arguments.

The ordering of the arguments matters. The first one should correspond to the first format specifier in the string and so on. Take this example:

printf("a=%d, b=%d, c=%d\n", a,b,c);

If a, b and c were integers, this statement will print the values in the correct order. Rewriting the statement as...

printf("a=%d, b=%d, c=%d\n", c,a,b);

... would still cause the program to compile OK, but the values of a,b and c would be displayed in the wrong order!


scanf

Allows you to input numbers and strings, as well as characters.

Here is an example:

printf("Enter a number ");
printf("
and press Enter: ");
scanf("%d", &a);

IMPORTANT: the symbol & is known as the ADDRESS-OF operator.

Back to the example: scanf takes at least two arguments.

The first one is a string that can consist of format specifiers. The rest of the arguments should be variable names preceded with the ADDRESS-OF operator. Try to picture the previous scanf statement by this: "Read in an integer from the input string, then go to the address of the variable called a and put the value there". Remember that a variable is like a container in your computer's memory - each one has a different address.

Like with printf, the number of arguments after the string argument should match the number of format specifiers contained in that string.

Similarly, the type of the format specifier should match the type of the corresponding variable. The ordering of the variables also matters.


Inputting Multiple Values

If you have multiple format specifiers within the string argument of scanf, you can input multiple values. All you need to do is to separate each format specifier with a DELIMITER - a string that separates variables. For convenience, the delimiter should be one character that's a punctuation mark, like a comma or a space. As a default, scanf stops reading in a value when space, tab or Enter is pressed.

Consider scanf("%d %d", &x, &y);

(Assume that x and y have been declared beforehand!).

If I entered: 1 2 and pressed Enter, 1 would get assigned to x, and 2 would get assigned to y.

But if I entered 1, 2 and pressed Enter, x would equal 1, but y won't get assigned 2 because scanf was not expecting a comma in the input string.

Now consider:
scanf("%d, %d, %d", &x,&y,&z);

If I entered 1 2 3 and pressed enter 1 would get assigned to x but 2 and 3 won't get assigned to y or z, simply because I didn't separate the numbers with commas.

Entering 1,2,3 works, but why does 1, 2, 3 also work? scanf ignores spaces, tabs and carriage returns immediately after the delimiters.

Just don't put a space, tab or carriage return before the delimiter! 1 ,2, 3 won't work.

If you want the user to press return after each number, try something along the lines as:

scanf("%d\n%d\n%d", &x,&y,&z);

Note that you shouldn't put a delimiter after the last format specifier!


Acknowledgement: These notes have been taken from Eddie's basic guide to C and slightly modified by Mirela Damian.