yulian
yulian

Reputation: 1627

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

The problem is that I cannot see 0 in an array.

I run my program and see 2D array. But instead of 0 (the first element) I see nothing.

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?

Upvotes: 1

Views: 94

Answers (2)

perreal
perreal

Reputation: 97938

The number after the dot is the precision. If the precision is 0 (or does not exist) then printf does not print out 0. In your case you do not need the dot:

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

Upvotes: 5

Sheng
Sheng

Reputation: 3555

change

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

to

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

Upvotes: 2

Related Questions