C language basic content combing

content

1. Overview of data types

Two, variables, constants

1. Constant

2. Variables

Three, strings, escape characters

3.1 Strings

3.2 Escape characters


Hello friends! ! ! I haven't updated my blog for a long time. It's not because I'm lazy. University is really not easy. Today I finally have free time to organize what I have learned.

1. Overview of data types

Data type Memory size 
 char //Character data type 1 byte 
 int //Integer 4 byte 
 short //Short integer 2 byte 
 long //Long integer 4 byte 
 long long //Longer integer 8 byte 
 float / /Single-precision floating-point type 4 byte 
 double //Double-precision floating-point type 8 bytes

Two, variables, constants

1. Constant

Values ​​that cannot be changed while the program is running are called constants. Constants are divided into the following categories.

2. Variables

A value that can change while the program is running.

2.1. Division of variables

What? can't read? ? ? So let's go straight to the example

#include <stdio.h> 
int global = 2019;//Global variable 
int main() 
{ 
    int local = 2018;//Local variable   
    int global = 2020;//Local variable 
    printf("global = %d\n", global); 
    return 0; 
}

In short, the variables outside the braces are called global variables, and those inside the braces are called global variables.

So now comes the problem. I have defined two globals, so what about the output of 2020 when the program is running? Or 2019?

Let's take a look at the running results

Obviously the final output is 2020. Then we get a conclusion: when a local variable and a global variable have the same name, the local variable takes precedence .

2.2 Scope and Lifecycle of Variables

People are born, old, sick and die, and flowers bloom and fall. Variables are no exception, it also has its scope and lifespan.

Lifecycle refers to the period of time between the creation of a variable and the destruction of the variable.

Scope refers to the code scope of the availability of names used in a piece of program code.

So what is the difference between the lifetime scope of a local variable and a global variable? ?

Three, strings, escape characters

3.1 Strings

"hello world"

This string of characters surrounded by double quotes is called a string.

#include <stdio.h> 
//The following code, what is the print result? 
int main() 
{ 
    char arr1[] = "abc"; 
    char arr2[] = {'a', 'b', 'c'}; 
    char arr3[] = {'a', 'b', 'c' , '\0'}; 
    printf("%s\n", arr1); 
    printf("%s\n", arr2); 
    printf("%s\n", arr3); 
    return 0; 
}

It seems that the second result is different from what we imagined, why is there garbled characters? ? ?

Let's go back to the above to compare our code, and find that the result is completely different after the third line has a /0 more than the second line. Then the problem should be at this /0.

In fact, there will be a \0 at the back of the string to tell the computer that the string has ended, and there is no \0 when inputting a single character, so when printing the second time, because the computer did not detect the \ 0, so it will keep printing backward until it finds a \0 to stop it.

3.2 Escape characters

escape character Paraphrase
\? Use when writing multiple question marks in a row
\' Used to represent character constants
\" double quotes used to denote a string
\\ used to denote a backslash
\a warning character, beep
\b backspace character
\f paper feed character
\n newline
\r Enter
\t horizontal tab
\v vertical tab
\ddd ddd means 1-3 octal digits
\ xdd dd means two hexadecimal digits

Guess you like

Origin blog.csdn.net/m0_60447315/article/details/120985505