您的位置:首页 > 编程语言 > C语言/C++

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

2015-01-28 12:20 260 查看
贺老师教学链接 C语言及程序设计初步 本课讲解

不同类型数据的自然分割

#include <stdio.h>
int main()
{
  int a, b, c;
  char op;
  scanf("%d%c%d",&a,&op,&b);
  if(op=='+')
  {
     c=a+b;
     printf("会算%c,结果是:%d\n", op, c);
  }
  else
  { 
     printf("不会算%c\n", op);
  }
  return 0;
}

输入的数据暂放都在“缓冲区”
//运行时输入1 2 3 4 5
#include <stdio.h>
int main(){
    int a, b, c;
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    printf("%d %d %d\n", a, b, c);
    return 0;
}

指定宽度的输入
//运行时输入1234567
#include <stdio.h>
int main()
{
    int a, b;
    scanf("%2d%3d",&a, &b);
    printf("%d %d\n", a, b);
    return 0;
}

细节:注意控制字符和类型的匹配
#include <stdio.h>
int main()
{
    double a, b;
    int c, d;
    scanf("%lf%lf",&a, &b);
    scanf("%d",&c);
    scanf("%d",&d);
    ……
    return 0;
}

细节:读取到某一地址
#include <stdio.h>
#include <stdlib.h>
int main(){
    int a, b, *p;
    p = &b;
    scanf("%d %d",&a, p);
    printf("%d %d", a, *p);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: