I’ve just started learning how to program in C and I’m trying to make a program that accepts a number and uses it as an ASCII value to return the ASCII character associated with that value.
The program works when the parameters are predefined but when I introduce the scanf function it compiles but doesnt give me the same results.
Here is my code :
#include <stdio.h>
int main(void)
{
question2();
return 0;
}
int question2(void)
{
int myInt = 65;
scanf("%d", myInt);
char ch = myInt;
printf("%c",ch);
return 0;
}
Cheers and thanks for any help guys.
You need to pass the address of
myInttoscanf()(the compiler should have emitted a warning for this):You should also check the return value of
scanf()to ensuremyIntwas actually assigned to.scanf()returns the number of assignments made, which in this case is expected to be1:Note that
inthas a larger range values than acharso you should check that the value stored inmyIntwill fit into achar. There are macros defined in the headerlimits.hthat you can use to check:The compiler should have also emitted an implicit function declaration warning with respect to
question2(). To correct, place the definition ofquestion2(), or a declaration forquestion2(), prior tomain().