Linux C学习第四天(输入&输出)

输入&输出
一、输出
1.格式
    printf("%d",x);
    printf("*****");
十进制整数: %d
八进制:        %o-->八进制打印 || %#o    表示打印时加入前缀#o
十六进制:    %x-->十六进制打印 || %#x    表示打印时加入前缀#x
浮点型:        %f
%c    %ld……
详细可以参考:
2.修饰符
  • 输出字符的宽度,超过设置宽度时原样输出;
--->printf("%8d",x);    表示用八个字节打印, 右对齐,不够补空格
  • 如果想要左对齐
--->printf("%-8d");    也是八个字节打印, 左对齐,不够补空格
  • 输出的小数位数
--->printf("%8.3f");    八个字节打印,三个小数位,即精度位1‰
PS:printf("% 08d");    表示8位不够的时候补0
        嵌套使用。
#include<stdio.h>
int main(int argc, char *argv[])
{
    printf("|%8d|\n",   123);
    printf("|%8d|\n",   1234567890);

    printf("|%-8d|\n",   123);
    printf("|%08d|\n",   123);

    printf("|%x|\n", 0x1122abCD);
    printf("|%X|\n", 0x1122abCD);
    printf("|%#x|\n", 0x1122abCD);
    printf("|%#X|\n", 0x1122abCD);


    float f = 10/3.0;
    printf("|%f|\n", f);
    printf("|%10.2f|\n", f);
    
    double  d = 20/3.0;
    printf("|%f|\n", d);
    printf("|%.8f|\n", d);
    printf("|%.18f|\n", d);

    int n;
    n = printf("hello");
    printf("%d\n", n);
    printf("%d\n", printf("hello"));

    n = printf("%d\n", 12345);
    printf("%d\n", n);
    printf("%d\n", printf("%d\n", 12345));

一切皆文件
对打开的文件进行编号,程序运行时自动打开了3个文件(0、1、2)
0-->标准输入stdin
1-->标准输出stdout
2-->标准出错stderr
#include<stdio.h>
int main(int argc, char *argv[])
{
    //printf("hello world");
    //printf("\n");
    //fflush(stdout);
    int i;
    for (i=0; i<1023; i++)
    {
        printf("A");
    }
    printf("B"); //1024
    //printf("B"); //1025
    while (1);
}
#include<stdio.h>
int main(int argc, char *argv[])
{
    //printf("hello world");
    //printf("\n");
    //fflush(stdout);
    int i;
    for (i=0; i<1023; i++)
    {
        printf("A");
    }
    printf("B"); //1024
    //printf("B"); //1025

    while (1);

}
二、输出
格式:scanf("%▢",&a);
*    是抑制符,控制输入项的读入。
输入分隔符:
默认的是:空格、TAB、回车-->但是char(%c)会读

scanf    有严格的输入格式要求
#include<stdio.h>
int main(int argc, char *argv[])
{
    int a;
    scanf("%d", &a);  //址传递,通过scanf改变a,就要传递a的地址
    printf("%d", a);  //值传递,只是将a的值打印
}
#include<stdio.h>
int main(int argc, char *argv[])
{
    int a;
    scanf("%d", &a);  //址传递,通过scanf改变a,就要传递a的地址
    printf("%d", a);  //值传递,只是将a的值打印
}
#include<stdio.h>
int main(int argc, char *argv[])
{
    char t;
    int a, b , c;
    //scanf("%d", &a);
    //%d前后可以有任意个数的分隔符,scanf会自动过滤
    //scanf("a=%d", &a);
    //scanf(" a=%d", &a); //只要中间有a=就可以
    scanf("a=%db", &a); //a=  998b\n  或 a=998\n
    //scanf输入时必须以a=开头,会自动过滤开头的a=以及整数前的分隔符,且会过滤结尾的字符b
    scanf("%c", &t);
    
    printf("%d\n", a);
    printf("%d\n", t);

}
#include<stdio.h>
int main(int argc, char *argv[])
{
    int a, b, c;
    //scanf("%d%d%d", &a, &b, &c); // 12   13   14
    //scanf("%d %d %d", &a, &b, &c); // 12   13   14
    //scanf("a=%db=%dc=%d", &a, &b, &c); //a=   12b=  13c=14  
    scanf("%d,%d,%d", &a, &b, &c);

    printf("%d %d %d\n", a, b, c);

}

getchar/putchar        字符串的输入/输出
#include<stdio.h>
int main(int argc, char *argv[])
{
    char c;
    int a;
    scanf("%d", &a);  //233\n
    //getchar();
    //scanf  ("%c", &c);
    scanf("%*c%c", &c);  //先读一个扔了不要
    //scanf(" %c", &c); //前面的空格俺一个都不读,
    printf("%d\n", a);
    printf("%d\n", c);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值