【C语言】04-输入输出

此笔记由个人整理

尚观C语言

一、格式化输入输出函数:scanf、printf

printf

int printf(const char *format, ...);
  • format:"%[修饰符]格式字符"
  • printf("%[修饰符]格式字符,[输出表项]");
#include "stdio.h"
#include "stdlib.h"

int main()
{
    int a = 1,b = 2;
    printf("hello world!\n");
    printf("a = %d,b = %d\n",a,b);
    
    exit(0);
}
  • 格式字符
image-20200607111541845
  • 修饰符
image-20200607111627066
#include "stdio.h"
#include "stdlib.h"
#define STRSIZE 32

int main()
{
    int i = 123;
    float f = 123.456;
    char str[STRSIZE] = "helloworld";
    
    printf("i = %4d\n",i);			//结果:i =   123(左补空格)
    printf("i = %2d\n",i);			//结果:i =  123(原样输出)
    
    printf("f = %f\n",f); 			//结果:f =  123.456001(大概范围的表示)
    printf("f = %8.1f\n",f);			//结果:f =     123.4(大概范围的表示)
    
    printf("%s\n",str);				//结果:helloworld
    printf("%.5s\n",str);			//结果:hello
    printf("%-10.5s[over]\n",str);	 //结果:hello     [over]
    
    printf("i = %o\n",i);			//结果:i = 173
    printf("i = %#o\n",i);			//结果:i = 0173
    printf("i = %x\n",i);			//结果:i = 78
    printf("i = %#x\n",i);			//结果:i = 0x78
    
    exit(0);
}

‘\n’可以刷新缓冲区

scanf

int scanf(const char *format, 地址表);
  • 输入案例
#include "stdio.h"
#include "stdlib.h"

int main()
{
    int i;
    float f;
    
    printf("please enter:\n");
    scanf("%d%f",&i,&f);//两个数据中间不要随意加东西
    printf("i = %d\n",i);
    printf("f = %f\n",f);
    exit(0);
}
//输入:345 38.83
//输出:   i = 345
//		  f = 38.830003
  • 输入字符串
    • 用scanf输入字符串十分危险,容易出现越界现象
#include "stdio.h"
#include "stdlib.h"

int main()
{
    int i;
    float f;
    
    printf("please enter:\n");
    scanf("%d%f",&i,&f);//两个数据中间不要随意加东西
    printf("i = %d\n",i);
    printf("f = %f\n",f);
    exit(0);
}
  • 在while循环中scanf
    • scanf的返回值:输入成功->1 输入失败->0
#include "stdio.h"
#include "stdlib.h"

int main()
{
    int i,ret;
    float f;
    
    printf("please enter:\n");
    
    while(1)
    {
        ret = scanf("%d",&i);
        if(ret != 1)
            break;
        printf("i = %d\n",i);
    }
    exit(0);
}
  • 多个输入函数依次使用(抑制符%*)
#include "stdio.h"
#include "stdlib.h"

int main()
{
    int i;
    float f;
	char ch;
    
    printf("please enter:\n");
    
    scanf("%d",&i);
    scanf("%c",&ch);	    //结果1:i = 32,ch = 10 -> 空格
    scanf("%*c%c",&ch); 	//结果2:i = 32,ch = 97 -> a
    
    printf("i = %d,ch = %d\n",i,ch);
    exit(0);
}

二、字符输入输出函数:getchar、putchar

getchar

int getchar(void);

putchar

int putchar(int c);
#include "stdio.h"
#include "stdlib.h"

int main()
{
	char ch;
    ch = getchar();
    putchar(ch);
}

三、字符串输入输出函数:gets()、puts

gets()

  • gets是一个十分危险的函数
  • 不会检查当前写缓冲区内容是否越界
  • 可以使用fgets或getline函数代替

puts

#include "stdio.h"
#include "stdlib.h"
#define STRSIZE 32

int main()
{
    char str[STRSIZE];
    gets(STR);
    puts(STR);
    
    exit(0);
}
  • 结果

image-20200613204125628

四、练习

1)一个水分子的质量大约为3.0e-23g,一夸脱水大约有950g,要求输入水的夸克数,然后显示这么多夸克水中包含有大概多少水分子。

#include "stdio.h"
#include "stdlib.h"
#define WEIGHT 3.0e-23	//水分子的质量
#define KQ     950		//一夸脱水质量

static void water(void)
{
	float num;//水的夸脱数
    float sum;//总共多少
    
    printf("please input for num:");//屏幕提示
    scanf("%f",&num)//输入
        
    sum = KQ * num / WEIGHT;//数学换算
    
    printf("total is %e\n",sum);
    return ;
}

int main()
{
 	water();   
    exit(0);
}
  • 结果

image-20200613204216943

2)从终端输入三角形的三边长求三角形的面积,要有自检部分。s=1/2(a+b+c) ,area=sqrt(s*(s-a)(s-b)(s-c))

#include "stdio.h"
#include "stdlib.h"
#include "math.h"


static void area(void)
{
    float a,b,c;
	float s,area;

    printf("please input:");
    scanf("%f%f%f",&a,&b,&c);
    if(a+b<=c || b+c<=a || c+a<=b)
    {
        fprintf(stderr,"EINVAL\n");
        exit(0);
    }
    s = 1.0 / 2.0 * (a + b + c);
   	area = sqrt(s * (s - a) * (s - b) * (s - c));
   	printf("area = %f\n",area); 
    return ;
}

int main()
{
	area();
    exit(0);
}
  • 结果

image-20200613204304719

3)从终端输入abc的值求方程的根

  • 例:求ax^2 + bx + c=0,b^2 - 4ac>0

    x1=(-b+sqrt(b^2-4ac)/2a

    x2=(-b-sqrt(b^2-4ac)/2a

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

static void root(void)
{
    float a,b,c,p,q;
    float disc;
    float x1,x2;
    
    printf("please input:");
    scanf("%f%f%f",&a,&b,&c);
    if((b*b - 4*a*c) < 0)
    {
        fprintf(stderr,"EINVAL\n");
        exit(1);
    }
    
    disc = b*b-4*a*c;
    p = -b/2*a
    q = sqrt(disc)/2*a;
    
    x1 = p + q;
    x2 = p - q;
    printf("x1 = %f\nx2 = %f\n",x1,x2);
    
    return ;
}

int main()
{
	root();
    exit(0);
}
  • 结果

image-20200613204501778

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值