'printf returning 6422044 for the value of variable
Question
Basically, I have a simple C code to return the value of a variable, but, if I change the & of the scanf() and the printf the returned value of variables is changed. What is the technical explanation for this event?
(input: 3) printf returning: 3
#include <stdio.h>
#include <stdlib.h>
void main(){
int x = 1;
scanf("%d", &x);
printf("%d", x);
}
(input: 3) printf returning: 6422044
#include <stdio.h>
#include <stdlib.h>
void main(){
int x = 1;
scanf("%d", &x);
printf("%d", &x);
}
(input: 3) printf returning: (nothing)
#include <stdio.h>
#include <stdlib.h>
void main(){
int x = 1;
scanf("%d", x);
printf("%d", &x);
}
Solution 1:[1]
On first code it is simple. You are storing the value in the address of x so you are mentioning &x and you can access the value by x.
on second code your are getting the value as i mentioned above. in printf statement you are prinintg the address of x.
Basically & refers to address you will see lot of them while you studying the concept Pointer.
Solution 2:[2]
just remove the & from printf("%d", &x); , it will definitely solve your problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
| Solution 2 | Omkar Gharat |
