Problem with scanf_s and printf.

Hi everyone,I'm a beginner at this and I was doing some exercises,but every time the result was some big weird number,so i thought "Why don't I write this simple program to see whats wrong *code below*."And I see that even that doesn't work out.Every time it prints out a different number *output also below*,so I'm looking for help here.

If someone could see what could be the problem,and explain it to me a little bit,I would be very thankful.

1
2
3
4
5
6
7
8
9
10
11
12
#include<math.h>
#include<stdlib.h>
#include<stdio.h>

int main()
{
	int a;
	scanf_s("%d", &a);
	printf("%d", &a);

	system("pause");
}


Output:
55
17824528Press any key to continue . . .
The problem is that printf(...) doesn't expect a pointer (while scanf(...) does).

This way you print the pointer not the value. Remove the & in front of a on line 9.
You should understand that &a isn't the value stored in a. It's the memory address of a - what we call a pointer to a, in C and C++.

For scanf, you need to supply the address of a. However, if you want to output the value of a, you should just use a in the printf statement, not &a.
Last edited on
Thank you both,you helped a lot :D.
You're welcome - glad I could help!
Topic archived. No new replies allowed.