Program includes an "8", doesn't reverse digits in beginner's exercise (CodeBlocks)
So I'm writing a program that is supposed to take a two digit number and reverse the digits as an output.
instead, it includes an "8" in the beginning of the two digit number as output.
#include <stdio.h>
int main (void)
{
int x, y;
printf("Enter a two digit number: ");
scanf("%d%d", &x, &y);
printf("The reversal is: %d%d", y, x);
return 0;
}
so when I run the program, it prompts for the number. if I enter "12" it will output "The reversal is: 812".
Just stumped
Edit: Tried it in a different IDE, the program hangs until I make another input, so if I enter "12" it goes to a new line, then if I enter a period "." it outputs "012"
13 comments · 57% upvoted · other discussions
The
%dspecifier reads a decimal number, not a decimal digit. You get an8becauseyis never actually scanned and thus its value is undefined.To fix these issues, you can use
%1dto scan a single digit.Jouissance_juice [OP] 1 points · Mar 2019
That worked, but this is the first time I've seen this problem come up with %d.
Anyway, thanks for your help!
[deleted] -3 points · Mar 2019
[deleted]
FUZxxl 2 points · Mar 2019
%dscans decimal integers as oposed to%ofor octals and%xfor hexadecimal integers.