hexadecimals
hexadecimals

Reputation: 9

Arrays: same line of code printing different answers?

I have just started learning C and I have encountered the following problem:

#include <stdio.h>
#define T 1
int G;

int main(void)
{
    int arr[5] = {7,8,9,10,11};
    int a;

    printf("%d\n", G);
    printf("%d\n", arr[T]);
    printf("%d\n", arr[G]);

    a = arr[T]*arr[G];

    printf("%d\n",a);


    printf("%c", arr[T]*arr[G]); //why is this answer printed differently from a?

    return 0;
}

Appreciate your kind help!

Upvotes: 0

Views: 58

Answers (2)

JBM
JBM

Reputation: 107

Considering that you are using G before assigning a value to it, it could print anything. Also your formats are different.

Upvotes: 1

Bijay Gurung
Bijay Gurung

Reputation: 1104

Because you're printing it as a character with "%c".

Try these:

printf("%d", 65);
printf("%c", 65);

And then look at the ascii table. And then read up on printf Format String. All the best.

Upvotes: 2

Related Questions