如何在没有 itoa() 的情况下将任何变量更改为字符串?
How do I change any variable into a string without itoa()?
在许多语言中,将任何变量转换为不同类型的变量非常容易,这让我想到了另一个转换函数,它应该像 Python.[=21 中的 str()
=]
所以我发现 itoa()
是一个将 int
转换为 char * (string)
的函数:
#include <stdio.h>
#include <stdlib.h>
int main() {
int num = 200;
printf("%s", itoa(num));
}
但事实证明,itoa()
实际上并不存在于我的 C 版本中,他们声称它是 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
所以我开始编写我的函数而不是调用 make_str()
,尽管我仍然没有关于如何将变量转换为字符串的计划:
char *make_str(void *var) {
}
问:我还可以使用哪些函数将变量转换为字符串?
No, not floating-point values, only int
.
在 C 中,char
变量实际上是 int。所有 char 变量都有一个数值。你可以用它来转换。在 ASCII 中,数字代码如下所示:
48.'0'
49. '1'
50. '2'
51. '3'
52. '4'
53. '5'
54. '6'
55.'7'
56.'8'
57. '9'
例如,int 48 表示 char '0'。因此你可以使用这个代码:
#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] = '[=10=]'; /*adding end of string*/
printf("%s", IntToChar);
return 0;
}
例如,entry是913。所以,size就是3,dividing将是 100。如果我们将 copy 除以 dividing,结果是 9。如果我们将 48('0') 与这个 9 相加,它给出 57('9')。之后,copy%=dividing 是 13,如果我们将 13 除以 10,它是 1,我们将这个 1 与 48(' 0') 结果是 49('1') 所以 on.I 希望能有所帮助。
在许多语言中,将任何变量转换为不同类型的变量非常容易,这让我想到了另一个转换函数,它应该像 Python.[=21 中的 str()
=]
所以我发现 itoa()
是一个将 int
转换为 char * (string)
的函数:
#include <stdio.h>
#include <stdlib.h>
int main() {
int num = 200;
printf("%s", itoa(num));
}
但事实证明,itoa()
实际上并不存在于我的 C 版本中,他们声称它是 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
所以我开始编写我的函数而不是调用 make_str()
,尽管我仍然没有关于如何将变量转换为字符串的计划:
char *make_str(void *var) {
}
问:我还可以使用哪些函数将变量转换为字符串?
No, not floating-point values, only
int
.
在 C 中,char
变量实际上是 int。所有 char 变量都有一个数值。你可以用它来转换。在 ASCII 中,数字代码如下所示:
48.'0'
49. '1'
50. '2'
51. '3'
52. '4'
53. '5'
54. '6'
55.'7'
56.'8'
57. '9'
例如,int 48 表示 char '0'。因此你可以使用这个代码:
#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] = '[=10=]'; /*adding end of string*/
printf("%s", IntToChar);
return 0;
}
例如,entry是913。所以,size就是3,dividing将是 100。如果我们将 copy 除以 dividing,结果是 9。如果我们将 48('0') 与这个 9 相加,它给出 57('9')。之后,copy%=dividing 是 13,如果我们将 13 除以 10,它是 1,我们将这个 1 与 48(' 0') 结果是 49('1') 所以 on.I 希望能有所帮助。