sprintf与sscanf用法举例

一、sscanf 

从tmp中读取a,b,c。 

int main(){
    char tmp[30];
    int a;
    double b;
    char c[10];
    while(gets(tmp) != NULL){
        sscanf(tmp, "%d%lf%s", &a, &b, c);
        printf("%d\n%.2lf\n%s\n", a, b, c);
    }
    return 0;
}

二、sprintf

将a,b,c输出到tmp中

int main(){
    char tmp[30];
    int a = 12;
    double b = 2.3;
    char c[10] = "weabab";
    sprintf(tmp, "%d\n%.2lf\n%s", a, b, c);
    printf("%s\n", tmp);
    return 0;
}

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6277536.html