Melt
Melt

Reputation: 39

Variable Initialization Bug?

So I declared a variable as an integer and initialized it to 0. When I tried printing the value of that variable, it gave me an incredibly high number. int

x=0;
printf("%d", &x);

This is what I did. Am I doing anything wrong? Thanks in advance.

Upvotes: 1

Views: 45

Answers (4)

krishna
krishna

Reputation: 146

Please print the value of X instead address like printf("%d",x);

Upvotes: 2

Esakki Thangam
Esakki Thangam

Reputation: 355

The operator '&' represent the address of that variable. we need, the actual value of that variable use like this...

            printf("%d",x);

Upvotes: 3

Rajalakshmi
Rajalakshmi

Reputation: 681

You print the address of the x so it will print the address during the compile time it will display the warning warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
So use following
printf("%d",x);

Upvotes: 2

Bhuvanesh
Bhuvanesh

Reputation: 1279

When we are using the &x, it will refer the address of the x . we need to print the value of x then use this,

 printf("%d", x);

In scanf() function, only we need to use &x, to locate the memory address to store the value.

Upvotes: 2

Related Questions