learn through suffering C language learn through suffering 
C language

Comparative sentences

Conditional judgment

All the programs I've created so far have been one-way programs.
In other words, the statements are executed in order, from beginning to end.
It was the simplest mechanism: that when the sentence ended, it would be over.

For truly simple programs, this might just work.
Whenever I try to make it do even a slightly complex task, it quickly gets stuck.

To solve this problem, we will vary the processing based on the conditions.

Lunch decisions
If you have money to spare, go for the yakiniku set meal. If you're tight on cash, stick to plain udon.

It will require judgments based on conditions or nuances.

In the world of computers, conditions are nothing more than numeric values.
To be more specific, the comparison of two numbers can be the condition itself.

Conditional sentences

C language provides the if statement as a statement for making decisions based on conditions.
An if statement compares the values of two numbers and then performs different actions based on the result.
The usage of if statements is as follows:

How to Use If Statements
if (condition) statement;

Numerical comparisons using if statements are very clear and straightforward.
In essence, it simply determines whether the specified number is zero or not.
In C, when performing conditional judgments, we refer to 0 as a false value and anything other than 0 as a true value.

Keyword
【The true value】

In conditional statements, it refers to a non-zero value (even negative numbers). It may also be written as true.


Keyword
【False values】

In conditional statements, it denotes 0.
It may also be written as false.

In an if statement, the following statement is executed only if the specified number is true (non-zero).
Otherwise, the line is skipped, and the statement after the if statement is executed.
The following program displays a number only when the number is true (i.e., not zero).

Source code
#include <stdio.h>

int main(void)
{
    int suuti = 10;
    if (suuti)
        printf("%d\n", suuti);
    return 0;
}

The results of this program are as follows:

Results
10

In this program, we have int suuti=10;
This is how to declare and assign a value at the same time.
This way of writing is sometimes called initialization because it allows you to determine the value of a variable initially.

Keyword
【Initialization】

Declaring a variable and assigning a numerical value to it simultaneously.

In this program, if the value of suuti is set to 0, nothing is displayed.
Other values will display that value.

Comparison operators

You might think, after reading the previous section, that if statements are rather useless.
Since all it can do is determine whether it's zero or not.

However, combining standard calculations with if statements allows for more advanced comparisons.
For example, when you subtract a number from itself, the answer is naturally 0.
By utilizing this property, value determination can be achieved through subtraction.
The following program exploits this property to determine if the input number is 10.

Source code
#include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);
    if (suuti - 10)
        printf("The input value is not 10.\n");
    return 0;
}

If you run this program and input 10, the result will be as follows:

Results
10 Input Data

If you run this program and enter a number other than 10, the result will be as follows:

Results
135 Input Data
The input value is not 10.

If this method is applied in a more complex way, it would allow for highly sophisticated comparisons.

However, it's inconvenient to have to subtract just to check if a variable's value is 10.

Therefore, C has operators dedicated solely for comparison.
When checking if two numbers are equal, use the == operator.
This operator performs a calculation that results in true when two values are equal.

= and ==
Even professionals often accidentally confuse `=` and `==`. `=` assigns to the variable on the left, while `==` compares whether the values on both sides are equal. When things don't work as expected, start by checking this point.

The following program is an example of checking if an input value is 10 using the == operator.

Source code
#include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);
    if (suuti == 10)
        printf("The input value is 10.\n");
    return 0;
}

If you run this program and input 10, the result will be as follows:

Results
10 Input Data
The input value is 10.

If you run this program and enter a number other than 10, the result will be as follows:

Results
135 Input Data

Although the show/hide functionality is reversed from before, the assessment is still being performed correctly.


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. line break
  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...