-2

I am bothered by a question of for loop in C language.

when i write:

#include<conio.h>
#include<stdio.h>
main()
{
    int i = 0;
    for( ; i ; )
    {
        printf("In for Loop");
    }
    getch();
}

Output: NOTHING PRINT.

Code get executed, but printf statement didn't print due to condition. OK, No problem here.

But when i write this code:

#include<conio.h>
#include<stdio.h>
main()
{
    for( ; 0 ; )
    {
        printf("In for Loop");
    }
    getch();
}

Output: In for Loop.

My for loop get executed 1 time, but actually it must not to be executed. I don't know why? can coder/programmer/hacker of stackoverflow help me. explain me please why my for loop giving this output only one time.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Mohd Iftekhar Qurashi
  • 2,385
  • 3
  • 32
  • 44
  • 1
    I cannot reproduce this behavior. Compile without flag with g++. – nhahtdh Aug 07 '12 at 14:47
  • nothing is printed in the second loop for me. Used Apple LLVM 3.1 – Andrew Aug 07 '12 at 14:47
  • How are you compiling your program? Also: Would be nice if you pasted the *entire* second version of the program, exactly as it is, instead of just the loop. – ArjunShankar Aug 07 '12 at 14:50
  • can't reproduce the problem on gcc and clang on linux. – Aftnix Aug 07 '12 at 14:52
  • I am compiling my program in turbo C++. – Mohd Iftekhar Qurashi Aug 07 '12 at 14:58
  • ok ArjunShankar. for you i am editing my code. – Mohd Iftekhar Qurashi Aug 07 '12 at 14:59
  • TurboC++? you should not use that, that's a arcane and out of date compiler. lot of standards came after the those borland compilers. – Aftnix Aug 07 '12 at 15:06
  • But this is a basic code aftnix. i need exact answer. if you had any better compiler they please compile my program and then give my answer. i am waiting for your reply. – Mohd Iftekhar Qurashi Aug 07 '12 at 15:09
  • @MohdIftekharQurashi - The code you pasted is not standard C. There is no such thing as `conio.h` in the C standard. It's a non-standard include part of Borland/Turbo and maybe some other old old compilers. Anyway, [here is the same program and its output](http://ideone.com/Mi4Q0) (after being compiled with GCC). You will see that there is no output. The website I linked to (ideone.com) allows you to write small programs and compile and run them online. – ArjunShankar Aug 07 '12 at 15:14
  • @MohdIftekharQurashi If the executable compiled by TurboC++ from the second code prints anything, TurboC++ is broken worse than anybody imagined. – Daniel Fischer Aug 07 '12 at 15:16
  • 2
    Possible duplicate: http://stackoverflow.com/questions/11086759/need-help-understanding-this-for-loop-code-in-c – tinman Aug 07 '12 at 15:18
  • **@Visitors from the future:** While this question has been closed as 'too localized', you really should look at @tinman's comment. This is a bug in Turbo C++. A comment on that linked question refers to the *exact same problem* as reported here. The real answer: Stop using Turbo C++ and other ancient/broken stuff. – ArjunShankar Aug 07 '12 at 17:02
  • Also: I think these down votes are unfair. At worst, this is a slightly-difficult-to-spot duplicate. – ArjunShankar Aug 07 '12 at 17:04
  • i think you are probably right. I should not use turbo c++. – Mohd Iftekhar Qurashi Aug 07 '12 at 19:03

3 Answers3

12

What you wrote shouldn't print anything, but I suspect the actual second-case code (not what you typed in) that causes the problem is:

for( ; 0 ; );     // <==== note the trailing semicolon there.
{
    printf("In for Loop");
}

In this case the for loop doesn't execute the empty statement, and then the { } code is executed once.

EDIT: If this isn't the problem please just paste a complete program that exhibits the problem directly into your question.

EDIT2:

The following minimal compilable example doesn't print anything:

#include <cstdio>

int main()
{
    for( ; 0 ; )
    {
        std::printf("In for Loop");
    }
}
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • 3
    Every programmer runs in this problem at least once in its lifetime – akappa Aug 07 '12 at 14:50
  • Yeah. I'd bet money on this one, not just because it would have the behaviour described, but also as @akappa says, everyone's done it at least once. (And if you find cases where empty `for` loops are useful, you'll do the opposite at least once too). – Jon Hanna Aug 07 '12 at 14:52
  • 1
    When I need an empty `for` loop I *still* use braces and a big fat comment saying that it's a null loop body. – Mark B Aug 07 '12 at 14:55
  • No Mark B brother. i didn't write code as you typed. i don't make any silly mistakes in coding. I run this code on different systems many times. all they give me same answer. you can run my code on your c compiler. you will get the same result. I didn't add any semicolon after printf statement. reply Mark B brother, i need your help. – Mohd Iftekhar Qurashi Aug 07 '12 at 14:55
  • 8
    @MohdIftekharQurashi You don't make any silly mistakes in coding? You'd be the only one in the whole world. – Daniel Fischer Aug 07 '12 at 14:56
  • +1 for @DanielFischer's comment :D – Kiril Kirov Aug 07 '12 at 14:57
  • Excellent psychic debugging, @MarkB! If you aren't "making silly mistakes coding" you probably aren't coding much. – n8wrl Aug 07 '12 at 15:01
  • 2
    [Link to runnable code example](http://codepad.org/9WLH6H5A) – JoeFish Aug 07 '12 at 15:04
  • @MarkB I am, of course, dumb and have deleted my incorrect comment :) – JoeFish Aug 07 '12 at 15:05
  • Funny @Mr Daniel Fscher. i like your comment. ha ha ha. – Mohd Iftekhar Qurashi Aug 07 '12 at 15:12
  • @MohdIftekharQurashi It's not funny. _Everybody_ makes silly mistakes sometimes. If you believe you don't, you're likely to overlook more than if you are aware that you sometimes do. The first rule of programming, "if things aren't doing what you expected, it's probably your own fault". – Daniel Fischer Aug 07 '12 at 15:18
  • I just mean that, i didn't make any silly mistakes in this type of short programs. for big problems, all programmer does silly mistakes. – Mohd Iftekhar Qurashi Aug 07 '12 at 15:20
  • Personally, I find I'm more likely to have a mistake in a 10 line program than in any given 100 lines of a real program. Still, we're all able to compile `for(;0;)` loops that skip the loop body. – Jon Hanna Aug 07 '12 at 15:32
  • Once compile this program in Turbo C++ buddy. The answer is surely surprising for you. – Mohd Iftekhar Qurashi Aug 07 '12 at 19:08
1

Both ways should output nothing.

noelicus
  • 14,468
  • 3
  • 92
  • 111
0

I can't replicate this behavior on my machine, compiling with gcc. The two programs were:

#include <iostream>
int main()
{
  for( ; 0 ; )
  {
    std::cout << "Here I am!";
  }
  std::cout << "End of the program.";
}

outputs

End of Program

as does

#include <iostream>
int main()
{
  int i = 0;
  for( ; i ; )
  {
    std::cout << "Here I am!";
  }
  std::cout << "End of Program";
}

This is what we would expect to happen, as the 0 read as the loop continuation condition is evaluated to false, so the loop is never entered.

Eosis
  • 548
  • 1
  • 4
  • 12