• C

Warning: ..... is assigned a value that is never used in function main


When I compile my code in Borland I always get warning messages like this :

<variable> is assigned a value that is never used in function main

Is there anyway I can get rid of these ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi andyw27,

Delete the variable. You get this error when you have something like this:

int main(){
    int a = 0;
    int b = 1;

    return b;

a is assigned but no program code depends on it and so it is essentially pointless, hence the warning.
Jaime OlivaresSoftware ArchitectCommented:
It is not an error, compiler is just advicing you that you have a variable declared and assigned to a value, apparently without any utility, but sometimes this warning appears when you "comment" or delete a portion of code that exclusively uses this variable, so, you have to analyze if variable is useful or not.
In many Microsioft compilers I've used:

#pragma unused ...

but these are usually to indicate parameters that are unused in the code rather than variables.

Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

ddunlea had given the perfect answer.
>>Delete the variable. You get this error when you have something like this:
>>int main(){
>>    int a = 0;
>>    int b = 1;

Actually, that code wont show the problem because assigning a value to the variable is considered as 'use'.

It most often shows with stuff like:

int i = 0, j = 0, k;

where 'k' isnt used in the function.

The alternative solution is to initialise the variable.

int i = 0, j = 0, k = 0;

Hi PaulCaswell,

Please re-read the question. It said:

<variable> *is* *assigned* *a* *value* that is never used in function main

This kind of warning is definitely generated by C# compilers, and by more pedantic c compilers. If a non-volatile variable is assigned, but never used for anything else, then the compiler can easily spot this and issue a warning. Especially if the variable is a local variable in main and not a global/etern variable.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Got me there! I am obviously having a dumb day today.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.