-1

gcc is throwing warning :

log:- warning: right-hand operand of comma expression has no effect [-Wunused-value] for(i=4, data; i

FYI,data is of int type which we are passing to the function which has this forloop

I tried locally to with sample c code which is running fine. Not sure what i need to change in my orig code.

#include <stdio.h>
int main (){
int i;
int data;
for (i=4, data; i<10; ++i){
printf("i valuse is %d", i);
}

I Know "," will evaluate the expression from left to right and it will check last evaluated expression and proceed accordingly. o/p:- i valuse is 4 i valuse is 5 i valuse is 6 i valuse is 7 i valuse is 8 i valuse is 9

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Johnney
  • 127
  • 1
  • 9
  • So what does this code print? – alk Jan 08 '18 at 13:48
  • 1
    You're evaluating the non-initialized and not used variable `data`. That is what the compiler is warning you... That this has no effect on the code and you cand remove it. – kazbeel Jan 08 '18 at 13:48
  • 3
    `i = 10, data` evaluates `i = 10` and evaluates `data`. The result of evaluating `data` is discarded. What are you trying to do with the expression `i = 10, data`? *... and it will **check** last evaluated expression* not exactly. Not sure what you mean by "check" in this context. The *result* of `e1, e2, ..., en` is the value of `en`. But in a `for` loop, the *result* of the first expression isn't used. The `for` statement only counts on the side effect of the expression, which in this case is from `i = 10`. – lurker Jan 08 '18 at 13:48
  • What is it you think `, data` is supposed to accomplish? – Scott Hunter Jan 08 '18 at 13:48
  • 2
    Why do you do `i=10, data`? What is the reason for that? Perhaps you should [get a good beginners book or two](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and read? – Some programmer dude Jan 08 '18 at 13:48
  • @kazbeel, no, small snippet i tried(data is not assigned) which i mentioned above is running fine without any compilation issue. – Johnney Jan 08 '18 at 13:50
  • Of course it runs fine. Your `for` loop body doesn't use or need `data` at all, and you show no subsequent use of `data`. Which gets back to the question asked a couple of times now: what is your purpose in writing `i = 10, data` as the initial expression for the `for` loop? Just remove `, data`, then your warning will disappear and your code will behave exactly the same way. I don't even see a question here. What is your question? – lurker Jan 08 '18 at 13:51
  • Sorry, I apologize i value i assigned is '4' . for (i=4, data; i<10; ++i){ //code} o/p :- i valuse is 4 i valuse is 5 i valuse is 6 i valuse is 7 i valuse is 8 i valuse is 9 – Johnney Jan 08 '18 at 13:53
  • 1
    That code update (which you should apply by editing your question, not adding as a comment) doesn't change any of the above comments at all. You need to go back and read them carefully and understand them. – lurker Jan 08 '18 at 13:54

2 Answers2

1

It's inclusion is, in fact, potentially very harmful indeed.

data is an expression with value data and because it's the last expression in an outer expression exploiting the expression separator operator, that outer expression has the value data. Strictly speaking its presence means that the behaviour of your program is undefined since it's not initialised and conceptually at least, the compiler is free to generate code that reads the value.

Since it has no useful effect, so your helpful compiler is telling you that, and you should remove it.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

right-hand operand of comma expression has no effect

The warning is quite clear. The occurrence of the comma operator is in

  for (i=10, data; i<10; ++i)
           ^^^^^^

The variable data is unused, even if you remove from the "expression" part for the for loop. The best way is to completely get rid of that.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261