1. First knowing C language

1.1 origin of C language

In 1972, Dennis Ritchie and Ken Thompson of Bell LABS designed C while developing the UNIX operating system. C was originally designed as a programming tool for programmers.

1.2 reasons for choosing C language

1. Design characteristics

  • The design philosophy of C language makes it easy for users to complete top-down planning, structured programming and modular design.
  • Programs written in C are easier to understand and more reliable.

2. Efficiency

C is an efficient language. C program is compact and fast. Have fine tuning control ability of assembly language.

3. Portability

C programs written on one system can run on other systems with little modification.

4. Powerful and flexible

C is powerful and flexible (two words that are often used in computing). For example, the powerful and flexible UNIX operating system is largely written in C. Many compilers and interpreters for other languages (FORTRAN, Perl, Python, Pascal, LISP, Logo, BASIC) are written in C.

5. For programmers

6 and disadvantages

C language uses Pointers, which are prone to hard-to-find errors.

1.3 C language standards

  • The 1989 ANSI C standard defines the C language and the C standard library (often referred to as the C89 standard or C90 standard)
  • In 1994 the C99
  • But this standard is not supported by all compilers.
  • Standards vary a little from generation to generation, but not by much.

1.4 7 Steps to use C language

  1. Define the goal of the program

  2. Design program

  3. Write the code

  4. compile

    1. As mentioned earlier, a compiler is a program that converts source code into executable code.
    2. The C compiler is responsible for translating C code into a specific machine language.
    3. In addition, the C compiler combines source code with code from the C library, which contains a large number of standard functions for users to use, such as printf() and scanf(), into the final program
    4. More precisely, library functions are linked by a program called a linker, but on most systems the compiler runs the linker.
    5. The result is an executable that the user can run.exeContains code that computers can understand.
    6. The compiler also checks that C programs are valid. If the C compiler finds an error, it does not generate the executable and reports an error.
  5. To run the program

  6. Test and debug

  7. Maintain and modify code

  8. instructions

Programming is not a linear process like the steps 1 through 7 mentioned above, sometimes iterating between different steps. (For example, you may need to re-analyze the problem if you find a problem with the way you wrote it during testing)

1.5. Programming mechanism

  • C: source file
  • H: header file

The basic strategy of C programming is to programmatically convert source files into executable files (containing machine language code that can be run directly). A typical C implementation does this through two steps: compilation and linking.

  • The compiler converts source code into intermediate code
  • The linker merges intermediate code with other code (library code, startup code) to generate an executable. Exe file.

C uses this divide-and-conquer approach to make it easy to modularize programs, allowing individual modules to be compiled independently, and later merging (loading) compiled modules with the linker. This way, if you change only one module, you don’t have to recompile the other modules. In addition, the linker merges the program you write with the precompiled library code.

2. Introduction to C language

2.1. Simple C program example

First. C program

#include <stdio.h>
int main(void)                /* A simple C program */
{
    int num;                  /* Define a variable named num */
    num = 1;                  /* Give num a value */
    
    printf("I am a simple "); /* Use the printf() function */
    printf("computer.\n");
    printf("My favorite number is %d because it is first. \n", num);
    return 0;
}
Copy the code

Print result:

I am a simple computer.
My favorite number is 1 because it is first.
Copy the code

Note: I wrote this program under CLion. In addition, if the program is running and flashes by, you can return 0; The statement is preceded by getchar(); This means that you can make the program wait for the back window to close by pressing a key.

2.2. Example explanation

2.3. Structure of simple program

  • A program consists of one or more functions.
  • There must be a main function.
  • A function consists of a function header and a function body.
  • The function header format is the return value type function name (the parameter to be passed in)
  • The function size is {… } are statements enclosed in curly braces

A general simple C program format is as follows:

#include<stdio.h>
Int main(void){statementreturn 0;
}
Copy the code

2.4 skills to improve program readability

  1. Choose meaningful function and variable names, especially if the program is large, and try to give the variables and functions a good name, do not confuse
  2. Reasonable comment
  3. Separate conceptual parts with blank lines
  4. Each statement has a single line

2.5. C

// fathm_ft.c -- Convert 2 fathoms to feet
#include <stdio.h> 
int main(void) { 
    int feet, fathoms; 
    fathoms = 2; 
    feet = 6 * fathoms; 
    printf("There are %d feet in %d fathoms! \n", feet, fathoms); 
    printf("Yes, I said %d feet! \n".6 * fathoms); 
    return 0; 
}
Copy the code

2.5.1 Multiple statements

The program declares two variables in a single declaration, not one. To do this, separate two variables (feet and fathoms) with commas in the declaration. In other words,

int feet, fathoms;
Copy the code

and

int feet;
int fathoms;
Copy the code

equivalent

2.6. Multiple functions

A program can contain more than one function, or it can contain its own functions.

/* two_func.c -- A file containing two functions */ 
#include <stdio.h> 
void butler(void); /* ANSI/ISO C function prototype */ 
int main(void) { 
    printf("I will summon the butler function.\n"); 
    butler(); // Call the Butler function
    printf("Yes. Bring me some tea and writeable DVDs.\n"); 
    
    return 0; 
} 

void butler(void) /* Function definition starts */ 
{ 
    printf("You rang, sir? \n"); 
}
Copy the code

The results are as follows:

I will summon the butler function. 
You rang, sir? 
Yes. Bring me some tea and writeable DVDs.
Copy the code

The Butler () function appears three times in the program:

  • The first is the function prototype, which tells the compiler to use this function in the program.
  • The second appears as a function call in main();
  • The last time it appears is in a function definition, which is the source code for the function itself.

The C standard recommends that you provide function prototypes for all functions used in your program. Standard include files provide function prototypes for standard library functions. For example, in the C standard, the stdio.h file contains a function prototype for printf(). The last example in Chapter 6 shows how to use a function with a return value, and Chapter 9 covers functions in detail.

2.7 Debug

There are two common errors: grammatical errors and logical errors, and don’t forget the semicolon at the end of every sentence

Common ways to locate errors:

  • Insert printf() at key points to print the current value

  • Ide of the debug

2.8 keywords and reserved identifiers

Keywords are C language vocabulary. They are special to C and cannot be used as identifiers (for example, variable names). Many keywords are used to specify different types, such as int. Other keywords, such as if, control the order in which statements are executed in a program. In the C keywords listed in Table 2.2, the keywords in bold are those added to the C90 standard, the keywords in italics are those added to the C99 standard, and the keywords in bold and italics are those added to the C11 standard.

If you use a keyword incorrectly (for example, as a variable name), the compiler will treat it as a syntax error. There are also reserved identifiers that C already specifies their purpose or reserves their right to use, which can cause problems if you use these identifiers to mean something else. So while they are also effective name, not cause | syntax errors, also can’t use it. Reserved identifiers include those starting with an underscore character and library function names, such as printf().