简体   繁体   English

获得“未声明的标识符”错误? 为什么?

[英]Getting an “undeclared identifier” error? why?

I am getting the following errors: 我收到以下错误:

error C2143: syntax error : missing ';' before 'type'
error C2065: 'month' : undeclared identifier
error C2065: 'day' : undeclared identifier
error C2065: 'year' : undeclared identifier
error C2065: 'month' : undeclared identifier
error C2065: 'day' : undeclared identifier
error C2065: 'year' : undeclared identifier

I am running Visual Studio. 我正在运行Visual Studio。

And here is my code: 这是我的代码:

#include <stdio.h>
#include <string.h>

main()
{
    char middle;
    char first[30], last[30];
    printf("WHat us ur midint?");
    printf("\n");
    scanf(" %c", &middle);
    printf("\n");
    printf("WHat us ur name?");
    printf("\n");
    scanf(" %s %s", first, last);
    printf("ur name is %s %c %s\n\n", first, middle, last);
    printf("\n");
    int month, day, year;
    scanf(" %d/%d/%d", &month, &day, &year);
    printf("Birthdate: %d/%d/%d\n\n", month, day, year);
}

Does anyone know why these errors are happening? 有谁知道为什么会发生这些错误?

When compiling a C program, MSVC doesn't allow declarations to follow statements in a block (it uses old C90 rules - support for declarations mixed with statements was added to C in the 1999 standard). 在编译C程序时,MSVC不允许声明遵循块中的语句(它使用旧的C90规则 - 在1999标准中将对语句混合的声明的支持添加到C中)。

Move the declaration of int month, day, year; 移动int month, day, year;的声明int month, day, year; to the top of your program: 到你的程序的顶部:

char middle;
char first[30], last[30];
int month, day, year;

...

Visual Studio 2010 does not support c99 and c99 mixed declarations and statements. Visual Studio 2010不支持c99和c99混合声明和语句。 You have to put all your declarations ( month , day , year ) at the top of the main function. 您必须将所有声明( monthdayyear )放在main函数的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM