参考网上:
#include <stdio.h>
#include<stdlib.h>
int main(){
long n, j;
int count = 0;
printf("please input number:");
scanf_s("%d", &n);
j = n;
while (n != 0){
n = n / 10;
count++;
}
printf("input number is:%d, len is:%d\n", j, count);
return 0;
}
结果回车后一闪而过,显然自己退出了,要加个阻塞,于是
#include <stdio.h>
#include<stdlib.h>
int main(){
long n, j;
int count = 0;
printf("please input number:");
scanf_s("%d", &n);
j = n;
while (n != 0){
n = n / 10;
count++;
}
printf("input number is:%d, len is:%d\n", j, count);
getchar();
return 0;
}
加了getchar()结果还是一闪没了,怎么都想不通原来是个低级错误,因为getchar()获得一个字符包括回车符,结果可想而知就退了,正确方法如下:
#include <stdio.h>
#include<stdlib.h>
int main(){
long n, j;
int count = 0;
printf("please input number:");
scanf_s("%d", &n);
j = n;
while (n != 0){
n = n / 10;
count++;
}
printf("input number is:%d, len is:%d\n", j, count);
system("pause");
return 0;
}
应该是system这个函数。看起来getchar还要慎用,尤其在输入数据集时候