'I got an error in C while making a quick test program [closed]
I know other versions of this questions exist, but all of the answers for them confuse my beginner brain. All I am trying to do is take a single number as input and then output it, here is the code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int var;
scanf("%u\n", var);
printf("%d\n", var);
}
and the error message I got:
warning: format ‘%u’ expects argument of type ‘unsigned int *’, but argument 2 has type ‘int’ [-Wformat=]
The weird thing is that I tried all the format specifiers for integers that I could think of (%d %u %i) and they all returned a similar error.
I'm still a beginner in C so this might be a really stupid question but go easy on me lol
Solution 1:[1]
scanf needs to be able to write into var. Currently, you're only passing it a copy of the value of var, but that's of no use to it.
Instead, you need to pass in a pointer to var, so that it can write into that pointer's location:
#include <stdio.h>
#include <stdlib.h>
int main() {
int var;
scanf("%d", &var);
printf("%d\n", var);
}
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 | Alexander |
