1

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.

4 Answers 4

3

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

            printf("%d",x);
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Oh okay. I completely forgot about that. Thanks :)
2

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);

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.