请解释一下这个C代码

请解释一下这个C代码,c,validation,integer,C,Validation,Integer,我为一个类练习编写了部分代码,代码正在运行,但我并不真正理解某个特定部分中发生了什么 请任何人解释一下这一部分: if(scanf("%d%c", &num1, &term1) != 2 || term1 != '\n'){ printf("ERROR! You should type an integer e.g 5, 80, 100\n"); } else { .... 完整的代码是: #include <stdio.h> int main(){

我为一个类练习编写了部分代码,代码正在运行,但我并不真正理解某个特定部分中发生了什么

请任何人解释一下这一部分:

if(scanf("%d%c", &num1, &term1) != 2 || term1 != '\n'){

    printf("ERROR! You should type an integer e.g 5, 80, 100\n");

} else { ....
完整的代码是:

#include <stdio.h>

int main(){

int num1,num2;

char term1,term2;

printf("Type your first integer\n");

if(scanf("%d%c", &num1, &term1) != 2 || term1 != '\n'){

    printf("ERROR! You should type an integer e.g 5, 80, 100\n");

} else {

    printf("Type your second integer\n");

    if (scanf("%d%c", &num2, &term2) != 2 || term1 != '\n'){

        printf("ERROR! You should type an integer e.g 5, 80, 100\n");

    } 

    printf("\n%d", num1);
    for (int i=0; i<num1; i++) {
        printf(" *");
    }

    printf("\n");

    printf("%d", num2);
    for (int j=0; j<num2; j++) {
        printf(" *");
    }
}
}
练习是制作一个程序,请求2个整数值,并用这两个值制作一个水平条形图

谢谢。

scanf返回它成功收到的条款数量。你要求两个条件,所以你要比较结果,以确保两个条件都收到。然后检查字符项以确保它是回车符,这样您就知道用户在输入数字项后没有键入任何其他文本


我非常同意@Vlad关于阅读文档的评论。如果你在没有任何实际努力就可以自己解决的问题上发帖寻求帮助,那么当你接触到更复杂的事情时,你就注定要失败。

不要只扔掉一堆代码,而是做一些研究,提出一个简短而具体的问题。