[C language - first understanding of C language (2)]


foreword

Continue the learning content of the previous article. This article is still a quick preview of the basic content of the C language.

4. String + escape character + comment

4.1 Strings

Single quotation marks must be used to define a single character, and only one character can be contained in the single quotation mark, and %c can be used for the output character.

char ch1 = 'abc';//错误的
char ch2 = 'c';//正确
//输出字符用%c
printf("%c\n", ch1);
printf("%c\n", ch2);

Press ctrl+F5 to run the program, and the output is as follows:
insert image description here
We found that the ch1 output is not the imagined abc.
ch2 is correct. So single quotes can only define one character.
Multiple characters together are strings. For example, to define the string abc, it needs to be enclosed in double quotation marks. arr[] represents an array, and no numbers can be written in parentheses. The output string is represented by %s. It can also be written in the single-quote form below.

char arr1[] = "bit";

insert image description here

Note that when learning strings:
the end of a string is a \0 escape character. When calculating the length of the string, \0 is the end mark and is not counted as the content of the string. String input can also be written in single-quote form as follows, such as output arr3[ ] and arr4[ ]:

char arr3[] = {
    
     'b','i','t' };
char arr4[] = {
    
     'b','i','t','\0'};

Comparing the output results, it is found that the output results of arr1[ ] and arr4[ ] are the same, but arr3[ ] is garbled.
insert image description here
To analyze the reason, it is necessary to observe the changes of the string variables arr1[ ], arr3[ ] and arr4[ ] in the program.
In the program interface, press F10 on the keyboard , the program enters debugging, the interface will display debugging, click debugging - window - monitoring - monitoring 1 , see the figure below

insert image description here
Enter arr1, arr3, arr4 in the monitoring window below, then press Enter or click on the blank space. The yellow arrow represents where the program is running and debugged.
insert image description here

Then press F11, the program starts single-step debugging , and the string variables are random before they are initialized.
insert image description here
Continue to press F11 , after initialization, you will see that the values ​​of the three variables have been assigned.
insert image description here
Click the small triangle next to it, the specific content of the variable will be displayed, and you will be surprised to find that it seems that all three are input string bits, but the results are very different. Both arr1 and arr4 end with the character '\0'. Not in arr3.
insert image description here
arr1 uses double quotes to input the string, and the end contains the end character '\0' by default; the input of arr3 is not available; arr4 is added actively. Simultaneously output three strings and their lengths:
since arr1 and arr4 have the end character '\0' at the end , the string ends, the output value is bit, and the length is 3 characters.
arr3 does not have the end character '\0' at the end , so in the memory, there will be some random values ​​at the end of which I don't know what it is, so the output is garbled, and the length of 15 is wrong, and I don't know what is behind the tail. messy stuff.
insert image description here
insert image description here
Therefore, it is recommended to use double quotes when entering strings.

4.1 Escape characters

To print a directory on the screen: c:\code\test.c

int main()
{
    
    
	printf("c:\code\test.c\n");
	return 0;
}

insert image description here
It should be changed to the following form

printf("c:\\code\test.c\\n");
int main()
{
    
    
	printf("%c\n", '\'');//在屏幕上打印一个单引号'
	printf("%s\n", "\"");//在屏幕上打印一个字符串,字符串的内容是一个双引号“
	return 0;
}

Run the following program to output the length of the string

#include <stdio.h>
int main()
{
    
    
printf("%d\n", strlen("abcdef"));
// \62被解析成一个转义字符
printf("%d\n", strlen("c:\test\628\test.c"));
return 0;
}

insert image description here
Here, according to the method of monitoring variables mentioned above, you can clearly observe the change of the string variable arr, which contains 14 characters, the length is 14, and the end character '\0' is included at the end, which is hidden and does not Counted as character content, it just means the end of the string.
insert image description here

5. Notes

  • Unnecessary code in the code can be deleted directly or commented out
  • Some code in the code is more difficult to understand, you can add a comment text
int add(int x, int y)
{
    
    
	return x - y;
}
/*C语言风格的注释
int add(int x, int y)
{
	return x - y;
}*/

int main()
{
    
    
	//C++注释风格
	//int a = 10;
	//调用add函数,完成加法
	printf("%d\n", add(1,2));

	return 0;
}

6. Select statement

If you study hard, get a good offer when recruiting, and you will reach the pinnacle of your life.
If you don't study, graduation equals unemployment, and you go home and sell sweet potatoes.
This is the choice!

int main()
{
    
    
	int coding = 0;
	printf("你会去敲代码吗?\n");
	printf("选择1 or 0)");
	scanf("%d", &coding);
	if(coding == 1)
	{
    
    
		prinf("坚持,你会有好offer\n");
	}
	else
	{
    
    
		printf("放弃,回家卖红薯\n");
	}
	return 0;
}

7, loop statement

The statement executed repeatedly in the C language is a loop, which has three forms:

  • while statement
  • for statement
  • do … while statement

The following is an example of a while loop, and the other two loops will be updated later

int main()
{
    
    
	printf("加入大厂\n");
	int line = 0;
	while(line<=20000)
	{
    
    
		line++;
		printf("我要继续努力敲代码\n");
	}
	if(line>20000)
		printf("好offer\n");
	return 0;
}

8. Function

Below is a simple sum of two input integers and prints it out. The writing method before learning the function:

int main()
{
    
    
	int num1 = 0;
	int num2 = 0;
	int sum = 0;
	printf("输入两个操作数:>");
	scanf("%d %d", &num1, &num2);//输入函数
	sum = num1 + num2;
	printf("sum = %d\n", sum);
	return 0;
}

After the addition statement is written as a function, the feature of the function is to simplify the code and reuse the code:

int Add(int x, int y)//加法的函数定义
{
    
    
	int z = x+y;
	return z;
}
int main()
{
    
    
	int num1 = 0;
	int num2 = 0;
	int sum = 0;
	printf("输入两个操作数:>");
	scanf("%d %d", &num1, &num2);
	sum = Add(num1, num2);//调用函数
	printf("sum = %d\n", sum);
	return 0;
}

9. Array

The C language stipulates that each element of the array has a subscript, and the subscript starts from 0. Arrays can be accessed by subscripting.
The following defines an array arr1[10] containing 10 elements, all initialized to 0. The range of subscripts is 0-9.

int arr1[10] = {
    
    0};//定义了一个包含10个元数的数组,初始化都为0
///如果数组10个元素,下标的范围是0-9

insert image description here

int main()
{
    
    
	
	int i = 0;
	
	int arr[10] = {
    
    1,2,3,4,5,6,7,8,9,10};
	for(i=0; i<10; i++)//for循环打印数组中的10个元数
	{
    
    
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}

Summarize

The basic content of C language is not over yet. This article mainly wants to introduce the simple debugging function of VS2017 and monitor the changes of variables in the window, which is very helpful for the subsequent learning of complex programs.

F10 program enters the debugging interface
F11 single-step debugging
shift+f11 to end debugging

Guess you like

Origin blog.csdn.net/taibudong1991/article/details/123708243
Recommended