在以下代码中找不到错误:- 在 try() 之前预期未限定 id

Can't find the error in following code :- expected unqualified-id before try()

本文关键字:id try 代码 找不到 错误      更新时间:2023-10-16
#include<stdio.h>
#include<string.h>
void try(char s[])
{
    if(strlen(s)>5)
    {
        puts("Errorn");
    }
}
int main()
{
    char string[10];
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",&string);
        try(string);
    }
    return 0;
}

仍然找不到错误...try 是一个简单的函数,我一如既往地创建一个 func 并调用它。编译器给出错误 -(在"try"之前应为非限定 ID)

我怀疑您正在尝试将代码编译为C++而不是C。在C++中,try是一个保留字(用于异常处理)。

$ gcc test.c
$ g++ test.c
test.c:3:6: error: expected unqualified-id before 'try'

您可以使用-x显式设置语言(使用 gccg++):

$ gcc -x c test.c
$ gcc -x c++ test.c
test.c:3:6: error: expected unqualified-id before 'try'

尝试将-x c添加到调用或将文件重命名为 main.c 。可能是gcc出于某种原因选择将您的 C 文件编译为 C++。