Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 667
Programmer Avatar asked Jan 23 '11 16:01

Programmer


People also ask

How do you solve undeclared identifier?

To solve it simply declare i outside the loop so that the whole program inside the main function can use it. LIBRARY NOT INCLUDED: If we try to use a data type such as vector without including its library we will get this error. To fix this, make sure that you are using an identifier only after including its library.

What does the error message undeclared identifier mean?

The identifier is undeclaredIf the identifier is a variable or a function name, you must declare it before it can be used. A function declaration must also include the types of its parameters before the function can be used.

Is an undeclared variable a runtime error?

Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using var or const keyword. If we use 'typeof' operator to get the value of an undeclared variable, we will face the runtime error with return value as “undefined”.

What is undeclared identifier error in C++?

The identifier is undeclared: In any programming language, all variables have to be declared before they are used. If you try to use the name of a such that hasn't been declared yet, an “undeclared identifier” compile-error will occur.


2 Answers

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

like image 106
Pointy Avatar answered Nov 04 '22 02:11

Pointy


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 ;

    ...
}
like image 36
Clifford Avatar answered Nov 04 '22 02:11

Clifford