I have this code to break up numbers entered at the console and return them and I do the same for negative numbers, but here is the strange behaviour I enter 0123
and the number gets converted to 83
. I am new to objective-c and c, so I need some explanation why this happened.
I also noticed from debuting this code that the change actually takes place in the scarf function
not even in the main code block.
Here is my code:
//
// main.m
// ex-5.9
//
// Created by george on 05/07/2016.
// Copyright © 2016 george. All rights reserved.
// Program to reverse the digits of a number
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
int number, right_digit;
NSLog(@"Enter your number.");
scanf("%i", &number);
if(number < 0){
number = ABS(number);
do {
right_digit = number % 10;
NSLog(@"%i", right_digit);
number /= 10;
} while (number != 0);
NSLog(@"-");
}else{
do {
right_digit = number % 10;
NSLog(@"%i", right_digit);
number /= 10;
} while (number != 0);
}
}
return 0;
}