First introduction to C language (1) - a simple understanding of the whole picture of C

1.What is C language

C language is a general computer programming language that is widely used in low-level development . The design goal of the C language is to provide a simple
A programming language that compiles , processes low-level memory , generates a small amount of machine code , and can run without any runtime environment support.

 Drawing a picture can better understand what "low-level development" is.

2. The first C language program

Let's open VS2022 and start writing the first C language program.

First click on VS, then double-click "Create New Project", click "Next" after "Empty Project" in the next interface

 Next, VS will ask you to name your project, as shown in the figure. Then click "Create" in the lower right corner.

 After entering, the following page will appear on the right side of the page, right-click "Source File"

 

 Then select "Add" and "New Item" as shown in the figure

 Then give your project a name. The suffix ".cpp" refers to "C Plus Plus", which is C++. Because we are learning C language, we need to change the suffix to ".c"

 Then type the code as shown in the picture, and you can start writing the first program at the cursor. Remember to enter the symbols in English.

 printf is printing, letting the computer print out what you want.

2.2 Certain issues

1. If you open vs but cannot find the "source file" column, the problem can be solved as shown in the figure

 2. Why are all the codes I type in white? And my compiler doesn’t respond at all.

 In fact, when you name your project, remove the suffix ".c" and just rename it.

3.Data type

There are many data structures in C language, as shown in the figure.

char represents characters; short\int\long\long long all represent integers, but they represent different precisions; float\double represents decimals. The appearance of these types can well describe the data in life.

If we count the memory they occupy, we can borrow a function "sizeof" to calculate the lengths they represent.

The unit of calculation result is: bytes. Next, let’s take a look at the common units in computers:

bit - bit

byte - byte

KB

MB

GB

TB

PB

Except for 1byte=8bit, the other two adjacent units are all in 1024 format, such as 1KB=1024byte.

4. Variables and constants

4.1 Methods of defining variables

You should first specify the data type of the variable, then determine the variable name, and then initialize the variable (that is, assign an initial value).

Both float and double can represent the data type of weight. 

4.2 Naming variables

 1. Can only consist of letters, numbers and underscores (_)

2. Cannot start with a number

3. Length should not exceed 63 characters

4. Variable names are case sensitive

5. Keywords cannot be used in variable names

* Name variables as meaningful as possible

4.3 Classification of variables

*Global variables

*Local variables

 When I define a global variable outside the main function and modify the value of the global variable inside the main function, what will be the final compilation result?

 As you can see, the value of the global variable global has been modified!

Let’s talk about the conclusions: 1. There is actually nothing wrong with the definition of local variables and global variables above!

                                 2. When local variables and global variables have the same name, the local variable takes precedence.

4.4 Scope and life cycle of variables

4.5 Constants

Constants in C language are divided into the following types:

1. Literal constants

2.const modified constant variable

3.#define defined identifier constants

4. Enumeration constants

 

 You can see that pai was reported as an error. Why is this? You can try it yourself on VS. It will prompt "The expression must be a modifiable lvalue", which means that pai has been modified by const and you cannot change it anymore.

The pai in the above example is called a const- modified constant variable. The const -modified constant variable in C language only limits the variable pai at the grammatical level and cannot be directly changed, but pai is essentially a variable , so it is called a constant variable.

 There is one thing to note: when defining with define, #define MAX 100, there is no equal sign between the constant name and the value!

 

Guess you like

Origin blog.csdn.net/m0_75186846/article/details/131196203