About C Language

Introduction to C Language

C is a general-purpose high-level language originally designed by Dennis Ritchie at Bell LABS for the development of the UNIX operating system. C was first implemented in 1972 on a DEC PDP-11 computer. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and almost all UNIX applications are written in C. C has now become a widely used professional language for a variety of reasons.

Features of C language

  • Easy to learn.
  • Structured languages.
  • It produces efficient procedures.
  • It can handle the underlying activities.
  • It compiles on a variety of computer platforms.

No man is perfect. C also has some disadvantages. Pointers are the soul of C, but errors involving Pointers are often hard to detect. To enjoy programming freely in C, you have to take on more responsibility. Shortcomings of this point so far, otherwise to ridicule, printf(” Chuheridangwu, learn C really his mother bitter 😢”);

The application scope of C language

C was originally used for system development work, especially the programs that make up an operating system. Because the code running speed produced by C language is almost the same as that written by assembly language, C language is used as the system development language. Here are a few examples of using C:

  • The operating system
  • Language compiler
  • The compiler
  • Text editor
  • The printer
  • Network driver
  • Modern program
  • The database
  • Language interpreter
  • The entity tool

The first C language program

I’m not going to say much more about what C is good for and why we need to learn C, but let’s get right to the subject of today, writing our first C program.

Just like learning any other language, start with Hello World

VS2019 is one of the three programming tools introduced earlier. If you have any questions, please refer to the previous article.

code

#include<stdio.h>
// For more C language dry goods, please search on wechat [C Language Chinese community]
int main(a) {                   /* Main function */
    printf("hello world! \n");  /* Output hello world! * /
    return 0;                  Return to * / / *
}
Copy the code

Ctrl+F5 run, the result is as follows:

The first program to get started, HelloWorld, is complete. #include

includes commands for files. Stdio.h is a header file that contains some basic elements needed in programs. Printf () is one of them.

Here we define a main() function, int is the return type, printf() outputs the string “hello world”, and “\n” is a newline character, which is used to output a newline after “hello world”. Return 0: returns.

In the code “/ *… */” is a comment, its content does not participate in the program run. “\\” can also be used for single-line comments in C.

The following figure summarizes the parts that make up the C programThe picture is from

7 steps to write a C program

To give you an idea of what programming is all about, the process of writing A C program is broken down into seven steps. This is ideal, of course. I won’t go through each of these steps in detail here, but you can understand them in subsequent articles.

Programming mechanism

Let’s take a look at the process from source code to executable on Windows and Unix systems

  • Windows

  • Unix

C basic algorithm

Basic characteristics of the algorithm

Algorithm includes two aspects: algorithm design and algorithm analysis

Algorithm design is actually an implementation process designed for a specific type of problem. The algorithm has the following characteristics:

  • Have a poor sexual
  • deterministic
  • The feasibility of
  • The input
  • The output

That is to say, we are designing the algorithm to satisfy the properties described above. Of course, there are good and bad algorithms, so how do we measure the pros and cons of an algorithm?

In fact, algorithm analysis is to measure the advantages and disadvantages of an algorithm, usually from the following aspects:

  1. correctness
  2. readability
  3. Robustness,
  4. Time complexity and space complexity

The expression of the algorithm

Common methods used to describe an algorithm are: natural language, flow charts, n-S diagrams, etc.

Natural language

Natural language is easy to understand, so let’s look at a concrete example. Demand: arbitrary input 3 numbers, find out the smallest number.

(1) Four variables are defined as a, B, C and min.

(2) Input three numbers with different sizes to a, B and C respectively.

(3) Judge whether a is less than B. If so, assign the value of A to min; otherwise, assign the value of B to min.

(4) Determine whether min is less than C. If so, perform (5); otherwise, assign the value of C to min.

(5) Output min.

The advantage of this approach is that it is easy to understand, but when it comes to complex algorithms, natural language is not very convenient.

The flow chart

Flow chart is to use some boxes to represent the operation of different properties, with flow line to indicate the direction of the algorithm. His characteristic is intuitive image, application is very wide. The following figure shows the symbols and meanings of flow charts

Flowchart has three basic structures: sequential structure, selection structure and cyclic structure.

  • Sequential structure: A sequential structure is a simple linear structure

  • Selection structures: Selection structures are also called branching structures

  • Loop structure: repeated execution of a series of operations, until the condition is not determined to terminate.

Let’s use a flow chart to represent the above requirements

Navier-stokes flow chart

N-s flow chart is to write all the algorithm in a rectangular box, leaving out the flow line in the flow chart. Here is another example: Requirement: Enter a number and check whether it is prime.

The basic properties of the algorithm and the representation of the algorithm are all covered.

The data type

Basic types of

Primitive types are the basic data types we use in C, including integer (short, primitive, long), character, floating point (single, double), and enumeration types.

Structural type

It is not enough to use only basic types in programming. To meet the requirements, you need to use types other than basic types, such as constructed types. Construct types You can use primitive types to construct new data types to meet actual development requirements. Constructor types come in three forms: array type, struct type, and common type.

Pointer types

Pointers are the cream of THE C language. Many programming languages support Pointers in some form, but C is the most famous. A pointer is special in that its value is a memory address. We’ll explain more about Pointers later.

Empty type

The main purpose of void types is to qualify function returns and parameters.

To facilitate memorization, I made a simple mind map of the data types as follows:

constant

This part of the content will not be described in too many words, from the following mind map we can intuitively understand.For symbolic constants let’s look at a simple piece of code

#include<stdio.h>

#define3.14 PI?/* Defines the symbolic constant */
// For more C language dry goods, please search on wechat [C Language Chinese community]
int main(a) {
    double r=5.0;/* Define the radius */
    double area;/* Area variable */
    area = PI*r*r;/* Calculate the area */
    printf("The area of the circle is: %f\n",area);/* Prints out */
    return 0;
}
Copy the code

Flow control statement

Loop control statement

C has three kinds of loops

  • for
  • while
  • do while

The while loop

Let’s take a look at the while loop. The flow chart is as follows:Let’s look at a little example

The do while loop

The flow chart is as follows:Let’s look at a little exampleThe two small examples run with the same results.

The for loop

The flow chart is as follows:The execution of the for loop can be roughly divided into four steps

  • Execute expression 1, only once
  • Evaluate the value of representation 2 to see if it is true (non-zero). If true, execute the body part of the loop; otherwise, exit and execute the statement below the for loop
  • Execution loop body
  • Evaluate expression 3 and return to step 2

Let’s look at an example of a for loop We implemented the multiplication table with two nested for loops

Conditional control statement

Let’s start with a simple example:

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
// For more C language dry goods, please search on wechat [C Language Chinese community]
int main(a) {
    
    int score;
    printf("Please enter student's math score:");
    scanf("%d", &score);
    if (score < 60 && score >= 0)
    {
        printf("The student's math score was classified as: poor \n");
    }
    else if (score < 70 && score >= 60)
    {
        printf("The student's grade in mathematics is: pass \n");
    }
    else if (score < 80 && score >= 70)
    {
        printf("The student's grade in mathematics is: medium \ N");
    }
    else if (score < 90 && score >= 80)
    {
        printf("The student's math grade is: good \n");
    }
    else if (score < 100 && score >= 90)
    {
        printf("The student's grade in mathematics is: a \ N");
    }
    return 0;
}
Copy the code

This example uses the keywords if, else if, else, and so on, the keywords of the loop. Let’s look at them in detail

The flowchart of the if statement is as followsThe if statement is used to determine whether a given condition is met and determine whether to perform an operation based on the result. The if statement can be used as follows:The if else statement flow diagram is as follows:The flow chart of multiple if statements is as follows:Here is not much explanation, such as what do not understand you can practice the example again to consider, you will feel So easy!

Sample control statement

What is the maximum value of a,b, and c?

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
// For more C language dry goods, please search on wechat [C Language Chinese community]
int main(a) {
    int a, b, c;
    int max1, max2;
    a = 8;
    b = 12;
    c = 10;
    if (a>b)
    {
        if (a>c)
        {
            max1 = a;
        }
        else{ max1 = c; }}else
    {
        if (b>c)
        {
            max1 = b;
        }
        else{ max1 = c; }}// A ternary expression

    max2 = a > b ? (a > c ? a : c) : (b > c ? b : c);
    printf("the max number is %d\n", max1);
    printf("the max number is %d\n", max2);
    return 0;
}
Copy the code

The results of