简体   繁体   中英

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.

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).

Move the declaration of 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. You have to put all your declarations ( month , day , year ) at the top of the main function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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