第四章 数据的输入与输出

4-2  实型数据的输出

        f格式符,以小数形式输出

        e格式符,以指数形式(科学记数法)输出

#include <stdio.h>

int main(void) { 
    float x=12.345f;
    double y=2.3456;
    printf("x1=%f,x2=%6.2f,x3=%-6.2f,x4=%.2f\n",x,x,x,x);
    printf("x5=%e,x6=%10.2e,x7=%-10.2e,x8=%.2e\n",x,x,x,x);
    printf("y1=%f,y2=%6.2f,y3=%-6.2f,y4=%.2f\n",y,y,y,y);
    printf("y5=%lf,y6=%6.2lf,y7=%-6.2lf,y8=%.2lf\n",y,y,y,y);
    return 0;
}

/*
x1=12.345000,x2= 12.35,x3=12.35 ,x4=12.35
x5=1.234500e+01,x6=  1.23e+01,x7=1.23e+01  ,x8=1.23e+01
y1=2.345600,y2=  2.35,y3=2.35  ,y4=2.35
y5=2.345600,y6=  2.35,y7=2.35  ,y8=2.35
*/

4-3 字符型数据          

        输出一个字符 %c

#include <stdio.h>

int main(void) { 
    char ch='a';
    int i=97;
    
    printf("ch = %c,ch = %d\n",ch,ch);
    printf("i = %c,i = %d\n",i,i);
    return 0;
}

/*
ch = a,ch = 97
i = a,i = 97
*/

        %s 输出字符串

#include <stdio.h>

int main(void) { 
   printf("s1 = %05.2s,s2 = %-5.2s,s3 = %.2s,s4 = %3s,s5 = %s","abcd","abcd","abcd","abcd","abcd");
    return 0;
}
/*
s1 =    ab,s2 = ab   ,s3 = ab,s4 = abcd,s5 = abcd
*/

4-5 putchar

#include <stdio.h>

int main(void) { 
    char c1='A',c2=66;
    int c3='\103',c4;
    c4=c3+1;
    putchar(c1);
    putchar(c2);
    putchar('\n');
    putchar(c3);
    putchar(c4);
    putchar('\n');
    return 0;
}

/*
AB
CD
*/

4-6  scanf

        格式控制字符串中存在普通字符串,一般用来分割输入的数据项。在运行程序需要输入数据时,必须按照该字符的内容原封不动地用键盘输入到显示器上。如果输入内容和格式字符串不一致,则scanf函数立即结束。

        如以下程序,输入为 a=1,b=2,c=3

#include <stdio.h>

int main(void) { 
    int a=0,b=0,c=0;
    printf("Input a = ,b= ,c=\n");
    scanf("a=%d,b=%d,c=%d",&a,&b,&c);
    printf("a=%d,b=%d,c=%d",a,b,c);
    return 0;
}
/*
Input a = ,b= ,c=
a=1,b=2,c=3
*/

4-7 字符型和数字型数据混合输入

        

#include <stdio.h>

int main(void) { 
    int a=0,b=0,c=0,d=0;
    printf("Input a,b,c,d\n");
    scanf("%d%c%c%c",&a,&b,&c,&d);
    printf("a=%d,b=%c,c=%c,d=%c",a,b,c,d);
    return 0;
}
/*intput:
        123abc
*/

/*
a=123,b=a,c=b,d=c
*/

4-8  getchar

        只能接受一个字符

        可以将获得的字符赋值给int型char型变量

        可以接收键盘输入的不必要的回车或空格,使运行的程序暂停

#include <stdio.h>

int main(void) { 
    char c1;
    int c2;
    printf("\ninput 3 characters:\n");
  
    c1=getchar();
    putchar(c1);
    
    c2=getchar();
    putchar(c2);
    
    putchar(getchar()); //将键盘输入的字符直接输出
    
    putchar('\n');
    
    return 0;
}
/*intput:
        FBI
*/

/*
input 3 characters:
FBI
*/

4-9  使用getchar 接收键盘输入的不必要的回车或者空格

#include <stdio.h>

int main(void) { 
    int a;
    char b;
    printf("please  input a and b:\n");
    scanf("%d",&a);
    getchar();//接收上条语句中的回车避免将其作为有效字符输入给字符变量
    scanf("%c",&b);
    printf("a=%d,b=%c\n",a,b);
    
    return 0;
}
/*intput:
       12
       k
*/

/*
please  input a and b:
a=12,b=k
*/

4-10  用getchar 实现暂停

        

#include <stdio.h>

int main(void) { 
    printf("step 1\n");
    printf("step 2\n");
    printf("step 3\n");
    printf("按Enter键继续\n");
    getchar();
    printf("step 4\n");
    printf("step 5\n");
    printf("step 6\n");
    
    return 0;
}
/*intput:
    按回车键
*/

/*
step 1
step 2
step 3
按Enter键继续

step 4
step 5
step 6
*/

4-11  输入直角三角形的两个直角边的边长,求斜边的长和面积。

        

#include <stdio.h>
#include <math.h>

int main(void) { 
    double a,b,c,area;
    printf("input a and b\r\n");
    scanf("%lf%lf",&a,&b);
    printf("a=%lf,b=%lf\r\n",a,b);
    c=sqrt(a*a+b*b);
    area=1./2*a*b;
    printf("c=%7.2lf\r\narea=%7.2lf\r\n",c,area);
    
    return 0;
}
/*intput:
    3 4
*/

/*
input a and b
a=3.000000,b=4.000000
c=   5.00
area=   6.00

*/

4-12   以scanf函数输入并输出数据

#include <stdio.h>
#include <math.h>

int main(void) { 
    char c;
    int i,j;
    float x,y;
    printf("input c,i,j,x,y\n");
    scanf("%c,%d,%d,%f,%f",&c,&i,&j,&x,&y);
    printf("ch=\'%c',ASII=%d\n",c,c);
    printf("i=%-3d,j=%d\n",i,j);
    printf("x=%-8.2f,j=%.2f\n",x,y);
    return 0;
}
/*intput:
   A,1,2,12.34,56.78
*/

/*
input c,i,j,x,y
ch='A',ASII=65
i=1  ,j=2
x=12.34   ,j=56.78

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值