1. Basic concepts of C language

Prepare environment

The inheritance development environment I use is Visual Studio 2022 (VS 2022 for short)
download link

1. Source files and header files

C language
source file suffix is ​​.c (Note: c++ source file suffix is ​​.cpp)
header file suffix is ​​.h

2. Compile and link

The C language source code itself is just a simple text file, and the C language code is placed in a file with a .c suffix.

must passcompileandLinkThen it becomes an executable file before it can be run.

The text file itself cannot be executed and must be passedcompiler translationandlinker link, generate a binary executable file, and then the executable file can be executed.

Insert image description here

3. The first C language program

//头文件 没有头文件无法解析对应的库函数(例:printf()函数)
//std-标准	i-input输入	 o-output输出
#include <stdio.h>

//c语言代码中一定要有main函数,main函数也叫主函数
//main函数是程序的入口,都从main函数的第一行代码开始
int main() {
    
    
	//main函数里面写代码
	//每段代码后面都要加上 ; 
	printf("hello c语言!");
	
    //return 0结尾
	return 0;
}

//运行快捷键 ctrl + f5 或者 fn + ctrl + f5

4. About the main() function

1. The C language code starts executing from the main function.
2. The main function is also called the main function. The main function is the entry point of the program.
3. There can be multiple .c files in a project.But there can only be one main function(Because there can only be one entrance to the program)

5. About the printf() library function

Function: Output and print strings, and can also output and print other types of data (formatted output)
Prerequisite: The header file stdio.h must be introduced

#include <stdio.h>
int main() {
    
    

	//printf是一个库函数,专门用来打印数据
	printf("hello c!");//打印字符串
	
	//利用格式字符串输出(根据占位符决定数据类型)
    printf("%d\n",100);//打印整型
	printf("%f\n",100.01);//打印浮点型(默认保留至小数点后6位)
	printf("%s\n","你好!");//打印字符串
	printf("%c\n",'c');// 打印字符
	return 0;
}

Output result:
Insert image description here

The difference between characters and strings and precautions
1. Use single quotes ' ' for
characters 2. Use double quotes " " for strings
3. Chinese characters occupy two characters, so when a single Chinese character is output in the form of a character, it will be garbled.

6. Keyword introduction

Keywords, as the name suggests, are very key words. In C language, they mostly form part of the grammar, such as definitions of various data types (int, long, float, char, etc.) and some keywords with important special meanings. (if, while, do, for, etc.).

Note: When creating a logo, it cannot have the same name as a keyword.

The 32 keywords of C language are as follows:
Insert image description here

7.Character and ASCII encoding

Characters in C language are enclosed in single quotes

The meaning of ASCII encoding:
All data in the computer is stored in binary form. If each of us compiles a binary sequence for each of these characters, this is called encoding. For the purpose of It was convenient for everyone to communicate with each other without causing confusion. Later, the American National Standards Institute (ANSI) introduced a standard ASCII encoding, and the characters in the C language followed the ASCII encoding method.

Some common ASCII codes
. The ASCII code value of
character AZ ranges from 65 to 90. The ASCII code value of character az ranges from 97 to 122.
The difference between the ASCII code values ​​of the corresponding uppercase and lowercase characters (a and A) is 32 (characters lowercase letters + 32=Character uppercase letters)
The ASCII code values ​​of numeric characters 0-9 range from 48~57.
Among these characters, the ASCII code values ​​range from 0~31. These 32 characters are non-printable characters.
0-31 these 32 characters correspond to some Operation function cannot be displayed.

For specific ASCII tables, please refer to https://www.runoob.com/w3cnote/ascii.html

8. String and /0

String
Definition: A string of characters enclosed in double quotes is called a string.
Formatting placeholder: %s

The following prints the string, which on the surface appears to be only 6 characters, but we start monitoring it

int main() {
    
    
	printf("abcdef");
	return 0;
}

From a monitoring perspective, this string actually has a total of 7 characters, and there is an extra \0 escape character at the end.
In C language, an escape character \0 is hidden at the end of the string. \0 is the end mark of the string.
Insert image description here

The relationship between strings and \0

int main() {
    
    
	//arr1 可以将字符串放到字符的数组中
	char arr1[] = "abc";//abc

	//arr2 将3个字符放入字符数组中
	char arr2[] = {
    
    'a','b','c'};

	//arr3 用转义 \0 将字符串隔开
	char arr3[] = "abc\0ddd";
	
	printf("%s\n", arr1);
	printf("%s\n", arr2);
	printf("%s\n", arr3);
	return 0;
}

Output result:
Insert image description here

Analysis:
arr1 outputs abc, normal

There is still a bunch of content after arr2 outputs abc, because after the array arr2 prints abc, because it is not a string, there is no \0 character
mark stop printing, so it keeps printing, and there is some so-called "junk dirty content" in the memory. "It's all printed.

When arr3 is output to abc, it touches the \0 mark, so printing will stop.

Modify the arr2 array and add a character \0 after the last element of the array.

int main() {
    
    
	//如果在arr2数组在结尾添加一个 \0 转义字符
	char arr2[] = {
    
    'a','b','c','\0'};//此时输出完abc即刻停止
	printf("%s\n", arr2);
	return 0;
}

9. Escape characters

As the name suggests, escaping means changing the meaning. The obvious characteristic of escape characters is that they are preceded by a backslash (for example, \n newline)

Take \n as an example.
Before adding \, n is just an ordinary character.
After adding \, \n represents a line break operation.

int main()
{
    
    
	printf("a\nb");
	return 0;
}

Insert image description here

And the \0 mentioned in the above string is also an escape character. After adding \ to character 0, it becomes \0. \0 is another brand-new meaning, that is, the end of string identifier.

The following are some commonly used escape characters

escape character significance
\\ Represents the symbol backslash \
\" "double quotes inside characters"
\‘ Indicates a single quote inside the character '
\n newline character
\t Tab character, the cursor moves to the next horizontal tab stop, usually the next multiple of 8
\b Backspace key, the cursor goes back one character but does not delete the character
\v Vertical separator, the cursor moves to the next vertical tab stop, usually the same column as the next line

10. About strlen() function

Function: Calculate the length of the string

int main() {
    
    
	//计算字符串的长度,直到空结束字符,但不包括空结束字符。
	int len = strlen("c:\test\123.c");//这里调式执行后结果为9,但是监视结果为10(监视把\0转义字符算上了)
	printf("%d\n", len);
	return 0;
}

Pay attention to the escape characters,An escape character counts as one character,c:\test\123.c(The yellow marked part counts as one character, that is, \t counts as one character\123 counts as one character

\ddd: ddd represents an octal number

Debugging results:
Insert image description here

Monitoring results:
Insert image description hereMonitoring results found that there are 10, because \0 is included,strlen() does not include the \0 ending identifier character

Guess you like

Origin blog.csdn.net/qq_45657848/article/details/131823517