5

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.

5 Answers 5

7

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

Sign up to request clarification or add additional context in comments.

4 Comments

VS2010 silently "broadens" the concept of a valid C program
@mbaitoff: VS2010 doesn't have the concept of a C program. It only compiles C++ programs. You need MinGW if you still want to compile C programs on Windows.
@Quandary: You can compile C Programs from VS 2010 command prompt. cl hello.c will give you hello.exe
@mbaitof: Not true; VC++ supports C compilation, and will use C compilation automatically for source files with a .c extension. Just because the IDE does not offer .c as one of the preselected choices does not mean you cannot just create one. However VC++ does not support ISO C99, and Microsoft have no plans to do so. It does however support elements of C99 that are also valid C++, so I believe that there may be an option to allow this, but it would be as easy to simply use C++ compilation.
3

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 ; ... } 

Comments

0

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.

Comments

0

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?

Comments

0

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

1 Comment

In fact they need only be declared at the beginning of a statement block.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.