首页 > 学院 > 开发设计 > 正文

只用getchar函数读入一个整数,存到int类型的数中

2019-11-08 01:06:23
字体:
来源:转载
供稿:网友
/*只用getchar函数读入一个整数,假设它单独占据一行  读到行末为止,包括换行符,输入的数保证可以存到int中 **auhtor:@zk **date:2017 2 21 *///getchar每次只可以读取一个字符,返回的是一个Int类型的数值,//即该字符的ASCII码 #include <stdio.h>#include <stdlib.h>/* run this PRogram using the console pauser or add your own getch, system("pause") or input loop */#define N 100int main(int argc, char *argv[]) {int i;i=0;char a[N];char ch;for(ch;ch = getchar();ch != EOF){if(ch != '/n'){a[i++]= ch;}else{break;}}i= atoi(a);//atoi()将字符串转换为数字,在stdlib中 printf("%d",i); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表