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

This is a discussion on What happens if you put ';' after for(...)? within the C Programming forums, part of the General Programming Boards category; Look at this code Code: for (a=0; a<10; a++) { printf ("%d",matrix1[a]); } Code: for (a=0; a<10; a++); { printf ...

  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 04: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
    22,271
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    23,012
    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
    Posts
    23,012
    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
    Posts
    23,012
    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 05: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 05:18 AM.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    printf ("Cell &#37;d is %d",i, matrix1[i]);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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?

Page 1 of 2 12 LastLast
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, 04:21 AM
  2. How to put it into an array
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 10-10-2005, 02:19 PM
  3. Combining user inputs to put into System()
    By TDMedia in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2005, 01:00 AM
  4. cant figure out where to put the loop....
    By seal in forum C Programming
    Replies: 2
    Last Post: 08-30-2005, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21