Solve the static problem in C

Updated on technology 2024-02-08
12 answers
  1. Anonymous users2024-02-05

    This problem requires understanding that the system allocates memory to static variables when compiling, and the memory units occupied by them are not released after the function call ends, that is, the value of the variable is the value of the previous function call at the next function call.

    When i=0, execute a+=f(); call the function f(); In this case, s=1+0, i=1; in the f() functionthen a=1;

    When i=1, continue to run a+=f(); call the function f(); In this case, s=1+1, i=2; in the f() functionthen a=1+2=3

    When i=2, continue to run a+=f(); call the function f(); In this case, s=1+2, i=3 in the f() functionthen a=3+3=6

    When i=3, continue to run a+=f(); call the function f(); In this case, s=1+3, i=4; in the f() functionthen a=6+4=10

    When i=4, continue to run a+=f(); call the function f(); In this case, s=1+4, i=5 in the f() functionthen a=5+10=15

    When i=5, continue to run a+=f(); call the function f(); In this case, s=1+5, i=6 in the f() functionthen a=15+6=21

    At this point, i=6 in the main function, the program ends. Note that the i in the main function has nothing to do with the i in the modulated function.

    Results: 21

  2. Anonymous users2024-02-04

    If you don't use a static variable, the initial value of i in f() is always zero, so that every time s are returned, it is 1

  3. Anonymous users2024-02-03

    Static variables are initialized at the time they are defined. The default initialization value of the int type is 0. After that, every change will affect it.

    For example, in a program, every time the f function is called, i will be added. Instead of initializing i every time the f function is called.

    The i in the main function is not the same as the i in the f function. In the main function, the i is not static, but the i declared by the main function itself, while in the f function, the i of the main function is masked.

  4. Anonymous users2024-02-02

    Teenager, you miscalculated, the first time s=1, i=0 returns s=1;The second time s=1, i=1 returns s=2; The s-values returned for the six times are , which add = 21

  5. Anonymous users2024-02-01

    In the 9th trap question, there is no break after each case; So, no matter how much M types, the final answer is always A

    Question 8 is still a trap question, the second if is degraded to the first else, which gives people the illusion that it is a branch of else, and the purpose of the investigation is meaningless, in fact, it is a new if, else judgment, which has nothing to do with the first if else, so x=y=-5

    Question 16. The operation is skipped when i is a multiple of 2 and 3, so the first cout is to output a number between 1 and 20 that is not a multiple of 2 and 3, and then sum these numbers, save them to s, and finally output.

    Final question. if(i==10)break;When i reaches 10, it jumps out.

    So the number of logical operations below is between 1 and 9.

    if(i%3 != 1)continue;

    This sentence means that if i is not 1 for 3, the operation is skipped, and between 1 and 9 for 3 is 1, there are only 3 numbers 1, 4, and 7, so the output is 147

    Friend, please [adopt the answer], your adoption is the motivation for me to answer the question, if you don't understand, please ask. Thank you.

  6. Anonymous users2024-01-31

    9.Input 65, which should output b, but because there is no break statement, a is output

    8.Because the first else is followed by a semicolon, the first if is executed, followed by the second if

    16.Calculate the sum of prime numbers within 20.

    13.Calculate the number with a remainder of 1 divided by 3 within 10.

  7. Anonymous users2024-01-30

    Personally, I think it's a time.,Later, I knocked a ** myself.,I found that k can't be output.,Then it's a loop countless times.,And then I took a look at the inside of the for loop.。

    Actually, it's like this: for(;; k=1;) k++;

    And then you carefully compare with this for(; k==1;) k++;

    The constraint in the for loop given by the question does not exist, k=1 is actually the value of k at the beginning of each cycle is equal to 1, and if it is replaced by k==1, then the condition of this cycle becomes k equals 1 when the loop can be carried out, please note that k=1, the difference between k==1 and k==1, == is the judgment condition, and = is the assignment so that the value of k is 1;

  8. Anonymous users2024-01-29

    Infinite, is an assignment, not equal.

  9. Anonymous users2024-01-28

    Hello int (*pt)[3] This is a two-dimensional array that declares an indeterminate line marker, which is mainly distinguished from int *pt[3], which is an array of pointers with 3 pointers, variables.

    Give the landlord an example to help you understand:

    #include

    int main(void)

    ;int (*pt)[3];

    int i,j;

    pt=a;This is to assign the two-dimensional array name a to the dynamic two-dimensional array pt, precisely because the line mark of pt is uncertain, then pt is a pointer variable that can be assigned, and array a is a line marker to determine a is a pointer constant.

    printf("a:");

    for(i=0;i<2;i++)

    printf("pt:");

    for(i=0;i<2;i++)

    printf("");

    return 0;

    You will find that the result of the program is the same as that of A, which proves its correctness, and I hope it can help you.

  10. Anonymous users2024-01-27

    It can be understood like this:

    int a=1;

    pt=&a;Assign the address of a to pt;

    2: [3] indicates an array variable;

    Then (*pt)[3] represents an array of pointer type variables, the number of which is 3;

    int(*pt)[3] represents an array of pointer variables of type int with the number 3.

    So I personally feel that the answer is b

  11. Anonymous users2024-01-26

    An array of three elements is defined, each of which is a pointer variable pointing to an int of memory space, and the answer is a

  12. Anonymous users2024-01-25

    () has the highest priority, so you should be able to understand it a little bit.

    #define qt *pt

    int qt[3];

Related questions