Introduction to C language (1)

Welcome to the preface
  , friends, this article is just to let us have a general understanding of the C language, and each knowledge point will not be very deep. After that, I will continue to update the blog to broaden these knowledge and carry out A more detailed description.

1. What is C language

C language is a process-oriented, abstract computer programming language, which is widely used in low-level development.
C language has a complete theoretical system with a long history of development, and plays a pivotal role in programming languages.
learn more

2. The first C language program

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

Small but complete. This code is the first code we encountered when we came into contact with C language. Although it is very short, it is of great help to us in understanding C language.

To be careful of:

  • To use the printf() function, you must add #include<stdio.h> to the header file
  • There must be one main function in a C program, and there can only be one main function
  • The main function is the entry point of the program. The first thing the program reads is the main function, not the first line
  • printf() is a format output function
  • \n is an escape character, which means a newline
  • return is the return value of the function, depending on the function type, the returned value is also different

3. Data type

Here are some basic types we will use later:

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

For different objects, we use different data types to represent more representatively.
For example, name, age, height, we can use different data types to express, which is more accurate and easy to classify.

char ch[20]='zhangsan';			//姓名
int age=18;						//年龄
float h=168.3f;					//身高

Will the memory space occupied by these data types be the same?

You can implement the following code on your own compiler to see the result of running:

#include<stdio.h>
int main()
{
    
    
	printf("%d\n",sizeof(char));
	printf("%d\n", sizeof(int));
	printf("%d\n", sizeof(long));
	printf("%d\n", sizeof(long long));
	printf("%d\n", sizeof(float));
	printf("%d\n", sizeof(double));
	return 0;
}

The sizeof() function is used to calculate the size of the space occupied by data and data types, in bytes

4. Variables & Constants

In life, there are quantities that do not change (blood type, ID number), and quantities that change (age, height).
In C language, the quantity that does not change is represented by "constant"; "Variable" means

4.1 Method of defining variables

char ch='j';
int age=18;
double h=1.68lf;

The essence of variable creation: it is to open up a space in memory to store data

4.2 Naming of variables

There are rules for naming variables:

  • Can only consist of numbers, letters, and underscores (_).
  • Cannot start with a number.
  • Cannot exceed 63 characters in length.
  • Letters are case sensitive in variable names.
  • Variable names cannot use keywords.

4.3 Classification of variables

  • local variable
  • global variable
#include<stdio.h>
int a = 1;					 //全局变量	
int main()
{
    
    
	int b = 2;				 //局部变量
	printf("%d %d\n", a, b); //输出结果:1 2
	return 0;
}

Here, the variable a we defined outside { } is a global variable, and the variable b inside the main() function is a local variable.

So what will be the output when b has the same name as a?

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

Running result:
Please add a picture description
when a local variable has the same name as a global variable, the local variable is used first

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 program 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.
The destruction here is to return the space to the operating system

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

Let's take a closer look at the scope and life cycle of variables

Please add a picture descriptionIn this code, the variable a we defined is in the second {}, that is, its scope, and it will not work outside of this {}.

If we want to output a, we need to define it before {}
.
code show as below:

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

Running result:
Please add a picture description
Note:
If the source file needs to refer to variables in other files, we can use extern declaration in the file

Please add a picture descriptionVariables that can be referenced by other modules with the extern modifier are usually global variables.

4.5 Constants

The constants in C language are divided into the following categories:

  • literal constant
  • const modified constant variable
  • Identifier constants defined by #define
  • enumeration constant
#include<stdio.h>
enum color
	{
    
    
		RED,GREEN,BLUE//枚举常量
	};
int main()
{
    
    
	//字面常量
	3.14'a';//字符
	"abc";//字符串
	
	//const修饰的常变量
	const int pi=3.14f;//这里的pi是const修饰的常变量,具有常属性,但本质还是变量
	pi=2.6;//pi是不能直接修改的
	//变量一旦定义就不能修改

	//#define
	#define SIZE 10
	printf("size = %d\n",SIZE);//输出:10
	
	//枚举常量
	printf("%d\n",RED);//输出:0
	printf("%d\n",GREEN);//输出:1
	printf("%d\n",BLUE);//输出:2
	//枚举常量的默认是从0开始,依次向下递增1
	
	return 0;
}

5. Strings & escape characters

5.1 Strings

"hello world"

This string of characters enclosed in double quotes is called a string literal, or string for short.

Note: The end of the string is a \0 escape character. \0 is the end mark when calculating the length of the string, and it is not counted as the content of the string.

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char a1[] = "hello";//这里隐藏了\0
	char a2[] = {
    
     'h', 'e', 'l', 'l', 'o' };
	char a3[] = {
    
     'h', 'e', 'l', 'l', 'o', '\0' };
	printf("%s\n", a1);
	printf("%s\n", a2);
	printf("%s\n", a3);
	return 0;
}

operation result:
insert image description here

Please add a picture descriptionDebugging the code, you can intuitively see that '\0' is the end sign of the end of the string.

We can also further prove this by calculating the size of the string:
Please add a picture description

strlen() calculates the length of the string, that is, the number of characters, and ends with '\0', excluding the end character '\0'. sizeof(
) calculates the size of the memory space occupied by the data, and the unit is byte

5.2 Escape characters

  • As the name suggests, the escape character is to change the meaning.
    Let's first see what will be the result of the following code running?
#include<stdio.h>
int main()
{
    
    
	printf("c:\code\test.c\n");
	return 0;
}

Running result:
Please add a picture descriptionThere are some escape characters in it.

escape character paraphrase
? Used when writing multiple question marks in a row, preventing them from being parsed into three-letter words
used to represent character constants
" Used to represent - double quotes inside a string
\ Used to denote a backslash, preventing it from being interpreted as an escape sequence
\a warning character, beep
\b backspace
\f Form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\ddd ddd represents 1-3 octal numbers. Such as: \130 means character X
\xddd dd means 2 hexadecimal digits. Such as: \x30 means character 0

After understanding the escape character, we can look at the string length in the code below.

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	printf("%d\n",strlen("c:\test\178\x65\test"));//13
	return 0;
}

6. Notes

  • Unnecessary lines of code can be deleted directly or commented out
  • There are some codes in the code that are not easy to understand, you can add comment text
#include<stdio.h>
int Fun(int x, int y)//定义乘法函数
{
    
    
	return x*y;
}
/*C语言注释风格
int Sum(int x, int y)
{
	return x + y;
}
*/
int main()
{
    
    
	//C++注释风格
	//int a, b;
	//printf("%d\n", Sum(10, 20));//调用Sum函数,完成加法
	printf("%d\n", Fun(10, 20));//调用Fun函数,完成乘法
	return 0;
}

Two styles of comments:

  • C-style comments / xxxxxx /
    defect: cannot nest comments
  • C++ style comments //xxxxxx
    can comment one line or multiple lines

conclusion

The above is the sharing of my article this time, which summarizes several basic concepts of C language, and hopes to be helpful to everyone. If there is something wrong, I hope everyone can comment and correct me, and I will definitely revise it in time.
  The road is long and long!
  See you in the next article.
Please add a picture description

Guess you like

Origin blog.csdn.net/iLoyo_/article/details/130161662