简体   繁体   English

为什么不显示数字“ 0”(数字,不是char)?

[英]Why the number '0' (number, not char) doesn't displaying?

The problem is that I cannot see 0 in an array. 问题是我无法在数组中看到0

I run my program and see 2D array. 我运行程序,看到2D数组。 But instead of 0 (the first element) I see nothing. 但是我看不到0 (第一个元素)。

Here is the code: 这是代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    int *Ptr;

    scanf("%d%d", &M, &N); /* Size of array. */

    Ptr = malloc(M*N*sizeof(int));

    for (i = 0; i < M * N; i++) /* Filling in. */
    {
        *(Ptr + i) = i;
    }

    for (i = 0; i < M * N; i++) /* Displaying. */
    {
        if (i % N == 0)
            printf("\n");
        printf("%2.d  ", *(Ptr + i));
    }

    return 0;
}

What is the problem? 问题是什么? Is there any way to fix it? 有什么办法可以解决?

The number after the dot is the precision. 点后的数字是精度。 If the precision is 0 (or does not exist) then printf does not print out 0 . 如果精度为0(或不存在),则printf不会打印出0 In your case you do not need the dot: 在您的情况下,您不需要点:

printf("%2d ", ...)

change 更改

printf("%2.d  ", *(Ptr + i));

to

printf("%2d  ", *(Ptr + i));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM