What I am trying to accomplish here is to split a long integer apart so I can look at its first, last, and center digits. What I am currently using is this:

int main() {
  long i, t[1024];
  
  i = 100;

  while (i < 150) {
    t = i; // My attempt at converting the long into an array.
    printf("%ld\n", t[2]);
    i += 1;
  }

Any help would be appreciated!

Recommended Answers

All 2 Replies

Well. If it's the digits you're interested in you need only do:

lint main( int argc, char *argv[] ) {
  long num = 123;
  int dig1 = num % 10;
  int dig2 = (num/10) % 10;
  int dig3 = (num/100) % 10;
  printf( "%ld = %d %d %d\n", num, dig3, dig2, dig1 );

  return 0;
}

You can ceil the log of the long number to get the number of digits it has.

you can not convert an integer to character array like you tried to do. Use sprintf() instead sprintf(t, "%ld", i);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.