The foundation of C language brushing notes is not firmly shaken <1>

  Personal homepage: Welcome everyone --> Populus euphratica under the desert

 Guys, you are so beautiful

 If you find the article helpful

 You can support bloggers with one click

 Every ounce of your concern is the driving force for me to persevere

 

 ☄: The focus of this issue: Some C language brush questions are prone to mistakes

  I hope you all have a happy study and work every day. 

1. About global variables

2. About scanf

3. Pointer practice

 4. The difference between "A" and 'A'

5. About the storage of data

6. About two-dimensional arrays

7. Operator precedence


1. About global variables

Can't use a global variable before it's defined?

extern int a;

int main()
{
	printf("a = %d\n", a);
	return 0;
}

int a = 10;

Let's take a look. First of all, the definition of int a is after the main function, and it can be used by declaration.

Essence: First of all, global variables have been allocated memory space when the program is running, so they can be used before they are defined.

2. About scanf

Add a suitable input statement at the horizontal line, and input from the keyboard during the running of the program: How are you? <Enter>, the final output is:  How are you? How

int main()
{
	char *a = "How are you?", b[20];
    ——————————————————————————————
	printf("%s %s\n", a, b);
	return 0;
}

A:gets(b)       B:scanf("%s",b)

 The end of gets is a newline ( '\n' )

 The end of scanf is the space carriage return Tab key, which is a continuous character.

So you should choose B:scanf("%s",b)

3. Pointer practice

	int a[10] = { 1, 2, 3, 5, 5, 6, 7, 8, 9, 10 };
	int x = 0;
	int *pa = a;

With the above definition, which of the following assignments does not assign the element with the subscript 3 of the array to x?

A:x = pa[3]                            B:x= *(a+3)

C:  x =a[3]                                D:x = *pa+3

Under analysis:

A: Correct, although pa is a pointer, it can be accessed in the form of an array.

B: Correct, a is the address of the first element, move the size of 3 ints successively, just access the element with subscript 3, and assign the value in dereference

C: Correct, the value with subscript 3 is assigned to x in the form of an array.

D: Error, first of all, it is directly dereference a is the first element of the array, and then +3, this is not correct.

 4. The difference between "A" and 'A'

First the character 'A' is a single character, and "A" is two characters, A and \0, they are not the same.

5. About the storage of data

Look directly at the code:

	unsigned short A = 10;
	printf("~A = %u\n", ~A);
	char c = 128;
	printf("c = %d\n", c);

Let's analyze A:

Stored in the A space is 00000000 00001010 , and then bitwise inversion of A becomes 11111111 11110101

Then print according to %u, that is, according to unsigned integer printing, it is 11111111 11111111 11111111 11110101

The print result is 4294967285

Then analyze c:

c is 128, the storage space is 10000000, it is %d printing, so the shaping is upgraded to

11111111 11111111 11111111 10000000, which is complement,

Convert to one's complement 10000000 000000000 00000000 01111111

The original code is 10000000 000000000 00000000 10000000,

The last print is -128

6. About two-dimensional arrays

The following output results:

	char arr[2][4];
	strcpy(arr[0], "you");
	strcpy(arr[1], "me");
	arr[0][3] = '&';
	printf("%s \n", arr);

Let's analyze first:

The data after the first copy is,

 The data after the second copy is,

 At this time, if we print directly with %s, it will be printed to the end of '\0', but we have performed an assignment operation and replaced '\0' in arr[0][3] with ' $ ', so The result is " you&me ".

7. Operator precedence

Is *p++ the self-incrementing pointer p or what the self-incrementing pointer p points to?

Look at the code:

	int a = 10;
	int *p = NULL;
	int **pp = NULL;
	p = &a;
	pp = &p;
	printf("%d\n", *pp);
	printf("%d\n", *p++);
	printf("%d\n", *pp);

We can directly run it and conclude that it is the pointer variable p that is self-incrementing.

 
There are two schools of interpretation:

1. Interpreted in C and pointers, under the same priority, the associativity is from right to left.

2. On the priority diagram, postfix ++ has higher priority than dereference ( * ).

Let's discuss this in our forum!

Guess you like

Origin blog.csdn.net/m0_64770095/article/details/124437038