Loop declaration in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gnanapoongothai
    New Member
    • Jun 2007
    • 62

    Loop declaration in C

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

    Is this kind of definition inside for loop is allowed in c standard.
  • kreagan
    New Member
    • Aug 2007
    • 153

    #2
    Originally posted by gnanapoongothai
    for (int i =0;i<0;i++)

    Is this kind of definition inside for loop is allowed in c standard.
    no, you cannot create a variable while assigning it a value in C. You can do that for C++ thought

    it should be

    [CODE=c]int i = 0;

    for (i = 0; i < number; i++)[/CODE]

    Also, your loop will always terminate from the first call because i is always greater than 0;

    Comment

    • saranjegan
      New Member
      • Feb 2007
      • 50

      #3
      Originally posted by kreagan
      no, you cannot create a variable while assigning it a value in C. You can do that for C++ thought

      it should be

      [CODE=c]int i = 0;

      for (i = 0; i < number; i++)[/CODE]

      Also, your loop will always terminate from the first call because i is always greater than 0;
      Yup you can create variable like this inside for loop , but your loop has no meaning !!?

      Comment

      • gnanapoongothai
        New Member
        • Jun 2007
        • 62

        #4
        somebody give a clear idea if u can or cannot

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by gnanapoongothai
          somebody give a clear idea if u can or cannot
          I think you need to re-read the thread, your question has been answered.

          I also changed the thread title as per the Posting Guidelines to describe your issue.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by saranjegan
            Yup you can create variable like this inside for loop , but your loop has no meaning !!?
            This is not true. Note this is a question about C. You can only create a variable in a for loop like that in C++.

            Comment

            Working...