Thread: storing number to array

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

    storing number to array

    what is use of for loop after decleration of array
    Code:
    #include<stdio.h>int main (void)
    {
    	int i;
    	int array[4]= {2,2,3,4};
    	for(i=0; i<4; i++)
    	{
    	  printf("print array element : %d \n", array[i]);
    	}
     
        return 0;
    
    
    }
    Does it check the size of array or store the array element ?
    Last edited by vead; 02-07-2018 at 01:26 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    What happen when this program execute
    Code:
    #include<stdio.h>
    
    int main (void)
    {
    	int i;
    	int array[4]= {2,2,3,4};
    	for(i=0; i<4; i++)
    	{
    	  printf("print array element : %d \n", array[i]);
    	}
     
        return 0;
    }
    What happen when this program execute
    Code:
     #include<stdio.h>
    
    int main (void)
    {
    	int i;
    	int array[4]= {2,2,3,4};
        return 0;
    }
    I am confused on "for" loop. Why do we use "for" loop after the array decleration

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The for loop means you don't have to copy and paste 4 times.

    printf("print array element : %d \n", array[0]);
    printf("print array element : %d \n", array[1]);
    printf("print array element : %d \n", array[2]);
    printf("print array element : %d \n", array[3]);

    Which is even more important if you don't know how many you need until run time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    The for loop means you don't have to copy and paste 4 times.


    Which is even more important if you don't know how many you need until run time.
    So the 'for' loop is only used to print array number

    What happen when we write this line ​
    Code:
    intarray[4]= {2,2,3,4};
    does it mean's that the array storing four number's and if I want to print them , Then I have to write loop for it

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A for loop is used to repeat any sequence of steps.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    A for loop is used to repeat any sequence of steps.
    Okay but still unclear

    does it mean's that the array storing four number's and if I want to print them , Then I have to write loop for it

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, the array stores 4 numbers.
    How you print them is up to you, but a loop is one common way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing random number into array
    By alien1 in forum C Programming
    Replies: 2
    Last Post: 12-16-2014, 06:36 PM
  2. Storing a number in an array
    By C++ student in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2009, 08:47 AM
  3. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 08:28 AM
  4. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:23 AM
  5. Storing the whole number
    By flip114 in forum C Programming
    Replies: 3
    Last Post: 10-21-2003, 08:50 AM

Tags for this Thread