Lesson 3. Getting inputs and processing them

Overview

  • Write the program that can get inputs from the users and process them

Before We Start

  • This lesson would be relatively hard/long/boring but it is essential part for programming
  • Most programs we use interact with us. By getting inputs from users, the programs will be able to perform things that are referring to the inputs.

Table of Contents

  1. Variables
  2. Getting inputs and Printing the values
  3. Processing the datas
  4. Practice Problem

Variables

keyword Variable Type Bytes Required Range Format
int integer (signed) 2 -32768 to +32767 %d
char character (signed) 1 -128 to +127 %c
float float (signed) 4 -3.4e38 to +3.4e38 %f
double double 8 -1.7e308 to +1.7e308 %lf
long long integer (signed) 4 -2,147,483,648 to +2,147,483,647 %ld
unsigned char character (unsigned) 1 0 to 255 %c
unsigned int integer (unsigned) 2 0 to 65535 %u
unsigned long unsigned long integer 4 0 to 4,294,967,295 %lu
long souble long double 10 -1.7e932 to +1.7e932 %Lf

Importance of Choosing appropriate Variable Type

There are multiple types of variables that you can declare. It is important to use appropriate type of variable depending on the needs. You should not only worry about the lack of range, but also bytes required since there could be limitations in maximum memory capacity (such as IoT devices, some coding competitions, or more).

Decimal Places and Place Holders

In some specific type of programs, it is required to cut the decimal places or extent them. To change the displaied decimal places, you can simply put .(number of decimal places) after the % sign inside the printf fucntion. If you put a number before ., you will be limiting the total number of digits (if there are not enough digits to display, the remaining digits will be displaied as blank before the number). Also if you put 0 infront of the number left to ., it will add 0s to fill the blank. All total digit calculation includes that . in the decimal numbers.

Sample Code #1

1
2
3
4
5
6
#include <stdio.h>
int main(){
  double a=10234.56789;
  printf("%lf",a);
  return 0;
}

Result:

10234.56789

Sample Code #3

1
2
3
4
5
6
#include <stdio.h>
int main(){
  double a=10234.56789;
  printf("%.2lf",a);
  return 0;
}

Result:

10234.57

The 7 was rounded up at the end

Sample Code #3

1
2
3
4
5
6
#include <stdio.h>
int main(){
  double a=34.56789;
  printf("%7.3lf",a);
  return 0;
}

Result:

34.568

There is a space before 3

Sample Code #4

1
2
3
4
5
6
#include <stdio.h>
int main(){
  double a=34.56789;
  printf("%07.2lf",a);
  return 0;
}

Result:

0034.57

There are two 0s before 3

Sample Code #5

1
2
3
4
5
6
1
2
3
4
5
6
#include <stdio.h>
int main(){
  int a=123;
  printf("%4d",a);
  return 0;
}

Result:

123

There is a space before 1

Sample Code #6

1
2
3
4
5
6
1
2
3
4
5
6
#include <stdio.h>
int main(){
  int a=123;
  printf("%4d",a);
  return 0;
}

Result:

123

There is a space before 1

Sample Code #7

1
2
3
4
5
6
#include <stdio.h>
int main(){
  int a=123;
  printf("%09d",a);
  return 0;
}

Result:

000000123

Getting Inputs and Printing the values

Usage of variables

Variables can be used in the program to store values of calculations, user’s input, and more places where you need to store the value.

Getting Inputs

There are multiple ways of getting input from the users in the program. Among all methods to get inputs, scanf function is widely used due to its simplicity.

Sample Code #1

1
2
3
4
5
6
7
#include <stdio.h>
int main() {
  int a = 0;
  scanf("%d",&a);
  printf("The user have inputted %d",a);
  return 0;
}

This sample code will wait for user to input an integer value and store it in variable called a. After that, the program will print the value stored in a with the explaining text.

Sample Code #2

1
2
3
4
5
6
7
8
#include <stdio.h>
int a = 0, b = 0;
int main() {
  printf("Input two integers separated by a blank to perform an addition calculation:\n");
  scanf("%d %d",&a,&b);
  printf("%d + %d = %d",a,b,(a+b));
  return 0;
}

This sample code will take two integers separated by a space and store them in each variable a and b. After that, the program will show the calculation result using the printf function.

Summary

Based on the given sample codes, it is evident to say we can declare the variable by stating

(keyword of the type) (variable name) = (variable initial value)

inside the code.

Also, the declaration can be done outside of the main function, making it a global variable.

When using scanf function, you can store value using

scanf (“(type format)”,&(variable name))

and similar form to get input for multiple variables.

Processing the Datas

Most of the programs (or softwares) requires the datas to be processed to meet the users’ needs. In order to achieve them, a method of performing calulations are required. To perform very complex calculations, external header file is needed. Programmers doesn’t necessary remembers all of them, but looks them up from the documentation of C language (it can be found here). However, the simple calculations (four arithmetic operations) are natively supported.

Calculation Symbol example What it does
* c=a*b multiplies the value stored in two variables
+ c=a+b sums the value stored in two variables
- c=a-b subtract the value stored in variable that comes after - symbol from the previous variable
  c=-a changes the sign of the variable (negative to positive or positive to negative)
/ c=a/b gets the quotient of the deviation calculation ( a devided by b)
% c=a%b gets the remainder of the deviation calculation ( a devided by b). This only works for integers.

These are the very basic calculation symbols in C. More of them will be introduced in later uploads or online resoureces.

In C, it is important to store the calculated value in another variable. Otherwise, the calculation result will not be remaining in the memory, and will result in waste of computing power. To store a value in a variable, we use = after the variable’s name. Another thing to remember is that you should choose the appropriate variable type when storing the calculation value (By this, I am talking about things such as not using integers for real numbers). Also, if you try to divide with zero, your program will be either terminated due to the error or stop responding with an error message.

Practice Problem

Get two doubles from the users and print the calculation result for all possible calculations among 5 calculation types shown in the table above. The printed values should be limited to 3 decimal places and 10 total digits with 0s filling the not-used digits. However, all acalculations should be done using the original values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main(){
  double inputA, inputB;
  double multiple, divide, sum, minus;
  
  scanf("%lf %lf",&inputA,&inputB);
  
  multiple=inputA*inputB;
  divide=inputA/inputB;
  sum=inputA+inputB;
  minus=inputA-inputB;
  
  printf("%010.3lf + %010.3lf = %010.3lf\n",inputA,inputB,sum);
  printf("%010.3lf - %010.3lf = %010.3lf\n",inputA,inputB,minus);
  printf("%010.3lf * %010.3lf = %010.3lf\n",inputA,inputB,multiple);
  printf("%010.3lf / %010.3lf = %010.3lf\n",inputA,inputB,divide);
  return 0;
}

Sample Input #1

1.25 1.25

Sample Output #1

000001.250 + 000001.250 = 000002.500

000001.250 - 000001.250 = 000000.000

000001.250 * 000001.250 = 000001.562

000001.250 / 000001.250 = 000001.000

Sample Input #2

3.141592 9.8765

Sample Output #2

000003.142 + 000009.877 = 000013.018

000003.142 - 000009.877 = -00006.735

000003.142 * 000009.877 = 000031.028

000003.142 / 000009.877 = 000000.318

Sample Input #3

99.99999 0.01

Sample Output #3

000100.000 + 000000.010 = 000100.010

000100.000 - 000000.010 = 000099.990

000100.000 * 000000.010 = 000001.000

000100.000 / 000000.010 = 009999.999

Next Post

Next post will be about making decisions using if and swich…case statements.