-1

I am following the book Let us C and the following code has been shown as being perfectly correct:

for ( i < 4 ; j = 5 ; j = 0 ) 
    printf ( "%d", i ) ; 

But in the Turbo C it gives 3 warnings:

Code has no effect. Possibly incorrect assignment. 'j' is assigned a value that is never used.

kartikeykant18
  • 1,737
  • 6
  • 28
  • 42

5 Answers5

2

If the book is making the point that this code is allowed by the C standard, then it is correct. This code does not violate any rule of the C standard, provided that i and j have previously been declared correctly (and printf too, by including #include <stdio.h>).

However, nobody would actually write code like this, because it is not useful. That is why the compiler is issuing a warning, because the code is technically allowed but is probably not what a programmer would intend.

If the book is claiming that this code is useful in some way, then it is probably a typographical error. It is certainly wrong. If the book has more than a few errors like this, you should discard it.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

I don't know what your book want to teach you with this example, but AFAIK a for loop should always be in the form

for ( init; check; next ) {
    /* do something */
}

where init initialize what you're going to use, check check if it should stop or continue and next perform some kind of action. It is the same as

init;
while ( check ) {
    /* do something */
    next;
}

Therefore you are getting the warning because:

  • Code has no effect is referred to i < 4. As you can see in the while form, this comparison isn't used in any way, therefore it has no effect.
  • Possibly incorrect assignment. is refereed to j = 5 cause you're making a check of an assignment witch will always evaluate to the value assigned (in this case 5)
  • 'j' is assigned a value that is never used as it says, 'j' is never used, as you print the 'i' in this example.

Probably what the book wants to do is for ( i = 5; i < 5; i++ ). And probably what you need to do is using a better book.

Andrea Carron
  • 1,001
  • 13
  • 22
0

It is valid C code but it's pretty much meaningless. This will not initialize the loop properly and trigger an infinite loop. Loops look something like

for (i = 0; i < 10; i++)

The first statement is the initializer, the second stipulates the end case, and the last is the increment. I would get rid of that book

sedavidw
  • 11,116
  • 13
  • 61
  • 95
  • ... And what about the warnings OP asked about? - That is the Q – VoidPointer Jul 17 '13 at 13:55
  • I was trying to lead him to that by pointing out that his loop while syntactically correct, is probably not going to give the behavior the author intended. Where a loop similar the one shown above will is a much more standard use of loops – sedavidw Jul 17 '13 at 14:02
0

Check this out.

int i=0;
for(i=0;i<5;i++)
{
    printf("%d",i);
}
Jasmin Mistry
  • 1,459
  • 1
  • 18
  • 23
0

This is a correct but infinite loop, the correct way to instantiate a for loop is

int i ;
for(i = 0; i< [variable or number];i++){
printf("%d",i);
}

the code you wrote is meaningless and you can't do anything with that code, actually it print the value of i infinite time because it never change. The only thing we know about i is less then 4. Probably the output is always the same number.

Lucarnosky
  • 514
  • 4
  • 18