C Language Mistake Tidy 5-2021-10.4

char *a="abcdef"; char d[]="abcdef"; char e[]={'a','b','c','d','e','f'}; char str1[8] = {'a', 's', 'd'}; char str2[20] =...


char *a="abcdef"; char d[]="abcdef"; char e[]={'a','b','c','d','e','f'}; char str1[8] = {'a', 's', 'd'}; char str2[20] = "0123456789"; int str3[20] = "0123456789"; char str4[] = "as\0df"; string str5={'a','b','c','\0','X'}; sizeof(a)=4; strlen(a)=6; sizeof(d)=7; strlen(d)=6; sizeof(e)=6; strlen(e)=14; sizeof(str1) = 8; strlen(str1) = 3; sizeof(str2) = 20; strlen(str2) = 10; sizeof(str3) = 80; strlen(str3) = error; sizeof(str4) = 6; strlen(str4) = 2; sizeof(str5) = 5; strlen(str5) = 3;

When assigned as a string,'abcdef', automatically ends with a'/0'.
strlen(a) ends when it encounters/0, requiring the length of the string, 6, sizeof(a) the size of the type space. As mentioned earlier, the size of the pointer point is 4 bytes, and the length of the system address bus is 32 bits.
strlen(d) is the same, string assignment, automatic addition/0, the length of the string is of course 6, sizeof(d) is the size of the space occupied by the array, that is, the number of bytes occupied by the array, should be 7.
sizeof(e), the array e is assigned with a single element and does not have a/0 terminator, so the size of the space occupied is 6 bytes, strlen(e), and the length of the string to find the end of/0. Since/0 cannot be found, the value returned is an indeterminate value.

str1 is also an array of characters, but the size is determined to be 8, so sizeof gets 8; strlen statistics\0 before the number of all characters, that is, 3;
sizeof(test) is 20 in str2, test means the pre-allocated size in memory, 20x1, strlen(test) is 10, and the loop ends when the'\0'character is encountered;
In str3, sizeof(test) is 80 and int is 4 bytes in memory, so the allocated memory is 20x4, and strlen(test) errors because stelen accepts only char parameters and must end with'\0', which is int *;
str4 is a constant character array, sizeof gets the total number of characters, which is 6; strlen calculates to the end of\0, so returns 2;
To summarize, sizeof calculates the size of the variable, while strlen calculates the length of the string, the former is not affected by the character\0, and the latter is based on the length.

The inverse code of a positive number is itself
The inverse code of negative numbers is based on the original code, with the symbol bits unchanged and the remaining bits reversed.
[+1] = [00000001] Original= [00000001] Reverse
[-1] = [10000001] Original = [11111110] Reverse

The complement of a positive number is itself
The complement of negative numbers is based on the original code, the symbol bits are unchanged, the rest of you are reversed, and finally + 1.(i.e. +1 on the basis of the inverse code)
[+1] = [00000001] Original= [00000001] Counter= [00000001] Complement
[-1] = [10000001] original = [11111110] inverse = [11111111] complement


int array[10] = ; int i; for (i = 0; i < 10; i++) { if (array[i] == 7) { printf("%d\n", i); break; } }

In the loop body, if statements are used to find subscripts with a value of 7 in the array and display their values, then break statements are used to jump out of the loop, that is, when the program executes a break statement, the entire loop will end, and what should have continued the loop will not be executed. The program above executes six cycles, with a value of 5, and the rest will not execute and end directly.

int array[10] = ; int i; for (i = 0; i < 10; i++) { if (array[i] < 5) { continue; } printf("%d\n", i); }

However, in if we determine that a continue statement is executed if the value of a variable in the array is less than 5, so we end this loop and go to the next one, so the printf statement is executed only if it is greater than or equal to 5, so the result is:
0 3 5 6 7

break: Jump out of the loop statement and execute the following code
continue: End this cycle and proceed to the next one.

#include <stdio.h> #include <string.h> int main() { char str[32] = "helloworld"; char ptr[32] = "12345"; strcpy(str, ptr); //Copy the data of the ptr array into the str array including'\0' printf("%s\n", str); strncpy(str, ptr, 3); printf("%s\n", str); strncpy(str + 1, ptr + 3, 2); printf("%s\n", str); strcat(str, ptr); //Connect data from ptr array to str printf("%s\n", str); strncat(str, ptr + 1, 3); printf("%s\n", str); if (strcmp(str, ptr) > 0) //string comparison { printf("%s > %s\n", str, ptr); } else { printf("%s < %s\n", str, ptr); } return 0; }

4 October 2021, 12:38 | Views: 3345

Add new comment

For adding a comment, please log in
or create account

0 comments