且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C语言及程序设计初步例程-16 数据的输出

更新时间:2022-10-12 20:37:49

贺老师教学链接  C语言及程序设计初步 本课讲解


用printf函数输出

#include <stdio.h>
int main()
{
    printf("Hello World!\n"); 
    printf("Welcome\nto\nBeijing!\n");
    return 0;
} 

用于整型数据的输出格式控制
#include <stdio.h>
int main(){
    printf("%d\n", 1234);
    printf("%6d\n", 1234);
    printf("%o\n", 1234);
    printf("%x\n", 1234);
    printf("%X\n", 1234);
    printf("%u\n", -1234);
    return 0;
}

用于浮点型数据的输出格式控制
#include <stdio.h>
int main(){
    printf("%f\n", 1234.56);
    printf("%10.3f\n", 1234.56);
    printf("%e\n", 1234.56);
    printf("%E\n", 1234.56);
    printf("%g\n", 1234.567);
    printf("%g\n", 1234567.89);
    return 0;
}

避免参数和转换描述之间的类型的不匹配
#include <stdio.h>
int main()
{
    int a, b, c;
    scanf("%d %d", &a, &b);
    c = a + b;
    printf("%d + %d = %d\n", a, b, c);
    printf("%f + %f = %f\n", a, b, c);
    return 0;
}

避免参数和转换描述之间个数不匹配
#include <stdio.h>
int main()
{
    int a, b, c;
    scanf("%d %d", &a, &b);
    c = a + b;
    printf("%d + %d = %d\n", a, b);
    printf("%d + %d = %d\n", a, b, c, a);
    return 0;
}