Thread: What happens if you put ';' after for(...)?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32

    What happens if you put ';' after for(...)?

    Look at this code

    Code:
    	  for (a=0; a<10; a++)
    	  {
    		  printf ("%d",matrix1[a]);
    	  }
    Code:
    	  for (a=0; a<10; a++);
    	  {
    		  printf ("%d",matrix1[a]);
    	  }
    For the 1st code i get 10 zeros which are the matrix contents.

    For the 2ond code I dont get an error but a "4006960"

    In what is the for function transformed if you put a ";" in the end?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you do that the loop body will be an empty statement (made up of no code). a will be incremented till it is 10 and then you'll access matrix1[10] which might be out of bounds.

    It is the equivalent of
    Code:
        for (a=0; a<10; a++)
        {
            ; //or nothing here
        }
        printf ("%d",matrix1[a]);
    Last edited by anon; 09-14-2008 at 03:05 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    for (a=0; a<10; a++);
    {
        printf("&#37;d", matrix1[a]);
    }
    Is equivalent to:
    Code:
    for (a=0; a<10; a++)
    {
        /* empty statement */
    }
    
    {
        printf("%d", matrix1[a]);
    }
    You get a random value printed because a is initialised to garbage, and thus matrix1[a] likely accesses the array out of bounds, so you got some garbage at the given location.

    EDIT:
    Oops, yes, a is set to 10, not initialised to garbage... but then that matrix1[10] is out of bounds applies.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    yeah I understand, since the last cell of matrix[10] is 9 not 10

    Thanks

    P.S.
    These questions might be silly for troubling an entire forum but unlike C++, this time in C I dont want to leave holes of knowledge. When many of them gatehred, then when you're creating complex programms you get errors like this and you say "why"

    My Blog. Personal Notes and experiences in the learning of C

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    Eh guys this crashes! The programm runs but when I insert a value it says programm has to shut down!
    Code:
          int a=0;
          int i;
          float b=3;
          int matrix1 [10]={0};
          for (i=0; i<10; i++)
          {
              printf ("&#37;d",matrix1[i]);
          }
                for (i=0; i<10; i++)
          {
              printf ("\nGive value for cell %d\n",i);
              scanf ("%d",matrix1[i]);
          }
                for (i=0; i<10; i++)
          {
              printf ("Cell %d",i,"is%d",matrix1[i]);
          }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well yeah, you're not passing the address of matrix1[i] to scanf, but instead its value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    Of course! I have to put just the matrix name!

    P.S.
    This complex form of printf is illegal?
    Code:
    printf ("Cell &#37;d",i,"is%d",matrix1[i]);

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Unfortunately it is a very common mistake to put ; after for (by accident). It took me once several hours to find out what was the problem :/

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You realize there are debuggers, right? Should be very easy to spot with them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    My bloodshed i have a feeling that doesnt shows warnings, just errors.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George M. View Post
    Of course! I have to put just the matrix name!

    P.S.
    This complex form of printf is illegal?
    Code:
    printf ("Cell %d",i,"is%d",matrix1[i]);
    Actually, that is perfectly legal printf statement - the only problem is that the format string is not matching the arguments, as you have one integer format, and you provide one integer, a string and another integer argument.

    Obviously, it also doesn't do what I think you expect it to do. It would print only the first integer argument. To print multiple values, you'd make the first string contain all the formats, and then put all the arguments after that. That means, move the "i" to after the format string, and merge the two format portions to one string.

    --
    Mats
    Last edited by matsp; 09-14-2008 at 04:08 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    Yes it shows only the first number

    I want the i to appear between those 2 texts not both i and matrix toghether

    But you confused me...

    Would be easier for me if you wrote the correct expression. Easier for you too I think
    Last edited by George M.; 09-14-2008 at 04:18 AM.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    printf ("Cell &#37;d is %d",i, matrix1[i]);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    oh......... so it appears where th "&#37;" ares (how are they called?)

    So thats no a symbol to decalarate end of text but to declarate placing of a variable there wright?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where do i put this?
    By hero_bash in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2006, 03:21 AM
  2. How to put it into an array
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 10-10-2005, 01:19 PM
  3. Combining user inputs to put into System()
    By TDMedia in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2005, 12:00 AM
  4. cant figure out where to put the loop....
    By seal in forum C Programming
    Replies: 2
    Last Post: 08-30-2005, 10:10 AM
  5. What /what not to put in header files
    By Just in forum C Programming
    Replies: 1
    Last Post: 12-14-2002, 10:45 AM