C言語では、整数を文字列に変換する方法は何ですか?

Can convert integers to strings in C using the sprintf function. Prototype of the sprintf function is as follows:

C言語では、整数を文字列に変換する際に sprintf 関数を使用することができます。sprintf 関数のプロトタイプは以下のようになります。

int sprintf(char *str, const char *format, ...);

strは目標文字列のポインタで、formatはフォーマット文字列で、後のパラメータは変換する整数です。

以下はサンプルコードです。

#include <stdio.h>

int main() {
    int num = 12345;
    char str[10];

    sprintf(str, "%d", num);

    printf("The integer is: %d\n", num);
    printf("The string is: %s\n", str);

    return 0;
}

結果は次のとおりです:

The integer is: 12345
The string is: 12345

上記の例では、整数numを文字列に変換するためにsprintf関数を使用し、その文字列を配列strに保存します。最後に、整数と文字列をそれぞれprintf関数で出力します。

bannerAds