C Language—Daily Multiple Choice Questions—Day56

 Pointer related blogs

The first shot of pointer: pointer family-CSDN Blog

In-depth understanding: dereference and addition of pointer variables-CSDN Blog

first question

1. Which of the following statements is correct ()

A: '\0' means character 0

B: "a" represents a character constant

C: The expression: 'a' > 'b' evaluates to false;

D: '\"' is illegal

Answer and analysis C

This question tests the basics

A: The ASCII value of the character ' \0 ' is 0, and the ASCII value of the character ' 0 ' is 48, so they are different;

B: "a" is a string with a hidden \0 behind it. Please note that it is a double quote!

C: Correct, just look at the ASCII value.

D: ' \" ', this is an escape character, \ changes the original meaning of the following ", so \" is a new character, correct usage.

Question 2

2. It is known that int a[] = {0,2,4,6,8,10}, *p = a+1; the expression whose value is equal to 0 is ( )

A:* (p++)

B:*(++p)

C:*(p--)

D:*(--p)

Answer and analysisD

Having been friends for so long, this question should be no problem!

First, p points to the address of the second element of the array a. If the value is equal to 0, the address of the first element of the array must be obtained through p.

Therefore, p must be reduced to exclude AB;

The difference between CD is the preposition of -- and the postposition of --. The value of the expression of prefix -- is the value after --; the postposition of -- is the value before --;

So D:--p is the address of the first element of the array; *(--p) = *a = 0;

Question 3

3. In C language, the following string assignment operations can be performed correctly ()

A:char s[5]={"ABCDE"};

B:char s[5]={‘A’,‘B’,’C’,’D’,’E’};

C:char *s; s="ABCDE";

D:char *s; scanf("%s",s);

Answer and analysis  C

In C language, strings must end with '\0', so when assigning a value to a string, \0 must be assigned;

A: s is defined as a character array with 5 elements, that is, it can store up to 5 characters, and the string end character '\0' cannot be stored;

B: The assignment to the array is correct, but like A, there is no string terminator '\0';

D: "Wild pointer" assignment is wrong:

C: First define the character pointer s, and then assign the first address of the string "ABCDE" to the pointer variable s, s points to this string

Question 4

4. The output result of the following C program is ()

#include <stdio.h>
int main() 
{
    int a[5] = {
    
    1, 2, 3, 4, 5};
    int *ptr=(int *)(&a + 1);
    printf("%d, %d",*(a + 1), *(ptr - 1 ));
    return 0;
}

A:2, 5

B:1, 3

C:1, 5

D:2, 43586

Answer and analysis A

I remember this question appeared 3 times, it’s very classic!

I suggest you take a look at my blog and try it again if you didn’t get it right. If it still doesn’t work, post it in the comment area.

In-depth understanding: dereference and addition of pointer variables-CSDN Blog

Question 5

5. The output result of the following program is ()

#include <stdio.h> 
int main() 
{
    char str1[] = "hello world";  
    char str2[] = "hello world";  
 
    const char str3[] = "hello world";  
    const char str4[] = "hello world";  
 
    const char* pstring1 = "hello world";  
    const char* pstring2 = "hello world";  
 
    if(str1 == str2) 
        printf("true,");
    else
        printf("false,");

    if(str3 == str4) 
        printf("true,");
    else
        printf("false,");

    if(pstring1 == pstring2) 
        printf("true\n");
    else
        printf("false\n");
        
    return 0;
}

A:false,false,true

B: false, false, false

C:true,true,true

D:false,true,true

Answer and analysis A

This question tests whether the compared variables are equal.

First group

        This group compares the array name, which is the address of the first element, that is, whether the two addresses are the same, they must be different, because these are two different variables, and both variables will be allocated on the stack. space, and then store the strings separately;

The second group has the same truth as the first group

The third group

This is to define two pointer variables on the stack area, pointing to a string, and these two pointer variables are the addresses of the first elements of the string stored. So what is compared is the address of the first element of this string; but this string is defined in the constant area, so the upper and lower strings are the same. Because our system is very smart, the strings in the constant area are read-only. Cannot be modified. So there is no need to get you two strings.

Guess you like

Origin blog.csdn.net/2302_76941579/article/details/135115882