Thread: Print the array element error in program

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    Print the array element error in program

    I was trying to write program where I want to print array element

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

    hello.c:8:24: error: expected ';' before ')' token
    for (i = 0, i < 5; i++)
    ^
    hello.c:11:2: error: expected ';' before '}' token
    }

    if I use array [5]
    then
    Print element of array : 0
    Print element of array : 1
    Print element of array : 2
    Print element of array : 3
    Print element of array : 4

    I don't understand why there is array [5] only ?
    Last edited by vead; 01-04-2018 at 11:16 PM. Reason: if i put array [i] get error but when i put array [5] I don't see error

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your program has syntax errors, and your compiler is trying to tell you about the right syntax it was expecting. Look between your for loop parentheses and make sure it is written with semi-colons separating the three terms. Make sure that all your lines end with a semi-colon.

    Edited to add: Changing array[i] to array[5] does not address the syntax errors that are still in this version of the program. Perhaps you changed other things, but no, doing that did not address the problems. Look at my compile of your latest code for proof of that:
    Code:
    C:\Users\jk\Desktop>gcc -c vead.c
    vead.c: In function 'main':
    vead.c:6:24: error: expected ';' before ')' token
      for (i = 0, i < 5; i++)
                            ^
    vead.c:9:2: error: expected ';' before '}' token
      }
      ^
    
    C:\Users\jk\Desktop>more vead.c
    #include <stdio.h>
    int main (void)
    {
     unsigned i, array[5] = {1,2,3,4,5};
    
     for (i = 0, i < 5; i++)
     {
      printf(" Print array element : %d  \n", array[5])
     }
    
    
     return 0;
    }
    With errors like these, no executable is made, so there is nothing to run.
    Last edited by whiteflags; 01-04-2018 at 11:25 PM.

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    5
    I am not sure if I totally understand your question, but you have tons of error as whiteflags said.

    Here is a working code:

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

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Here is a working code:
    Yea, not even close.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by jimblumberg View Post
    Yea, not even close.
    It's not a bad effort (LOL!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insert element after the last element in array in c
    By amaturequestion in forum C Programming
    Replies: 3
    Last Post: 04-09-2015, 08:29 AM
  2. Replies: 2
    Last Post: 08-25-2011, 08:30 AM
  3. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  4. how to print/access the 1st element of the first set of the vector
    By kapil1089thekin in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2010, 02:28 PM
  5. get the largest element from the array and print it
    By casperghost in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 06:22 PM

Tags for this Thread