Reputation: 498
Following is the C Code :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a,b,d;
void Values1()
{
a=a*2;
b=b-5;
}
void Values2(register int c)
{
d+=b-c;
a=a+10;
b*=3;
}
int a,b;
int main()
{
int i=7;
Values2(i);
Values1();
printf("a=%d, b=%d, d=%d\n",a,b,d);
system("pause");
}
It Gives a Compilation Error related to redefinition of 'a' and 'b' in MS Visual C++ 2010. Please help me in figuring out why this error has occured.
Upvotes: 0
Views: 146
Reputation: 882386
Why do you have these two lines?
int a,b,d;
int a,b;
You only need to define the variables once. I'd suggest getting rid of the second line.
Some other things you may want to consider in the interests of furthering your skills.
d += b - c;
instead of d+=b-c;
.register
keyword, it's a suggestion to the compiler and most modern compilers are far better than developers at figuring out how best to optimise code.int main()
isn't one of them - use int main (void)
if you don't care about program arguments.main
(C99 will return 0 for you if you reach the closing brace of main
).Upvotes: 3
Reputation: 108988
Your compiler is badly configured. Your program is legal C.
The re-declarations of a
and b
fall into the category of "tentative definition"
. A properly configured C compiler, cannot stop compilation because of that.
Upvotes: 3
Reputation: 803
Right after the definition of Values2()
, there is the line int a,b;
which will conflict with int a,b,d;
before Values1()
, so you can safely remove the line int a,b;
.
Besides, to get good results, you may want to set tha values of a
, b
and c
in the start of the main function to get consistent results.
Upvotes: 0
Reputation: 75659
You define a and b in the global scope here:
int a,b,d;
and here
int a,b;
You can not define variables twice.
Upvotes: 4