A preliminary and simple understanding of C language

Table of contents

————1. A brief introduction to C language

————2. The first C language program

————3. Common keywords

————4. Data type

————5. Constants

————6. Variables

————7. Operator

————8. Select statement

————9. Loop statement

————10. Definition of function

————11. Array


Preface: This article is a simple introduction to C language and simple knowledge of C language, so that it can serve as a review for forgotten basic knowledge.

1. A brief introduction to C language

C language is a computer language. After writing a C program, it must be edited, compiled, linked , and finally an executable program (.exe) file is generated. Select the appropriate editor vs2022), input the C language source program into the computer through the keyboard, and save it to the disk in the form of a file (.c or .cpp), and then compile it to translate the source program into a target program in binary form. (.obj), and finally start linking, connecting the compiled target file with the system's function library and other target programs to generate an executable program (.exe)

The process is roughly like this:

 In fact, C language is a high-level language, similar to our human language.

2. The first C language program

As shown in the picture:

   

 The following is a brief introduction, from top to bottom.

1. #include <stdio.h> is a header file

2. The main function is a main function. There can only be one and only one file in a project. The int in front represents an integer, which is 4 bytes;

        Inside the curly braces is the main body of the main function, which is the code you want to write.

3. Printf is a library function that is native to the computer and can be used for printout. The content in double quotes is the content you want to print (""). Finally, return 0 is the return value, and each paragraph in the main function There is a semicolon " ; ", which means the conclusion.

3. Common keywords

  

4. Data type

Bytes from small to large:

char——Character type ( 1 byte)

short - short integer ( 2 bytes)

int——integer type ( 4 bytes)

long - long integer type ( 4 bytes)

float - single precision floating point number ( 4 bytes)

long long - long integer type ( 8 bytes)

double - double precision floating point number ( 8 bytes)

Supplement: sizeof can be used to calculate the number of bytes of a data type, and sizeof is an operator, not a function. The ( ) in sizeof ( ) can be omitted. (To be distinguished from strlen, strlen is used to count the number of characters in the space. When encountering " \0 ", the calculation stops and the length after (" \0 ") is not calculated),

5. Constants and variables

Constant: It is an unchanging quantity, such as pi, gender, and the number of days in a week.

Variable: It is a quantity that can be changed, such as age and weight.


constant:

 1. Integer constant (the value range of an integer constant is limited, and its size depends on the type of the integer)

————(1) Decimal integer: data consisting of numbers from 0 to 9 that do not start with 0

                        Example: 0, 65, 83

————(2) Octal integer: data composed of numbers from 0 to 7 starting with 0

                        Example: 00,071,0123

————(3) Hexadecimal integer: It is data composed of numbers from 0 to 9 starting with 0x or 0X and letters from A(a) to F(f)

                        Example: 0x0, 0X0, 0x55, 0X55, 0x3f, 0X3f.

 

2. Real constants ( floating point constants )

(1) Decimal number format: composed of digits 0~9 and decimal point.

        Examples: 0.0, 25.0, 5.789, 0.13, 5.0, 300., -267, 8230, .35, 43.

(2) Exponential form: composed of decimal, plus exponent code sign "e" or "E" and exponent code (can only be an integer, can be signed).

        General form: a E n (a is a decimal number, n is a decimal integer), its value is a*10^n.

        Example: 2.3E5 (2.3*10 to the fifth power), 3.4E-2 (3.4*10 to the -2 power), -2.7e-2 (2.7*10 to the -2 power).

Supplement: By default, real (floating-point) constants are recognized as double-precision floating-point numbers. Adding the suffix f or F indicates single-precision float-type floating-point numbers, and the suffix l or L indicates double-precision double-type floating-point numbers.

————Example: 31.12 L is double type

                      31.12 f is of type float

3. Character constants

(1), character constants

——A character constant is a character enclosed in single quotes.

        Such as: 'a', 'b', '+', '=', '?'

Features:

        (1). It can only be enclosed in single quotation marks , not double quotation marks or other brackets.

        (2) Character constants can only be single characters , not strings.

        (3) The character can be any character in the character set. But after a number is defined as a character type, it cannot participate in numerical operations .

                For example, '5' and 5 are different, and '5' is a character constant and cannot participate in operations.

(2), escape characters

        Definition: The characters after the backslash are converted into another meaning , which is different from the original meaning, so they are called "escape characters"

                The escape character is a special character constant, starting with a backslash "\" and followed by several characters.

Common escape character table:

 (4), string constant

——Definition: A sequence of characters enclosed by a pair of double quotes

        例:"CHINA","C program","14.5"等

Features:

1. Surrounded by double quotes

2. It can be one or more characters

3. You cannot assign a string constant to a character variable, but you can assign a character constant to a character variable.

        There is no string variable in C language, but a character array can be used to store characters.

(5), symbolic constants

        Definition: An identifier can be used to represent a constant. Symbolic constants must be defined before they can be used.

        General form: #define identifier constant

                        For example: #define p1 3.14 - the value of p1 is 3.14

6. Variables:

        Definition: <variable type specifier> <variable list> = <initial value>

        例:int a = 10;

        Features: (1) The variable type specifier determines the value range of the variable and the operation specifications that can be performed on the variable.

                   (2). The variable list consists of one or more variable names. When defining multiple variables, separate the variable names with commas.

                   (3) The initial value is optional. The variable can be assigned a value at the same time as it is defined, or it can be defined first and then assigned a value.

7. Operator                              

Arithmetic operators: + - * / %

Shift operator: >> <<

Bit operators: & ^ |

Assignment operators: = += -= *= /= &= ^= |= >>= <<= 

Unary operator: ! Logical inverse operation

                      - negative value

                      + positive value

                      & get address

                      sizeof type length of the operand (in bytes)

                      ~ Bitwise inversion of a number’s binary representation

                      -- front, rear --

                     ++ pre-, post-++

                     * Indirect access operator (dereference operator)

                     (type) cast

Relational operators: >

                      >=

                      <

                      <=

                      != is used to test for "inequality"

                      == is used to test "equality"

Logical operations : && logical AND

                   || Logical OR

Conditional operator: exp1 ? exp2 : exp3

Comma expressions: exp1, exp2, exp3, …expN

Subscripted references, function calls, and structure members: [] () . ->

8. Select statements

(1), if statement

                1. Single branch if statement

                        Syntax format: if (expression)

                                                statement;

Implementation process

                2. Double branch if statement

                        Syntax format:

                        if(expression)

                                Statement 1;

                        else

                                Statement 2;

Implementation process

                 3. Multi-branch if statement

                       Syntax format:

                        if(expression1)

                        Statement 1;

                        else if(expression 2)

                        Statement 2;

                        else if(expression 3)

                        Statement 3;

                        ........

                        else

                        statement n;

Implementation process

 Supplement, nesting is possible

(2), switch statement

Syntax format:

9. Loop statements

(1), while statement

                General form:

                        while(expression)

                                loop statement

(2), do while statement

               General form:

                        do

                        statement

                        while(expression)

(3), for statement

                General form:

                        for(expression1;expression2;expression3)

10. Definition of function

                Parameterless function definition form

                        type identifier function name()

                        {

                        declaration part

                        statement

                        }

               Parametric function definition form

                        Type identifier function name (formal parameter list column)

                {

                        declaration part

                        statement

                }

11. One-dimensional array

                Array definition

                        type specifier array name [constant expression]

        Subscript: starting from 0 and increasing in sequence

example:

int a[5]={1,2,3,4,5};          1        2        3        4        5

                                         a[0]   a[1]    a[2]    a[3]    a[4]    

                                                

Guess you like

Origin blog.csdn.net/cool_tao6/article/details/130043843