Thread: why function ignores the parameter

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    93

    why function ignores the parameter

    I don't know why function ignores the parameter in my code and loop run 10 times. I expect it should be run 5 times only

    Code:
     #include<stdio.h>
    
    void foo ( int i)
    {
     for (i = 0; i < 10; i++)
     {
         printf("%d\n", i);
     }
    }
    
    
    int main(void)
    {   
        foo(5);
        return 0;
    }
    Code:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,112
    5 is passed but you are re-assigning the value with 0 in the for loop.

    Try this version:
    Code:
    #include<stdio.h>
    
    void foo ( int i)
    {
     for (; i < 10; i++)
     {
         printf("%d\n", i);
     }
    }
    
    
    int main(void)
    {
        foo(5);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    93
    Quote Originally Posted by rstanley View Post
    5 is passed but you are re-assigning the value with 0 in the for loop.
    Thanks for clearing doubts

    This is my code to print array

    Code:
    #include <stdio.h>
    
    int main (void)
    {
     int i, array[] = {1,2,3,4,5};
     
     for (i = 0; i < 5; i++)
     {
      printf("array element : %d  \n", array[i]);
     }
     return 0;
    }
    just now changed line inside the loop. I wonder why the line inside the loop printing the values form 0 to 4

    Code:
     #include <stdio.h>
    
    int main (void)
    {
     int i, array[5] = {1,2,3,4,5};
     
     for (i = 0; i < 5; i++)
     {
      printf("array element : %d  \n", array[5]);
     }
     return 0;
    }
    Code:
    array element : 0
    array element : 1
    array element : 2
    array element : 3
    array element : 4

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,112
    The subscripts of a 5 element array are 0 - 4, not 1 - 5!

    It is illegal to access any "element" outside of the array, so:
    Code:
    printf("array element : %d  \n", array[5]);
    Is illegal!!! You are accessing data outside of the array, and you are lucky in this case by actually accessing the loop variable 'i'!!!

    When I compile your second example on Linux with gcc version 12.2.0, it prints 5 zeros!

    Doctor, Doctor, it hurts when I access data outside of the array!

    Don't do it! ;^)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code ignores IF Statement
    By Neil Naik in forum C Programming
    Replies: 12
    Last Post: 07-02-2012, 07:57 PM
  2. do-while loop ignores break
    By edsoneicc in forum C Programming
    Replies: 8
    Last Post: 02-28-2012, 11:19 AM
  3. getchar ignores EOF
    By inliner in forum C Programming
    Replies: 9
    Last Post: 10-21-2010, 11:40 AM
  4. compiler ignores what i wrote
    By ashinms in forum C Programming
    Replies: 10
    Last Post: 05-07-2009, 02:30 PM
  5. Replies: 13
    Last Post: 08-24-2006, 12:22 AM

Tags for this Thread