C language string, escape character, operator

Notes on string, escape character and operator

1. String

End flag of string: \ 0

Find the length of the string: use the strlen function

Add a '\ 0' character to the arr2 array to print abc correctly

char arr1[] = "abc";			//Memory storage form: abc ABC \ 0
char arr2[] = {'a','b','c'};	//Memory storage form: abc?????????

//Print string
printf("%s\n",arr1);		//abc
printf("%s\n",arr2);		//abc ironing b

//Find string length
printf("%d\n",strlen(arr1));		//3
printf("%d\n",strlen(arr2));		//Random value

Note: the end flag of the string is an escape character of \ 0. When calculating the length of a string, \ 0 is the end flag and is not counted as the content of the string.

2. Escape character

\ddd ddd represents 1 ~ 3 octal digits. For example: \ 130 X
\xdd dd represents two hexadecimal digits. For example: \ 30 0

printf("%c\n",'\130');	//Octal 130 is decimal 88. If printed in the form of% c, it is the character X
printf("%c\n",'\101');	//Octal 101 is decimal 65, and the character A is printed in the form of% c
printf("%c\n",'\x30');	//Hexadecimal 30 is decimal 48. If printed in the form of% c, it is the character 0

'\130' -> 1 * 82+3 * 81 = 88

'\101' -> 1 * 82+1 * 80 = 65

'\x30' -> 3 * 161 = 48

printf("%d \n", strlen("c: \test\328\test.c"));		//14

The length of this string is 14, because \ t is a character and \ 32 is a character. Because there is no 8 in the octal number, character 8 cannot be counted into the escape character

There are two styles of annotation:

Comments in C language style / * xxxxxx * / defect: comments cannot be nested

C + + style annotation / / xxxxxxxx can annotate one line or multiple lines

3. Operator

sizeof is an operator, not a function

Function: to calculate the size of the type or variable

int main()
{
	int a = 10;
	printf("%d \n", sizeof(int));	//You can directly calculate the type size, and the result is 4
	printf("%d \n", sizeof(a));		//You can also find the size of the variable, and the result is 4
	return 0;
}

How to calculate the array size:

int main()
{
	int arr[10] = {0};
	printf("%d\n",sizeof(arr));		//The total size of the array is calculated in bytes, and the result is 40
	printf("%d\n",sizeof(arr[0]));	//The size of the first element of the array, and the result is 4
	int sz = sizeof(arr)/sizeof(arr[0]);	//The number of elements in the array is the number of bytes divided by the size of an element. The result is 10
	printf("%d\n",sz);
    return 0;
}

Bitwise inversion~

int main (){
{
	int a = 0;
	printf ( "%d \n", ~a) ;		//The result is - 1
}

Explanation:

Because a is 0, its binary in 32-bit system is:

00000 0000 0000 0000 0000 0000 0000 0000
~a:1111 1111 1111 1111 1111 1111 1111 1111

Because integers are stored in memory as complements

Therefore, the storage of a in memory after inversion is all 1, and the output of% d should be converted to the original code. From complement - > inverse - > original code, the result is - 1. Therefore, the storage and output of negative numbers in memory will be converted through binary calculation

1111 1111 1111 1111 1111 1111 1111 1111 1111 (complement)

​ ↓

1111 1111 1111 1111 1111 1111 1111 1110 (inverse code)

​ ↓

1000 0000 0001 (original code)

So the result output is: - 1

Positive integer: the original code, inverse code and complement are the same

Negative number: it is saved in memory in the form of complement. Complement is equal to inverse code plus one

comma operator

Comma expressions are evaluated from left to right

The result of the entire expression is the result of the last expression

int main()
{
	int a = 0;int b = 3;int c = 5;
	int d = (a = b + 2,c = a - 4, b = c + 2);	// a=3+2=5, 		 c=5-4=1, 	 b=1+2=3 		 Final d = b = 3
	printf ("%d \n", d);			//The result is 3
}

Example code:

a = get_val();
count_val(a);
while (a > 0)
{
    //Business processing
    a = get_val();
    count_val(a);
}

If comma expressions are used, redundancies can be reduced after Rewriting:

while (a = get_va1(),count_val(a), a>0)
{
		//Business processing
}

Subscript reference operator []

The [] used to access array subscripts is called the subscript reference operator

printf ("%d \n", arr[4] );	//[] has 2 operands: arr, 4

Parentheses (): function call operators

Such as fun(a,b)

There are three operands, fun, a, b. some functions have no parameters, so one operand is the function name

Structure member access operator. - >

.

use:

struct B
{
    char name[10];
    char id[10];
    int price;
};

int main()
{
    Struct B b;
    printf("title:%s \n", b.name);
    printf("Book number:%s \n", b.id);
    printf("price: %d\n", b.price);
    return 0;
}

->For pointer form

struct B
{
    char name[10];
    char id[10];
    int price;
};

int main()
{
    struct Book b = { "c language","C20210509",55};
    struct Book * pb = &b;
    printf("title:%s \n",(*pb).name);
    printf("Book number:%s \n",(*pb) .id) ;
    printf("price:%d \n", (*pb) .price);
	//It is equivalent to the following writing method. The following writing method is generally used, structure pointer - > member name
    printf("title:%s \n",pb->name);
    printf("Book number:%s \n",pb->id) ;
    printf("price:%d \n", pb->price);
    return 0;
}

Tags: C

Posted by tpc on Sun, 24 Apr 2022 16:19:39 +0300