[Essential basic knowledge of C language 1] (Super detailed)

Table of contents

1. Use “hello world” to open the door to a new world

2. Basic data types

3. Variables and constants

3.1 Variables

 3.2 Use of variables

 3.3 Scope and life cycle of variables

3.3.1 Scope

  3.3.2 Life cycle

3.4 Constants

3.4.1 Literal constants

3.4.2 Const-modified constant variables

 3.4.3 #define defined identifier constants

 3.4.4 Enum constants

4. String

4.1 String

4.2 Constant string

5. Escape characters

6. Comments

Summarize


1. Use “hello world” to open the door to a new world

I believe that when many friends are learning a programming language, no matter what language it is, the first code they come into contact with will definitely be "hello world", and the learning process of C language is no exception. Let's take a look at this code:

#include <stdio.h>
int main()
{
    printf("hello world\n");
    return 0;
}
It is worth noting that in C language, the main function is the entry point of a program (ps: there is only one main function in a project). A complete program must have a main function; if not, the program will not run. The return type of the main function is generally int, and the return value is 0. The body of the main function is as follows:
int main()
{
    //函数中要实现的内容

    return 0;
}

The purpose of #include <stdio.h> is to allow us to access functions in the standard I/O library (such as scanf and printf). This set of functions is used to perform input and output.

2. Basic data types

In C language, there are only 4 basic data types - integer, floating point, pointer and aggregate types (such as arrays and structures, etc.).

Integer and floating point types are as follows:

char       //字符型
short      //短整型
int        //整型
long       //长整型
long long  //更长的整型
float      //单精度浮点数
double     //双精度浮点数

Note: Variables of type char are designed so that they can store character values.

It is worth mentioning that the reason why there is no string type in the data types of C language is because C language can store strings in the form of character arrays .

We can calculate the size of various data types through the keyword sizeof:

ps: The keyword sizeof is a keyword used to calculate the space occupied by a variable, and the unit is bytes.

After seeing this, someone may ask: Why are the calculated sizes of long and int the same? That's because the C language stipulates that the size of the long integer type should be at least the same as the integer type , and the size of the long type may be different in different compilers.

 Precisely because there are so many data types in the C language and the sizes of various types are different, various values ​​​​in life can be expressed more abundantly.

3. Variables and constants

3.1 Variables

Variables are divided into global variables and local variables

Let's take a look at the difference between global variables and local variables:

 Summary: Simply put, variables defined outside the largest curly braces (which can be understood as outside the main function and other functions) are global variables; variables defined within curly braces are local variables.

When a local variable has the same name as a global variable, the local variable takes precedence.

 3.2 Use of variables

  ps: When we use a variable, it is best to initialize the variable. If we do not initialize it, the variable will store a random value.

 3.3 Scope and life cycle of variables

3.3.1 Scope

Scope is a programming concept. Generally speaking, the name used in a piece of program code is not always valid/available, and the code range that limits the availability of the name is the scope of the name .

1. The scope of a local variable is the local scope where the variable is located. When out of this range, an error will be reported (undefined identifier) ​​if used again.

  2. The scope of global variables is the entire project (no matter whether it is in the same source file or not, when the global variable defined is not in the same source file, you only need to add the extern keyword to use the global variable).

  3.3.2 Life cycle

The life cycle of a variable refers to the period of time between the creation of the variable and its destruction.

1. The life cycle of local variables is: the life cycle begins when entering the scope and ends when it exits the scope .

2. The life cycle of global variables is: the life cycle of the entire program .

3.4 Constants

Constants in C language are divided into the following types:

3.4.1 Literal constants

int main()
{
    //字面常量演示
    6.66 //字面常量
    6000 //字面常量
    return 0;
}

3.4.2 Const-modified constant variables

When a variable is modified by const, it has constant attributes. Its value cannot be modified directly.

 But if you still want to modify it, you can modify it through a pointer.

  Although a variable has a constant attribute when it is modified by const, its essence is still a variable! !

 As shown in the figure, when defining an array, the value in [ ] must be a constant. Although p has a constant attribute, it is still a variable.

 3.4.3  #define defined identifier constants

 Through the definition of the arr array, we can find that MAX is actually a constant. The constant defined by the define keyword will replace all MAX with 1000 during the preprocessing stage.

 3.4.4 Enum constants

An enumeration type refers to a type whose values ​​are symbolic constants rather than literal values. They are declared in the following form:

enum sex
{
    male     //如果不特意声明的话,male的值为0,后面的依次加1
    female
    secret
}

At this time, male is 0, female is 1, and secret is 2.

If female is assigned a value of 20, then male is 0 and secret is 21.

4. String

4.1 String

"hello world\n" 

This string of characters enclosed in double quotes is called a string literal or simply a string .

ps: The end mark of the string is the escape character of \0. When calculating the length of the string, it ends when \0 is encountered and is not counted as the content of the string. Although \0 escape characters in a string do not count toward the length of the string, they still take up space.

 There are 6 characters in arr1, so the calculated length is 6, the space occupied by one character is one byte, and arr1 is calculated to be 7 bytes, which just confirms the ' \0 ' mentioned above. To take up space.

There are only 3 characters in arr2, but they do not contain '\0', so the obtained string is a random value.

4.2 Constant string

In the storage form of string constants, all characters and '\0' are stored somewhere in memory.

String constants are generally discussed together with pointers, because using string constants in a program will generate a "constant pointer to a character". When a string constant appears in an expression, the value used in the expression is the address where the characters are stored, not the characters themselves. Therefore, you can assign a string constant to a "pointer to characters", which points to the address where these characters are stored. However, you cannot assign a string constant to a character array because the direct value of a string constant is a pointer, not the characters themselves.

char    * message = "hello world! ";

This statement declares message as a pointer to a character and initializes the pointer with the address of the first character in the string constant. 

5. Escape characters

When we want to print a directory as "c:\code\test.c\n" on the screen,

How should we write code? Is the code below correct?

 We can see that the printed results seem to be somewhat different from what we want. Why?

It turns out that this is because there are escape characters in the C language. Let's take a look at what escape characters are available in C language:

escape character Definition
\? Used when writing multiple question marks in a row to prevent them from being parsed into three-letter words
\' Used to represent the character constant '
\“ Used to represent double quotes inside a string
\\ Used to represent a backslash, preventing it from being interpreted as an escape sequence character.
\a Warning character, buzzer
\b backspace character
\f paper feed character
\n newline
\r Enter
\t horizontal tab
\v vertical tab
\ddd

ddd represents 1~3 octal digits. For example, \130 is escaped as X via ASCLL

\xdd dd represents 2 hexadecimal digits. For example: \x30 0

6. Comments

Reason for comment:

1. If there are unnecessary codes in the code, you can delete them directly or comment them out.

2. Some codes in the code are difficult to understand. You can add comments.

 

C language style comments /*xxxxxxxx*/

Defect: cannot nest comments

C++ style comments //xxxxxxxx

You can comment one line or multiple lines

Summarize

The above is about some basic knowledge of C language. I hope it can help you. If there is anything poorly written, I hope you can correct me.

Guess you like

Origin blog.csdn.net/m0_74459723/article/details/127462951