关于if语句:尝试使用C编程,在Y类型错误之前获得期望的x

Trying programming with C, Getting Expected x before Y type error

尝试使用C(自学和使用《新波士顿指南》),但尝试简单地问这些问题,然后如果答案匹配,则打印X或Y。

我已经注释掉yourAge,因为我在做什么,我不需要它,如果您删除if语句并仅将其打印为Hi,firstName,则它可以工作,我可以看到您是yourAge等。

我遇到的错误是:

||=== Build: Debug in TestProject (compiler: GNU GCC Compiler) ===|
C:\\Users\\admin-jb\\Desktop\\TestCProject\\TestProject\\main.c||In function 'main':|
C:\\Users\\admin-jb\\Desktop\\TestCProject\\TestProject\\main.c|21|warning: implicit declaration of function 'If' [-Wimplicit-function-declaration]|
C:\\Users\\admin-jb\\Desktop\\TestCProject\\TestProject\\main.c|22|error: expected ';' before '{' token|
C:\\Users\\admin-jb\\Desktop\\TestCProject\\TestProject\\main.c|33|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

使用代码::块13.12

有什么想法吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char firstName[20];
//    int  yourAge;
    char excelLike[20];


    printf("Please Enter Your Name?\
"
);
    scanf("%s", firstName);

//    printf("How old are you?\
");

//    scanf("%d", &yourAge);

    printf("Do you like Excel?\
"
);
    scanf("%s", excelLike);

    If(strcmp(excelLike,"yes") == 0)
        {
        printf("Hey, %s I love Excel too!!\
"
, firstName);
        }
    else
        {
        printf("Hey %s, pfft nor do I, its rubbish!\
"
, firstName);
        }
    return 0;
}


1
C:\\Users\\admin-jb\\Desktop\\TestCProject\\TestProject\\main.c|21|warning: implicit declaration of function 'If' [-Wimplicit-function-declaration]|

您使用的是if而不是if。 C区分大小写,这意味着您必须注意大小写字母


C关键字区分大小写。使用if代替if

相关:根据C11标准第6.4.1章,关键字,

The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise.....

根据您的用法,编译器无法将关键字ifif匹配,并且不能将if视为函数调用,这在您的情况下未定义。因此,您会收到错误消息。

说,

  • 对于main(),推荐的签名是int main(void)
  • 您应将输入字符串的长度限制为scanf(),以避免可能的缓冲区溢出。您应该使用like

    1
    scanf("%19s", firstName);
  • 始终检查scanf()的返回值是否成功。