Programmer
Programmer

Reputation: 6753

Why do I get "error undeclared identifier" unless I declare my variable at the beginning?

When I have following:

#include "stdafx.h"
#include<stdio.h>
int main()
{

int val1,val2;

printf("Enter the first value");
scanf("%d",val1);
scanf("%d",&val2);
int c;
c=val1 + val2;
printf(" the value is : %d", c);



return 0; // 0 means no error

 }

I get error undeclared identifier c. Also, syntax error. missing ; before type.

However, if I change above to following error disappears. Please help

#include "stdafx.h"
#include<stdio.h>

int main()
{

int val1,val2,c;

printf("Enter the first value");
scanf("%d",&val1);
scanf("%d",&val2);

c=val1 + val2;
printf(" the value is : %d", c);



return 0; // 0 means no error

}

I am running C in VS 2010.

Upvotes: 5

Views: 62855

Answers (5)

Clifford
Clifford

Reputation: 93476

Objects may only be declared at the top of a statement block in ISO C90. You can therefore do this:

#include<stdio.h>

int main()
{
    int val1,val2;

    printf("Enter the first value");
    scanf("%d",val1);
    scanf("%d",&val2);

    // New statement block
    {
        int c;
        c=val1 + val2;
        printf(" the value is : %d", c);
    }

    return 0; // 0 means no error
 }

Though it would perhaps be unusual to do so. Contrary to somewhat popular belief, the start of a function is not the only place you can declare an automatic variable. It is more common, rather than creating a dummy block, to use existing statement blocks introduced as part of an if or for construct for example.

It is useful to enclose case blocks in { ... }, even though not normally necessary, so that you can introduce temporary case specific variables:

switch( x )
{
    case SOMETHING :
    {
        int case_local = 0 ;
    }
    break ;

    ...
}

Upvotes: 3

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272507

In C90, local variables must all be declared at the beginning of a function block.

Upvotes: 0

Joe Cullity
Joe Cullity

Reputation: 528

One other observation. scanf() wants the ADDRESS of the destination, not it's value.

in the top example you are omitting the & in scanf("%d",val1); . In the bottom example it is included scanf("%d",&val1);

"val1" vs "&val1"

Shouldn't change the problem with the variable 'c', but probably causing a syntax error somewhere?

Upvotes: 0

Christoph
Christoph

Reputation: 169593

Microsoft decided against supporting newer revisions of the C language, so you can't mix code and declarations. With MSVC, you're basically stuck with C90, although some selected features (eg long long, restrict) are supported.

My recommendation would be to either switch to C++ or use a different compiler like the MinGW edition of GCC.

Upvotes: 0

Pointy
Pointy

Reputation: 413717

In C, at least back in the old days, variable declarations have to come at the top of the block. C++ is different in that regard.

edit — apparently C99 is different from C90 in this respect (C99 being essentially the same as C++ on this issue).

Upvotes: 7

Related Questions