learn through suffering C language learn through suffering 
C language

input function

The need for input.

With the content covered in the previous chapters, if it involves calculations using the four basic arithmetic operations, you can calculate even the most complex expressions.
But being satisfied with this shows a lack of ambition.

In the previous chapters, we were directly writing numbers into the program.
Essentially, when a program runs, the values are already determined.
That means the program can only perform the same calculation every time.

If we do this, we'll have to rewrite the program if we want to calculate with a different number.
It's practically the same as using a high-end calculator.
It's not an exaggeration to say that programming feels completely pointless.

The way to solve this problem is to enter the number from the keyboard.
It would be more convenient if you could enter numbers each time the program runs.

scanf function

as if using the printf function when displaying on the screen,
We also use functions for input from the keyboard.
For that purpose, C provides the scanf function (scan-eff).
The `scanf` function is used as follows:

scanf function
scanf("Input Conversion Specifiers",&Variable name);

An input conversion specifier is a character that indicates how to convert an input number into a numerical value.
You can use it in much the same way as the output conversion specifiers used with the printf function.

The variable name specifies the variable that will store the input data.
When using the scanf function, you must put the & character before the variable name.
This & indicates the location of a variable, but the details will be explained in Chapter 15.
When this function is executed, the program will enter a waiting state for input.
When a user of the program enters data and presses the Enter key,
That data will be assigned to the specified variable.

Note that using the scanf function requires #include .
There's no need to add it again, as it's the same as when using the printf function.

Numerical input

With this much knowledge, you can input data from the keyboard.
The following program is an example of using the scanf function to display the entered numbers.

Source code
#include <stdio.h>

int main(void)
{
    int data;
    scanf("%d", &data); /* Input section */
    printf("%d\n", data);
    return 0;
}

Here's an example of the program's output:
The text displayed in a faint style is an explanatory sentence that will not actually be displayed.

Solution 2-1
100ใ€€Input Data
100ใ€€Displayed data

This program will wait for input.
Then, enter a number and press the return key, and that number will be displayed.
Of course, numbers other than 100 are also acceptable.

The scanf function can accept not only integer inputs, but also floating-point numbers.
However, for real numbers, you must use the %lf specifier.
Please be careful, the printf function uses the %f specifier.
The following program is an example of using the scanf function to read a floating-point number and then display it.

Source code
#include <stdio.h>

int main(void)
{
    double data;
    scanf("%lf", &data); /* Input section */
    printf("%f\n", data);
    return 0;
}

Here's an example of the program's output:
The text displayed in a faint style is an explanatory sentence that will not actually be displayed.

Solution 2-1
175.128ใ€€Input Data
175.128000ใ€€Displayed data

Multiple inputs

The `scanf` function isn't limited to accepting only one piece of data at a time.
Using multiple specifiers allows you to perform multiple inputs at once.
The following program takes two numbers as input using the scanf function and then displays them.

Source code
#include <stdio.h>

int main(void)
{
    int data1, data2;
    scanf("%d%d", &data1, &data2); /* Input section */
    printf("%d , %d\n", data1, data2);
    return 0;
}

Here's an example of the program's output:

Solution 2-1
100 200ใ€€Input Data
100 , 200ใ€€Displayed data

Here, we are using two %d specifiers to allow the input of two numbers at once.
When entering, please separate the two numbers with a space, tab, or newline.

By the way, the reason I'm declaring int data1, data2; in this program is...
It's a convenient way to declare multiple variables of the same type.
Rather than declaring them in two separate lines, like 'int data1; int data2;'
It's easier if we put it all on one line.

You can also enter the two numbers as separate types.
For example, if you provide a format specifier like %d%lf,
The first can be an integer, and the second can be a real number.

You can also specify the delimiter to use.
In this case, specify a separator between the two specifiers.
The following program prompts the user to enter two numbers, separated by a comma.

Source code
#include <stdio.h>

int main(void)
{
    int data1, data2;
    scanf("%d,%d", &data1, &data2); /* Input section */
    printf("%d , %d\n", data1, data2);
    return 0;
}

Here's an example of the program's output:

Solution 2-1
100,200ใ€€Input Data
100 , 200ใ€€Displayed data

This time, you will need to enter values separated by commas.
Instead, be aware that it will no longer be separated by spaces.

Simple Sigma Program

I'm not aiming to explain new concepts here, but rather to build a practical program.
The topic will be Simplified Sigma Calculation.

Sigma is something you learn in high school math, but it's not really that difficult.
Calculating sums like 1+2+3+4+5+โ€ฆ+100 is what sigma notation represents.
For simplicity, let's calculate the sum of all integers between the minimum and maximum values.

Before we start writing the program, let me explain the formula.
The sum can be calculated as (min+max)ร—(max-min+1)รท2.

First, it's clear that this program requires two integer inputs.
And you'll need to calculate it and display the result.
Of course, all of this is achievable with just the knowledge we've learned so far.

Of course, the `scanf` function will be used for inputting the two numbers.
With the scanf function, no explanatory prompt is displayed before input.
We will use the printf function to display the explanation beforehand.

Now that we've thought this far, all that's left is to write the program.
The following program calculates and displays the sum of integers between a minimum and maximum value.

Source code
#include <stdio.h>

int main(void)
{
    int min, max, sum;

    /* Input section */
    printf("Please enter the minimum and maximum values separated by a comma.:");
    scanf("%d , %d", &min, &max);

    /* Calculation Section */
    sum = (min + max) * (max - min + 1) / 2;

    /* Display section */
    printf("The total for %d to %d is %d.ใ€‚\n", min, max, sum);
    return 0;
}

Here's an example of the program's output:

Solution 2-1
Enter the minimum and maximum values separated by a comma: 100,200
The total for 100 to 200 is 15150.

This section contains a lot of tips for good programs, so let's go through them step by step.

First, pay attention to the results.
โ€œ, separated by, clearly indicates the delimiters as well.โ€
We make it clear by displaying the results, including showing the values from the input range.

The program declares three variables: min, max, and sum.
Each of these words represents minimum, maximum, and sum, respectively.
It's clear what the variable is used for.

I think variable names should be descriptive and ideally between 3 and 8 characters long.
You don't need to be overly concerned with the exact English meaning, so for example with sum,
another total, or
Options like answer, solve, or solution could also be considered,
I think it would also be a good idea to represent it as goukei kotae in Roman letters.
Whatever the case, avoid meaningless variable names and single-character variable names.

Please also note that the program is properly segmented and commented.
This program is divided into three stages: input, calculation, and display, and it is commented.
This could also be combined into a single process for calculation and display.
We've deliberately divided it here for better clarity.

The Drawbacks of Comments
Many introductory books recommend adding comments, so some people end up adding an excessive number of them. However, comments are unnecessary for simple parts that are self-explanatory. Furthermore, if comments remain unchanged after modifying the program, they become mismatched with the actual code, leading to misunderstandings.

The author recommends writing comments roughly at each major section break, as shown in this example. There are various styles for adding comments, and there is no single correct way.

It's important for programs to be understandable by both those who create them and those who use them.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.


Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. newline character
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19

Loading comment system...