Introduction to C language

“This is the 25th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

About the author

  • The authors introduce

🍓 blog home page: author’s home page 🍓 Introduction: JAVA quality creator 🥇, a junior student 🎓, participated in various provincial and national competitions during school, and won a series of honors


Introduction to C language

As soon as C language appeared, it was rapidly popularized and promoted all over the world with its features of rich functions, strong expression ability, flexibility and convenience, and wide application. C language not only has high execution efficiency and good portability, can be used to develop application software, drivers, operating systems and so on. C is also the ancestor of many other high-level languages, so learning C is a required course to enter the programming world.

A simple if statement with a branching structure

C language in the branch structure statement if conditional statement.

The basic structure of a simple if statement is as follows:

if(expression) {execute code block; }Copy the code

The semantics are: if the value of the expression is true, the following statement is executed; otherwise, the statement is not executed.

Note: if() is not followed by a semicolon, just {}

Simple if-else statements for branching structures

Basic structure of a simple if-else statement:

The semantics are: block 1 is executed if the value of the expression is true, block 2 is executed otherwise.

Note:

If () doesn’t have a semicolon, just say {},else doesn’t have a semicolon, just say {}

Multiple if-else statements for branching structures

C multiple if-else statements, which have the following structure:

The semantics are: judge the values of expressions in turn. When a value is true, the corresponding code block is executed; otherwise, code block N is executed.

Note: When a condition is true, no other statements of the branch structure are executed down.

Nested if-else statements for branch structures

Nested if-else statements in C. Nested if-else statement means that inside an if-else statement, we write an if-else statement. Its general form is:

Loop structure while loop

Performing an action over and over again is known as a loop.

There are three types of loop structures in C, so let’s look at the structure of a while loop in C

Where the expression represents the loop condition and the executing code block is the loop body.

The semantics of the while statement are: evaluates the value of the expression, and when the value is true (non-zero), executes the loop body block.

  1. An expression in a while statement is typically a relational or logical expression. If the expression is false, the body of the loop is not executed; otherwise, the body of the loop is always executed.
  2. Always remember to change the value of the loop variable in the body of the loop, otherwise an infinite loop (endless execution) will occur.
  3. Must be used if the body of the loop contains more than one statement{}To form a compound statement.

Do -while loop of loop structure

The general form of the do-while loop in C is as follows:

The semantics of the do-while loop statement are:

It executes the block of execution code in the loop, then determines whether the expression in the while is true and continues the loop if so. If false, the loop is terminated. Therefore, the do-while loop must execute the statement at least once.

Note: When using a do-while structure statement, a semicolon must follow the while parenthesis.

For loop (1)

General form of for loop in C:

Its execution process is as follows:

  1. Execute expression 1 to initialize the loop variable;
  2. Check expression 2, if its value is true (non-zero), executes the code block in the body of the for loop, and then executes downward; If the value is false (0), the loop ends;
  3. Execute expression 3, (i++), etc., on loop variables;
  4. Execute the second step after executing the code block in the for loop; The first initialization is performed only once.
  5. The loop ends and the program continues down.

Note: The two semicolons in the for loop must be written

For Loop structure (2)

In the for loop:

  • Expression 1 is one or more assignment statements that control the initial value of a variable;
  • Expression 2 is a relational expression that determines when to exit the loop;
  • Expression 3 is the step value of the loop variable and defines how the control loop variable changes after each loop.
  • The three parts are separated by a semicolon;To separate.

When using the for statement, note:

  1. Expressions 1, 2, and 3 in the for loop may not be empty, but two semicolons(;;)There can be no default.
  2. Omit “expression 1 (assigning initial value to loop variables)” to indicate that no initial value is assigned to loop variables.
  3. Omit “expression 2(loop condition)”, do no other processing, loop continues (infinite loop).
  4. Omit “expression 3(increase or decrease of loop variable)”, do no other processing, the loop keeps executing (infinite loop).
  5. Expression 1 can be an assignment expression that sets the initial value of a loop variable, or it can be any other expression.
  6. Expressions 1 and 3 can be one simple expression or multiple expressions separated by commas.
  7. Expression 2 is generally a relational or logical expression, but can also be a numeric or character expression, and executes the body of the loop as long as its value is non-zero.
  8. Variables in each expression must be defined before the for loop.

How do I get the hundreds, tens, and ones digits of a number

  • The digits:num/100Can be obtained becauseintIs an integer, and the decimal part is omitted. Such as765/100As a result of the7
  • Ten digits:num%100/10. Such as765% by 100First get65.65/10get6
  • Single digits:num%10.765% 10get5

Comparison of three kinds of cyclic structures

While, do-while and for loops are different in specific situations, as follows:

  1. It is better to use for loops when you know the number of loops;
  1. A while or do-while loop is useful when you do not know the number of loops:
    • Consider using a while loop if it is possible not to loop once
    • Do -while loops should be considered if they loop at least once.

But essentially, while,do-while, and for loops are interchangeable.

Multiple cycles of a cyclic structure

Multiple cycles are cycles within cycles of cycles.

In practice, a maximum of three levels of recirculation are used.

Because the more cyclic layers, the longer the running time, the more complex the program, so generally use 2-3 layers of multiple cycles. In addition, different loops can be nested.

Multiple loops in the execution process, the outer loop is the parent loop, the inner loop is the child loop,

** The parent loop is executed once, and the child loop needs to be completed until the loop is broken. ** The parent loop enters the next time, and the child loop continues execution…

Prints the triangular star heap

#include <stdio.h>
int main(a)
{
    int i, j, k;
    for(i=1; i<5; i++)
    {
        /* Observe the number of Spaces in each line and complete the loop condition */
        for(j=i; j<5; j++)  
        {
            printf("");    // Output Spaces
        }
        /* Observe the number of * signs in each line and complete the loop condition */
        for( k=0; k<2*i- 1; k++) {printf("*");   // * for each line output
        }
        printf("\n");     // Wrap each loop
    }
    return 0;
}
Copy the code

Print the 9×9 times table using the for loop

#include <stdio.h>
int main(a) 
{ 
    // Define the multiply numbers I,j and result
    int i, j, result;
     for(i=9; i>=1; i--) {for(j=1; j<=i; j++) {printf("%d*%d=%d ",i,j,result=i*j);
        }
        printf("\n");
     }
    return 0;
}
Copy the code

The break statement that terminates the statement

So on the fifth cycle, you have to stop training. In C, you can use the break statement to do this.

Note the following when using the break statement:

  1. Break cannot be used in a separate if-else statement without a loop structure.
  2. In a multi-level loop, a break statement only breaks out of the current loop.

The continue statement that terminates the statement

So on the fifth cycle, you have to stop and continue training. In C, you can do this using the continue statement

The continue statement ends the current loop and begins the next loop.

The difference between a break statement and a continue statement is that:

Break is to break out of the current loop, and continue is to end the current loop and start the next loop.

Switch statements for branching structures

The switch statement structure is as follows:

Note the following when using the switch statement:

  1. The value of each constant expression after case cannot be the same; otherwise, an error will occur.
  2. If there is no break after the case clause; ** is executed backwards until break is encountered; ** will jump from the switch statement.
  3. The expression statement following switch can only be an integer or a character.
  4. Multiple statements are allowed after a case, and ** may not be enclosed in {}**.
  5. The order of case and default clauses can be changed without affecting the execution result of the program.
  6. defaultThe clause can be omitted.

Switch and if statements (counting the day of the year)

#include <stdio.h>

int main(a) 

{ 

    /* Define the date to be computed */

    int date = 0;

    int year = 2008;

    int month = 8;

    int day = 8;

    switch(month)

    {

        case 12:date+=30;

        case 11:date+=31;

        case 10:date+=30;

        case 9:date+=31;
    
        case 8:date+=31;

        case 7:date+=30;

        case 6:date+=31;

        case 5:date+=30;

        case 4:date+=31;

        case 3:

        if((year%4= =0&&year%100! =0)||year%400= =0)

        {

            date+=29;

        }

        else

        {

            date+=28;

        }
        case 2:

        date+=31;

        case 1:

        date+=day;

        printf("%d year % D month %d day is the %d day of that year",year,month,day,date);

        break;

        default:

        printf("error");

        break;

    }

    return 0;

}
Copy the code

Correct: continue can only be used in circulatory bodies

The infamous goto statement

C also has a goto statement, which is an unconditional branching statement.

The format for the goto statement is:

gotoStatement label;Copy the code

After the language

The original intention of the director to write blog is very simple, I hope everyone in the process of learning less detours, learn more things, to their own help to leave your praise 👍 or pay attention to ➕ are the biggest support for me, your attention and praise to the director every day more power.

If you don’t understand one part of the article, you can reply to me in the comment section. Let’s discuss, learn and progress together!

Wechat (Z613500) or QQ (1016942589) for detailed communication.