Initial C language (1)

foreword

The initial C language is divided into 3 parts in total, the purpose is to help beginners to understand the basics of C language and have a general understanding of C language. Each knowledge point is a brief introduction or introduction part, and no detailed introduction is given here.

1. What is C language

C language is a general computer assembly language , which is widely used in formation development. The design purpose of the C language is to provide an assembly language that can be compiled in an easy way , handle low-level memory , generate a small amount of machine code , and can run without any operating environment support. Because the C language combines the control characteristics of computer science theory and practice , and is efficient and portable , powerful and flexible, it is popular for programmers and other advantages.
In the 1980s, in order to avoid errors caused by the use of C language grammar by various developers, the US National Bureau of Standards formulated a complete set of American National Standard grammar for C language, called ANSI C , as the original standard of C language. At present, on December 8, 2011, the C11 standard issued by the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) is the third standard of the C language and the latest standard of the C language. This standard better supports Chinese character function names and Chinese character identifiers realize Chinese character programming to a certain extent.

2. The first C language program

After introducing what is C language, let's take a look at the first C language program

#include <stdio.h>
int main()
{
    
    
	//printf("hello\n");
	return 0;
}

The above is the simplest C language program, except for the content of // this line is a basic framework of C language, but it should be noted that C language stipulates that the main function is the entry point of the program, and there is only one main function in a project . In addition, readers sometimes see the following three C language frameworks.

//这种语法读者可能在学校见的比较多,但由于这种写法是非常古老的,不推荐读者这样写。
void main()
{
    
    

}

//这种写法也有
int main(void) //void在这里表示main函数不接受任何参数
{
    
    
	return 0;
}

//这种写法对初学者不要求,以后在研究
int main(int argc, char* argv[])
{
    
    

	return 0;
}

3. Data type

Beginners may have seen the following data types, but readers may wonder why there are so many types? What is the size of each type?

char // character data type
short // short integer
int // integer
long // long integer
long long // longer integer
float//single-precision floating-point number
double//double-precision floating-point number

The purpose of multiple data types is to save memory space . The creation of each data type will apply for the corresponding memory space from the memory, but sometimes when we store a small number, imagine if we use a data type to apply for a large space in the memory, does it mean that we Will be wasting a lot of space? Therefore, the data types of different sizes created by the C language meet different actual needs and release excess space to achieve the purpose of saving memory space.
Due to limited space, the calculation process of each type of size code will not be given. Readers only need to remember that their sizes are (1, 2, 4, 4, 8, 4, 8 ) bytes , but it should be noted that C The language only stipulates that the length of long is not less than int, so if the reader's computer is in the x64 environment, int is 8 bytes .

4. Constants and variables

Some values ​​in life are constant (for example: gender, blood type, etc.), and some values ​​​​are variable (for example: age, weight, etc.). Constant values ​​are represented by constants in C language ; variable values ​​are represented by variables .
Next we introduce variables first.

4.1 Method of defining variables

char ch = 'a';
int age = 20;
int weight = 120;

The definitions of the three variables have been given above, is it very simple? We only need **data type + variable name (+ initial value)**.

4.2 Naming of variables

The C language has the following strict rules on the naming of variables

  • Can only consist of letters (both uppercase and lowercase), numbers, and underscores (_).
  • cannot start with a number
  • Length cannot exceed 63 characters
  • Variable names are case sensitive
  • Variable names cannot use keywords

4.3 Classification of variables

Variables in C language can be divided into local variables and global variables.
Initiators only need to understand that variables outside {} are global variables, and variables created inside {} are local variables.
(The actual situation is not the case. This is only for beginners and the first C language program. The specific situation will not be introduced in detail here.)
Next, readers can run the following code on their own compiler.

#include <stdio.h>
int global = 2023;  //全局变量
int main ()
{
    
    
	int global = 2022; //局部变量
	printf("%d\n",global);
	return 0;
}

In the above code, we will find that we have defined a local variable with the same name, what about the output?
insert image description here
We will find that the output result is 2022, why is this?
The C language stipulates that when a local variable has the same name as a variable variable , the local variable is used first . (However, readers are not recommended to define two variables with the same variable name when actually typing the code)

4.4 The scope and life cycle of variables

scope

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

1. The scope of a local variable is the local scope where the variable is located.
2. The scope of global variables is the entire project.

life cycle

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

1. The life cycle of local variables is: the life cycle begins when entering the scope, and ends when the life cycle exits.
2. The life cycle of global variables is: the life cycle of the entire program

4.5 Constants

Constants in C language can be divided into the following categories:

  • character constant
  • const modified constant variable
  • Identifier constants defined by #define
  • enumeration constant

1. Character constants

'a', '1', '!' etc. A character enclosed in single apostrophes entered on the keyboard is a character constant.

2. Constant variables modified by const

Let's look at the following code first

#include <stdio.h>
int main()
{
    
    
	const int a = 4;
	printf("%d\n", a);
	a = 98;
	printf("%d\n", a);
	return 0;
}

If we run it directly, we will find that the compiler reports an error, that is, the variable cannot be changed after being modified with const. insert image description here
Write it down and let's look at the following code

#include <stdio.h>
int main()
{
    
    
	const int a = 20;
	int arr[a];  //初学者只需知到arr[]中如果有数值必须是常量即可
	return 0;
}

After running this code, we will find that the compiler also reports an error.
insert image description here

The above two codes also reflect that after the variable a is modified by const , a here has a constant attribute and cannot be changed , but it is still a variable (ie a constant variable) in essence .

3. Identifier constants defined by #define

In the same way, let's first look at the following code and run it

#include <stdio.h>
#define MAX 1000
#define CH 'w'
int main()
{
    
    
	int a = MAX;
	printf("%d\n", a);
	int arr[MAX] = {
    
     0 };
	printf("%c\n", CH);
	return 0;
}

insert image description here
We found that the above code runs successfully and outputs the correlation information. Readers may have found that #define + variable name + number, etc. seem to just replace the following constant name with a variable name, and there is no change in essence . Congratulations, you got it right, that's actually what it means.

4. Enum constants

There are many values ​​​​in life that can be listed one by one (for example: gender, three primary colors, blood type, etc.)
Let's take the three primary colors (red, green, blue) as an example to see the relevant syntax

#include <stdio.h>
enum COLOR
{
    
    //列出枚举类型enum COLOR的可能取值
	RED,
	GREEN,
	BLUE
};
int main()
{
    
    
	printf("%d\n", RED);
	printf("%d\n", GREEN);
	printf("%d\n", BLUE);
	return 0;
}

insert image description here
Readers of the above code can see that the subscript (starting from 0) is printed on the screen output. It
should be noted that from the screen output, we can see that our enumeration type is a constant here and cannot be changed. Reassignment, but it can be used in the enum function Change its initial value in the definition. If you are interested, readers can knock on the compiler for themselves.
This article ends here, and readers are welcome to post their own explanations, thank you readers for reading.

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/130036870
Recommended