2021年3月2日星期二

Implement a reverse function to reverse an integer but the output here is always 0

I do not understand why the return .... does not work. Somehow, Output is always 0. Here the return call to atoi always outputs 0.

#include <stdio.h>  //INCLUDES  #include <stdlib.h>    int reverse (int x); //Func Decls    int  main ()  {    printf ("%d", reverse (123)); //123=321, -123=-321, 120=21, 0=0    return 0;  }    int  reverse (int x)  {      int i, rem = 0;    char arr[15];      while (x % 10 != 0)      {        rem = x % 10;        arr[i] = ((char)rem);        x /= 10;        i++;      }      return atoi(arr); //OUTPUT = 0, does not return actual output  }   
https://stackoverflow.com/questions/66450428/implement-a-reverse-function-to-reverse-an-integer-but-the-output-here-is-always March 03, 2021 at 11:39AM

没有评论:

发表评论