1

It is very easy to convert any variable into a different kind in many languages, which gave me an idea for another converting function, which should act like str() from Python.

So what I found out is that itoa() is a function that turns an int to a char * (string):

#include <stdio.h>
#include <stdlib.h>

int main() {
    int num = 200;
    printf("%s", itoa(num));
}

But as it turns out, itoa() doesn't actually exist in my version of C, which they claim is C99:

make_str.c:6:18: error: implicit declaration of function 'itoa' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    printf("%s", itoa(num));
                 ^
make_str.c:6:18: error: format specifies type 'char *' but the argument has type 'int' [-Werror,-Wformat]
    printf("%s", itoa(num));
            ~~   ^~~~~~~~~
            %d

So I went to make my function instead called make_str(), though I still don't have a plan about how to convert variables into strings:

char *make_str(void *var) {
    
}

Q: What other functions can I use to change the variables into strings?

No, not floating-point values, only int.

deceze
  • 510,633
  • 85
  • 743
  • 889
BMPL
  • 35
  • 16
  • 5
    Have you tried using [`sprintf()`](https://linux.die.net/man/3/sprintf)? – r3mainer Oct 01 '20 at 09:02
  • 1
    `sprintf()`, and [`snprintf()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html) since C99, come to mind – pmg Oct 01 '20 at 09:03
  • 1
    `itoa()` is **not** described by any version of the C Standard, or even POSIX. – pmg Oct 01 '20 at 09:06
  • @BPML If your comment about "output functions" refers to `sprintf` and `snprintf`, you are wrong. Check the difference between `printf` (or `fprintf`) and `sprintf`. – Bodo Oct 01 '20 at 09:30
  • To the people that marked the question a duplicate: Please read both questions before you do that. You, again, linked to a completely different question. The linked question is about Linux, this question is not about Linux, this can be relevant when the OP is on a system with very little RAM and ROM where the `printf()`-family needs too much RAM or ROM. – 12431234123412341234123 Oct 01 '20 at 10:44
  • It is not possible to know the original type from a `void` pointer. The `make_str()` function is not possible without more information, like a second argument for the type. If you are only interested in converting `int`, why not expect a `int`? – 12431234123412341234123 Oct 01 '20 at 10:58

1 Answers1

2

In C, char variables are actually int. All of char variables have a numeric value. You can use it to convert. In ASCII, number codes are like below:
48. '0'
49. '1'
50. '2'
51. '3'
52. '4'
53. '5'
54. '6'
55. '7'
56. '8'
57. '9'

For example, the int 48 means char '0'. Thus you can use this code:

#include <stdio.h>

#include <math.h>

int main(void) {

  int entry, copy, count, dividing = 1, size = 0;
  char IntToChar[50];

  printf("Enter an integer: ");
  scanf("%d", & entry);

  copy = entry;
  if (copy != 0) {
    while (copy != 0) {
      ++size; /*to calculate how many number entry has*/
      copy /= 10; /*for example, if you divide 17(for int, not float) to 10, the result is 1*/
    }
  } else
    size = 1; /*possibility of entry to be 0*/

  for (count = 0; count < (size - 1); ++count)
    dividing *= 10;

  copy = entry; /*assignment again*/

  for (count = 0; count < size; ++count) {
    IntToChar[count] = '0' + (copy / dividing);
    copy %= dividing;
    dividing /= 10;
  }
  IntToChar[count] = '\0'; /*adding end of string*/

  printf("%s", IntToChar);

  return 0;
}

For example,entry is 913. So, size will be 3 and dividing will be 100. If we divide copy to dividing, result is 9. And if we sum up 48('0') with this 9, it gives 57('9').After that, copy%=dividing is 13 and if we divide 13 to 10, it is 1, and we sum up this 1 with 48('0') and result is 49('1') and so on.I hope could help.

Tacticer01
  • 50
  • 6
  • 1
    Why would you want to use some `48` if you can use `'0'` instead? Using magic numbers makes code much worse readable and relies on character encoding. – Gerhardh Oct 01 '20 at 12:47