Character array and string -- C language description

Posted by gargoylemusic on Sun, 20 Feb 2022 15:27:40 +0100

Character array and string -- C language description

1 character array

The character array is defined with the keyword char. The memory structure is sequential storage, as shown in Figure 1.1 below

char ch1[] = {'w', 'e'};

Figure 1.1 memory structure of character array
#include <stdio.h>

/*
Purpose:
1. Test character array
*/
int main() {
	char ch1[2] = {'w', 'e'};
	int i = 0;

	for (i = 0; i < sizeof(ch1); ++i) {
		printf("ch1[%d] = %c\n", i, ch1[i]);
	}

	getchar();
	return 0;
}

result:

ch1[0] = w
ch1[1] = e

1.2 ASCII code table

Reference link:

https://blog.csdn.net/xujidong1576324301/article/details/88342192?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164527731916780264046399%2522%252C%2522scm%2522%253A%252220140713.130102334...%2522%257D&request_id=164527731916780264046399&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-1-88342192.first_rank_v2_pc_rank_v29&utm_term=c%E8%AF%AD%E8%A8%80ascii%E7%A0%81%E8%A1%A8&spm=1018.2226.3001.4187

There are 128 (0127) ASCII codes in total, of which the ASCII control characters are 031 and 127 and the ASCII displayable character is 32126. Among the displayable characters: the ASCII value of the space character is 20, the numeric character (09) is 4857, the uppercase alphabetic character (AZ) is 6590 (26), and the lowercase alphabetic character (AZ) is 97 ~ 122 (26), and the difference between the corresponding uppercase and lowercase letters is 32.

example:

Test the value of the ASCII code table.

#include <stdio.h>
#include <stdlib.h>

/*
Requirement: 
1. test ASCII
*/
int main() {
	char ch1[] = { 'w', 0, 'e', 48, 20, 65, 97};
	int i = 0;

	for (i = 0; i < sizeof(ch1); ++i) {
		printf("ch1[%i] = %c\n", i, ch1[i]);
	}

	getchar();
	return 0;
}

result:

ch1[0] = w
ch1[1] = -- null character
ch1[2] = e
ch1[3] = 0
ch1[4] = -- space
ch1[5] = A
ch1[6] = a

1.3 escape character

As shown in Figure 1.2 below, it is mainly aimed at the implementation of special functions and special characters, such as line feed (\ n), printing or storing special characters, such as (', ", \). The escape character' \ 0 'represents the ASCII value 0 (empty character)

Figure 1.2 escape character table

example:

Test the escape character. Generally, print this escape character and underline it once more\

#include <stdio.h>
#include <stdlib.h>

/*
Requirement: 
1.test CharArr
*/
int main() {
	char ch1[] = {'w', 'e', '\\', '\'', '\"', '\;'};
	int i = 0;

	for (i = 0; i < sizeof(ch1); ++i) {
		printf("ch1[%i] = %c\n", i, ch1[i]);
	}

	getchar();
	return 0;
}

result:

ch1[0] = w
ch1[1] = e
ch1[2] =
ch1[3] = '
ch1[4] = "
ch1[5] = ;

1.4 common problems - character array initialization is not full or cross-border access

**(1) * * the test character array is not filled with definitions and cross-border access

① Number of initialized characters < number of characters defined by character array

Other members are filled with empty characters. When accessing ch2[3], the result is a null character. When accessing ch2[4] and ch2[5], the returned value is garbled.

char ch2[3] = {'w', 'e'};

② Number of initialized characters > number of character array definitions

	//char ch3[1] = {'w', 'e'};// Direct error reporting

example:

The test character array is not full, overflow definition and access

#include <stdio.h>
#include <stdlib.h>

/*
Requirement: 
1.The test is not full and overflows
*/
int main() {
	char ch1[2] = {'w', 'e'};
	char ch2[3] = {'w', 'e'};
	//char ch3[1] = {'w', 'e'};// Define direct error reporting for overflow
	int i = 0;

	//The test is normal
	printf("Normal test:\n");
	for (i = 0; i < sizeof(ch1); ++i) {
		printf("ch1[%d] = %c\n", i, ch1[i]);
	}

	//The test is not full
	printf("Test not full:\n");
	for (i = 0; i < sizeof(ch2); ++i) {
		printf("ch2[%d] = %c\n", i, ch2[i]);
	}

	//Test cross-border access
	printf("Test cross-border access:\n");
	printf("ch1[3] = %c\n", ch1[3]);
	printf("ch1[4] = %c\n", ch1[4]);

	getchar();
	return 0;
}

result:

Normal test:
ch1[0] = w
ch1[1] = e
Test not full:
ch2[0] = w
ch2[1] = e
ch2[2] = -- empty character
Test cross-border access:
ch2[4] = ? —— Garbled code
ch2[5] = ? —— Garbled code

2 string

2.1 description of string

The string is an array of characters ending with '\ 0'\ 0 'is an escape character, which is escaped into ASCII code, and the value is 0, which also means null character. In c language, strings are defined in the form of character arrays. There are two ways to define: the first is to use {}, which needs to add '\ 0'; The second method is to use '", and the compiler will automatically fill' \ 0 'after it. The memory structure of string is shown in Figure 2.1. It is recommended to fully convert to memory structure to understand.

char c01[] = {'w', 'e', '\0'};
char c02[] = "we";

Figure 2.1 string memory structure

example:

Test string

#include <stdio.h>
#include <stdlib.h>

/*
Purpose:
character string
*/
int main() {
	//Character array mode
	char c01[3] = { 'm', 'e', '\0' };
	//character string
	char c02[3] = "me";
	int i = 0;

	//Print character array
	printf("sizeof(c01) = %d\n", sizeof(c01));
	for (i = 0; i < sizeof(c01); ++i) {
		printf("c01[%d] = %c\n", i, c01[i]);
	}
	printf("c01 = %s\n\n", c01);

	//Print string
	printf("sizeof(c02) = %d\n", sizeof(c02));
	for (i = 0; i < sizeof(c02); ++i) {
		printf("c02[%d] = %c\n", i, c02[i]);
	}
	printf("c02 = %s\n", c02);

	getchar();
	return 0;
}

Print results:

sizeof(c01) = 3
c01[0] = w
c01[1] = e
c01[2] = -- empty character
c01 = we

sizeof(c02) = 3
c02[0] = w
c02[1] = e
c02[2] = -- empty character
c02 = we

1.2 frequently asked questions

(1) Printing of test string

When printing, use the% s placeholder to print. Because the string ends with '\ 0', when printing the string, the system will look for '\ 0', and when '\ 0' is recognized, the printing will be ended.

example:

Test printing string as% s

#include <stdio.h>

/*
Requirement: 
1.Test printing string as% S
*/
int main() {
	//'\ 0' in the middle
	char ch1[4] = {'w', 'e', '\0', 'a'};
	char ch2[5] = "we\0a";//String defined with '' will be followed by '\ 0'
	int i = 0;
	
	//Test ch1
	printf("test ch1:\n");
	printf("ch1 = %s\n", ch1);//Stop when empty character '\ 0' is encountered
	for (i = 0; i < sizeof(ch1); ++i) {
		printf("ch[%d] = %c\n", i, ch1[i]);
	}

	//Test ch2
	printf("test ch2:\n");
	printf("ch2 = %s\n", ch2);//Stop when empty character '\ 0' is encountered
	for (i = 0; i < sizeof(ch2); ++i) {
		printf("ch2[%d] = %c\n", i, ch2[i]);
	}


	getchar();
	return 0;
}

result:

Test ch1:
ch1 = we
ch[0] = w
ch[1] = e
ch[2] =
ch[3] = a
Test ch2:
ch2 = we
ch2[0] = w
ch2[1] = e
ch2[2] =
ch2[3] = a
ch2[4] =

(2) The test character array is not full, that is, the number of initialized characters is less than the number defined by the character array

#include <stdio.h>

/*
Requirement: 
1.The test is not full
*/
int main() {
	char ch3[6] = "wea";//'\ 0' will be automatically added after 'a'
	int i = 0;

	//Test ch3
	printf("test ch3:\n");
	printf("ch3 = %s\n", ch3);//Stop when empty character '\ 0' is encountered
	for (i = 0; i < sizeof(ch3); ++i) {
		printf("ch3[%d] = %c\n", i, ch3[i]);
	}

	getchar();
	return 0;
}

result:

Test ch3:
ch3 = wea
ch3[0] = w
ch3[1] = e
ch3[2] = a
ch3[3] =
ch3[4] =
ch3[5] =

Topics: C Back-end