[英]How to get multiple input using scanf c
#include <stdio.h>
int main()
{
char name[10];
int birth_year;
printf("Enter your name : ");
scanf("%c",name);
printf("Enter your birth year : ");
scanf("%i",&birth_year);
int age = 2020 - birth_year;
printf("Your age is %i",age);
}
I am trying to take the value of birth_year as an input but it automatically assigns it to 0 for some reason what am I doing wrong我正在尝试将birth_year 的值作为输入,但由于某种原因它会自动将其分配为 0 我做错了什么
In the first scanf you should read a string instead of a char, that should do it.在第一个 scanf 中,您应该读取一个字符串而不是一个字符,应该这样做。 Also, it's always good to have a whitespace before you read a char, so it resets the buffer memory.
此外,在读取字符之前有一个空格总是好的,因此它会重置缓冲区 memory。
#include <stdio.h>
int main()
{
char name[10];
int birth_year;
printf("Enter your name : ");
scanf(" %s",name);
printf("Enter your birth year : ");
scanf(" %d",&birth_year);
int age = 2020 - birth_year;
printf("Your age is %i",age);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.