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.

6
  • 5
    Have you tried using sprintf()? Commented Oct 1, 2020 at 9:02
  • 1
    sprintf(), and snprintf() since C99, come to mind Commented Oct 1, 2020 at 9:03
  • 1
    itoa() is not described by any version of the C Standard, or even POSIX. Commented Oct 1, 2020 at 9: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. Commented Oct 1, 2020 at 9: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. Commented Oct 1, 2020 at 10:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.