The formatters in scanf()
and printf()
doesn't match the type of your variables n
and x
.
%d
uses the variables as they were int
while int
has probably the double number of bytes as short int
. (Integer types)
Hence, with the wrong formatters, scanf()
uses the provided addresses wrong.
For printf()
it's a bit more complicated: The short int
s are converted to int
internally. (Default argument promotions) Hence, printing short int
with %d
(as they were int
) doesn't fail.
So, it's the scanf()
what must be fixed.
Either use correct formatters:
#include <stdio.h>
int main()
{
short int n,x;
scanf("%hd",&n);
scanf("%hd",&x);
printf("%d %d",n,x);
return 0;
}
Live Demo on ideone
or use correct variable type for the formatters:
#include <stdio.h>
int main()
{
int n,x;
scanf("%d",&n);
scanf("%d",&x);
printf("%d %d",n,x);
return 0;
}
Live Demo on ideone
The formatting of scanf()
and printf()
families are very powerful and flexible but unfortunately very error-prone as well. Using them wrong introduces Undefined Behavior. The compiler is (usually) unable to recognize errors as the evaluation of formatters happens at run-time and inside the scanf()
/printf()
functions. So, they have to be used caaarefully.