How to Interact With Users in Programming?

Shafi Sahal
Geek Culture
Published in
3 min readMay 4, 2021

Software is like a factory that takes raw materials and gives out a product. The Software takes an input and gives out an output. What every software basically does is taking data from the users or environment(input) and producing new data(output) which will benefit the user of the software. Till now, we have been programming without user interaction. Programming starts to get interesting when interacting with the user.

scanf()

To output values onto the screen we used the printf() function. To take an input from the user through the screen, we use the scanf() function. The usage is similar with a minute difference. What scanf () does is take values from the user and store it in a variable. We can later use the value in this variable.

scanf("%d", &num);

Here scanf() takes a format specifier which specifies that the input will be an integer. The “&num” specifies that the value should be stored in the num variable. Below is an example to take an input and output it to the screen

#include <stdio.h>int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("%d", num);
return 0;
}

Output

Enter a number: 10
10

Here when the execution of the program reaches the scanf(), it will pause the execution there till it gets an input from the user. Only after getting the input, it will proceed to the next line. So, in this case, the program after executing the printf() function that displays a message to the user: “Enter a number: ”, the execution will stop and the program will wait for the user input. Once it gets the input, it will store the input in the num variable and display it on the screen using the printf() function.

You can also make the output more convincing as shown below

#include <stdio.h>int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("The number you just entered was %d", num);
return 0;
}

Output

Enter a number: 10
The number you just entered was 10

See, we put the format specifiers in our message where we want to replace the format specifiers with the value from the variable num. We can do the same thing with more inputs.

#include <stdio.h>int main()
{
int num1, num2;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter a number again: ");
scanf("%d", &num2);
printf("The first number you enterd is %d and the second number
you entered is %d", num1, num2);
return 0;
}

Output

Enter a number: 10
Enter a number again: 20
The first number you entered is 10 and the second number you entered is 20

We just use the format specifiers in our message where we want to replace it with a value and we will also specify the variables that contain the value in the same order we want to display it.

Now let’s make a simple program to take two numbers from the users, calculate their sum and display the result.

Algorithm

  1. Declare three variables num1, num2 and sum of type integer.
  2. Show a message: “Enter the first number”
  3. Take the input and store it in num1
  4. Show a message: “Enter the second number”
  5. Take the input and store it in num2;
  6. Store the value of “num1 + num2” in sum
  7. Show the result

Program

#include <stdio.h>int main()
{
int num1, num2;
int sum;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("%d + %d = %d", num1, num2, sum);
return 0;
}

Output

Enter the first number: 10
Enter the second number: 20
10 + 20 = 30

What we did here is, we took two inputs from the user and calculated its sum. Then we displayed it to the user. That’s how we interact with users in programming.

Previous => The logic in logical operators

Next => Decision Making in Programming

--

--

Shafi Sahal
Geek Culture

Developer, adventure lover and a truth seeker. Like to write about topics from a unique perspective. Twitter: https://twitter.com/_shafisahal.