C语言输入输出格式

1.转换说明符

%c 字符

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char a;
    scanf("%c",&a);
    printf("%c
",a);
    system("pause");
    return 0;
}

在这里插入图片描述
%d 有符号十进制整数

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a;
    scanf("%d",&a);
    printf("%d
",a);
    system("pause");
    return 0;
}

在这里插入图片描述
%f 浮点数

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float a;
    scanf("%f",&a);
    printf("%f
",a);
    system("pause");
    return 0;
}

在这里插入图片描述
%o 八进制整数

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a;
    scanf("%d",&a);
    printf("%o
",a);
    system("pause");
    return 0;
}

在这里插入图片描述
%x 十六进制整数

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a;
    scanf("%d",&a);
    printf("%x
",a);
    system("pause");
    return 0;
}

在这里插入图片描述
%s 字符串

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char a[5];
    gets(a);
    printf("%s
",a);
    system("pause");
    return 0;
}

在这里插入图片描述

2.f格式符指定数据宽度和小数位数,用%m.nf。

m指定数据宽度,n指定小数位数
例:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float a;
    scanf("%f",&a);
    printf("%20.15f
",a/3);
    system("pause");
    return 0;
}

在这里插入图片描述
%20.15f
指定数据宽度为20位,小数位数15位,可以看出,整数部分33前面还有两个空格。算上小数点一共是20个位置。

另:在用%f输出时要注意数据本身能提供的有效数字,如float型数据存储单元只能保证6位有效数字。double型数据能保证15位有效数字。

3.对齐方式

输出的数据向左对齐,用%-m.nf
输出的数据向右对齐,用%m.nf
例:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float a;
    scanf("%f",&a);
    printf("%-25.15f,%25.15f
",a/3,a/3);
    system("pause");
    return 0;
}

在这里插入图片描述
有负号,输出结果向左靠,右端空5列;无负号,输出结果向右靠,左端空五列。

以上为个人总结,如有问题,请大佬留言指正

原文地址:https://www.cnblogs.com/HBU-xuhaiyang/p/12520649.html