printf打印错误的值

时间:2016-10-13 06:17:18

标签: c printf

我有这段C代码

return mysql_result($query, 0) == 1;

并且它的输出是

#include <stdio.h>

int main(){
        int i , j , m , A[5]={0,1,15,25,20};
        i = ++A[1];
        printf("%d:\n",i);

        j = A[1]++;
        printf("%d:\n",j);

        m = A[i++];
        printf("%d:\n",m);

        printf("%d %d %d",i,j,m);
        return 0;
}

printf不应该将值打印为2,2,15,但为什么要打印3,2,15

P.S:我真的没有滥用这个代码,其他人(也许是我的教授)并且我只是在学习C语言。

4 个答案:

答案 0 :(得分:1)

该行

m = A[i++];

在从数组A获得相应的值后,将在原位增加变量i。

答案 1 :(得分:1)

我作为以下陈述

的一部分递增
       m = A[i++];

答案 2 :(得分:0)

m = A [i ++];

此代码将A [2] 15分配给变量m,然后+1分配给i的当前值变为3。

答案 3 :(得分:0)

让我们看看我们在这里得到了什么..

int i,j,m,A [5] = {0,1,15,25,20};

    i = ++A[1];  // takes the value of A[1], increment it by 1 and assign it to i. now i = 2, A[1] = 2
    printf("%d:\n",i);
    j = A[1]++; // takes the value of A[1](which is 2), assign it to j and increment the value of A[1] by 1. now j = 2, A[1] = 3
    printf("%d:\n",j);

   //remember the value of i? its 2
    m = A[i++]; // takes the value of A[2](which is 15), assign it to m and increment the value of i by 1. now m = 15, i = 3
    printf("%d:\n",m);

    printf("%d %d %d",i,j,m); // Hola! we solve the mystery of bermuda triangle :)
    return 0;