Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-25
7
Medium Priority
?
3,019 Views
Last Modified: 2006-11-17

Hi,

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 ?
0
Comment
Question by:andyw27
7 Comments
 
LVL 5

Expert Comment

by:ddunlea
ID: 12674735
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12674881
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.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12675089
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.

Paul
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:arun80_inin
ID: 12678882
ddunlea had given the perfect answer.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12679727
>>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;

Paul
0
 
LVL 5

Accepted Solution

by:
ddunlea earned 750 total points
ID: 12679910
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.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12679930
Got me there! I am obviously having a dumb day today.

Paul
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question