0% found this document useful (0 votes)
12 views

Using Scanf

Using scanf in C

Uploaded by

DARWIN RANILE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Using Scanf

Using scanf in C

Uploaded by

DARWIN RANILE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Using scanf

See program below (scanf.c).

#include<stdio.h>

main()
{
int num;
printf("Enter a number: ");
scanf("%d",&num);
printf("You entered %d.",num);
}

And the display upon compile and run of the program:


Input is being asked at this moment. For the purpose of continuation, 2 is
inputted and we press the Enter key.

Explanation of the program:

The sample program makes use of the function scanf. While printf allows us to
display to the screen, scanf allows us to retrieve something from the screen. In other
words, printf is output while scanf is input.

In the program execution, when the line for the scanf is executed the compiler
pauses because it awaits an input from the user. Whatever integer is inputted is
displayed accordingly in the succeeding printf statement.

A more thorough explanation on scanf:

scanf(“%d”, &num);

Since scanf is a function, the parentheses are needed. Inside the parentheses
are a string (“%d”) and an argument (&num) separated by a comma.

The string contains the data format which would correspond to the data type of
the variable (the argument part).

The argument contains an ampersand (&) preceding the variable. The variable
will be the placeholder for the input.
In relation to the sample program and execution, after integer 2 is entered it will
be stored in num.

The scanf function is very restrictive in how it is to be used. Here are several
things to remember:

- Like in printf, the number of data formats should correspond to the number of
variables in the argument part and the data formats should match with the
data types of the variables.
- Other than the data format, there is nothing else needed to be placed inside
the string.
- The comma is needed to separate the string and the argument. If there are
more arguments then each should be separated with a comma.
- The ampersand (&) is required for every variable. The ampersand is the
address operator. For now, just remember that it is required.

Any of the above things are missed or incorrectly placed would generate an
error.

Self-activity:

Modify the program by adding another variable to be used for storage and ask for
a second input. The second input should be stored in the second variable declared. This
input should be displayed (in the same manner as the first input).
Multiple variables in scanf

In the previous example, only one variable is used in the scanf statement. This
enables only one input per scanf statement. It is possible though to have multiple
variables in a scanf statement therefore allowing multiple inputs.

See program below (scanf2.c):

#include<stdio.h>

main()
{
int num1,num2;
printf("Enter two numbers: ");
scanf("%d%d",&num1,&num2);
printf("You entered %d and %d.",num1,num2);
}

The display after input:

Note the scanf statement:

scanf("%d%d",&num1,&num2);

The string contains two data formats. The arguments also have two variables
which are separated by a comma.
During the execution of the program, 5 and 9 was inputted. During the said input,
a blank space separates the 5 and the 9 which would then store the 5 into the first
variable (num1) and 9 into the second variable (num2) upon pressing the Enter key.
The order of storing is done sequentially.

This situation might happen: Instead of having a space after 5 the Enter key was
pressed. Since the program is expecting two inputs then pressing the Enter key only
satisfied the first input and the program will continue to wait for the second input to be
entered. Only after the second input will the program continue to execute the next
statements.

Note: The preceding paragraph is one reason why I personally do not use
only one scanf for two (or more) inputs. There is the tendency that the user of the
program would not know how to proceed when the program is waiting for the
second input. Of course the remedy would be to provide the proper instruction
for the said input. Another course of action is to have the corresponding number
of scanf per input needed.

Check updated program below (scanf3.c):

#include<stdio.h>

main()
{
int num1,num2;
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("You entered %d and %d.",num1,num2);
}

You might also like