sprintf()函数

来源:互联网 发布:打印机接收数据 不打印 编辑:程序博客网 时间:2024/06/07 10:10

sprintf()原型:int sprintf(char *string,char *fomat,arg1,arg2..)

sprintf函数和printf()函数一样,按照format格式格式化参数arg1,arg2,...但它将输出结果存在在string中,而不是输出到标准输出中,当然,string必须足够大以存放输出结果

spirntf()实例

#include<stdio.h>
#include<string.h>
#define SIZE 40
char *s_gets(char *s, int n)
{
    char *t = fgets(s, n, stdin);
    int i= 0;
    if (t)
    {
        while (s[i] != '\n'&&s[i] != '\0')
        {
            i++;
        }
        if (s[i] == '\n')
            s[i] = '\0';
        else
        while (getchar() != '\n')
            continue;
    }
    return t;

}
void main()
{
    char first[SIZE];
    char second[SIZE];
    char str[2 * SIZE + 10];
    double num;
    printf("you name:");
    s_gets(first, SIZE);
    printf("you sex:");
    s_gets(second, SIZE);
    printf("you age:");
    scanf("%d", &num);
    sprintf(str, "%s,%s,%d", first, second, num);
    printf("%s",str);
    system("pause");
}