ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

One-Dimensional Arrays in C Programming Language with Examples

Updated on September 29, 2014
Source

What is an Array in C?

In the programming language C, an array is a collection of values of a given data type. Arrays can be created from any of the C data types, such as "int," "float," and "char." Arrays can only hold data of their designated type: for example, an integer array can only hold integer values.

How does an array differ from a variable? When we declare array, it allocates a contiguous memory location for storing values, whereas two or three variables of same data-type can be given random locations. Arrays store data under a single variable name and index.

There are different types of arrays:

  1. One-dimensional array (Also known as 1D array).
  2. Two-dimensional array (Also known as 2D array).
  3. Multi-dimensional array.

This tutorial discusses the one-dimensional array.

Declaration of One Dimensional Arrays:

To declare a one-dimensional array in C, use the following syntax: data_type array_name[width];

Example: Declare an array that can store the roll numbers of eight students in a class, for the purpose of taking roll.

Answer: int roll[8];

In our example:

  • int specifies the type if the variable. In this example, an integer;
  • roll specifies the given name of the variable;
  • [8] is the value;
  • The brackets ([ ]) tell the compiler that what follows is an array, and the number in the brackets specifies how many elements (values in any array are called elements) it can store. This number is also the size of array.

In this example we have declared an integer array named“roll” that can store the roll numbers of eight students.

You can see the memory arrangement of the above declared array in the following image:

How a 1D array is stored in memory. You can access any value in the array based on the index.
How a 1D array is stored in memory. You can access any value in the array based on the index.

C Array Assignment and Initialization

We can assign values to arrays just as we assign them to variables. We can initialize an array (assign its initial values) just was we initialize a variable. Value can be assigned to an array at the time of declaration or during runtime. Let’s look at each approach.

The syntax for assigning value to an array is: data_type array_name[size]={list of values};

Examples:
int arr[5]={1,2,3,4,5};
int arr[]={1,2,3,4,5};

Both these examples are valid ways of declaring an integer array named “arr,” stating that it can hold five elements, and initializing it by listing the values it holds. The first example mentions the size of the array—[5]—and assigns the values to the elements by putting the values in curly brackets, separating each element’s value with a comma.

In the second example we left the “size” field blank, but we provided values for five elements. When we only give element values without specifying the size of an array, then the C compiler automatically assumes an array size based on the number of element values given (in this case, five).

Initializing an Array by Assigning Individual Variables

There is one more way to initialize an array C programming. In this method we can assign values to each individual element of an array. See the example below.

Syntax:

#include<stdio.h>
#include<conio.h>

void main()
{
int arr[5],i;
clrscr();
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;

printf("Value in array arr[0] : %d\n",arr[0]);
printf("Value in array arr[1] : %d\n",arr[1]);
printf("Value in array arr[2] : %d\n",arr[2]);
printf("Value in array arr[3] : %d\n",arr[3]);
printf("Value in array arr[4] : %d\n",arr[4]);
printf("\n");

for(i=0;i<5;i++)
{
	printf("Value in array arr[%d] : %d\n",i,arr[i]);
}
getch();
}

Print:

This print-out shows you how the initial values are stored in this array in C.
This print-out shows you how the initial values are stored in this array in C.

In this example, we have assigned individual values to the integer array, just as we would to an integer variable. We called the value of each array element individually, and used a for loop operation so it would be clear for beginner C programmers.

This example shows we can assign individual values to array elements, and call the values whenever we need them.

Your opinion required

This tutorial helped me to improve my C Array understanding.

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      umeed 2 years ago

      Plzzz help me!

      How to write a progam that shows 3 no. of rows ,two having lists of integers with size [9] and the third shows their sum by the help of array?????????

    • profile image

      improzee 2 years ago

      please give me a sample of this program.

      A software company has requested you to implement a Computer Information System that will keep up to a maximum of 50 Computer data. The computer’s information to be kept is:

      • ComputerID – int

      • ComputerName – 50 characters

      • ProcessorSpeed – integer

      • RAM - Integer

      • OperatingSystem – 50 characters

      • Price - float

      You would need to create a Computer structure and an array called Computers. You are requested to test the Computer Information System developed by performing the tasks b) to e), and display its output.

      a) Declare the Computer structure and the Computers array, using the information given above.

      b) Insert 5 new computers using a sequential ComputerID

      c) Search for a particular computer by ComputerID

      d) Search for a particular computer by ComputerName

      e) Display a list of all computers

    • profile image

      cokemaster 2 years ago

      great hub...

    • profile image

      manisha 3 years ago

      i want user se 10 no. enter kraye then user hi unme se koi no. search kare ki wo no. jo user ne enter kiye unme h ya nhi agar h toh print ho "element found" agar nhi toh print ho "element no found"

    • profile image

      annabelle_carza@yahoo.com 3 years ago

      hi! hello! mam ,sir, ahm i need some example in arrays in how to run a programm..

    • profile image

      deepanshu 4 years ago

      good information

    • profile image

      mikzz 5 years ago

      int array [9][9];

      assigning values from 1 to 9

      magic square..

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

    • profile image

      saiba 5 years ago

      hi

      write one more programme for multi dimentional array..

    • profile image

      ricardo 5 years ago

      hi...i had to put it this way to get the results:

      main (void)

      and

      remove the clrscr();

      to get it worked in Dev c++...

      nevertheless...it was of great help :D

      thanks

    • profile image

      suraweera 5 years ago

      please, help me to write C program to display index number & name of 5 students by using array based list.

    • profile image

      ronnie 5 years ago

      please help me solve this question...

      using arrays write a simple program to display numbers 1-20.

    • profile image

      سووووووووسو 5 years ago

      consider the following java statement double [] A1 = new double [7]; int A2 [],A3; A2 ={2,4,10);

      1) how many arrays in the previous code?name them

      2)what is the data type of A3?

      3)draw the memory address for A1,A2,A3.

      4)what is the output of the follwing code?

    • profile image

      hhh...d 5 years ago

      write a program read the integer 10 element into an array and display it in reverse order?

    • profile image

      ridhima 5 years ago

      plzz tell me a c++ program to multiply a number x upto n terms...????

    • profile image

      indu 5 years ago

      plzz tell me how to proiduce nested loop??

    • Java Programs profile image

      Java Programs 5 years ago from India

      one of the best article i have seen for c programming dealing with arrays ..... keep the good work up ..

    • mkeith profile image

      mkeith 5 years ago from United States

      Your "Memory Arrangement" in your diagram is likely wrong. Computers have been running a 32-bit os since the mid to late 90's. If you are programming on a windows machine then an 'int' variable type gets 4 bytes of data, not 2. If you are programming for some other type of processor that only has a 16 bit integer (word size) then this is fine, but you need to declare this as it might confuse others.

    • profile image

      don 5 years ago

      how to reverse a printout of an array?

    • profile image

      Emmanuel 5 years ago

      Hello,

      I need someone to help me solve this questions (C programming).

      You have a two-dimensional array [ 4 ] [5 ].

      a) make a program that calculates the sum of the elements.

      b) make a program that finds the entered value from the array and prints out ”success” if found and ”empty” if not found.

      c) change the exercise b so that it prints out the place from where the program found the entered value .

      Example of initializing a two-dimensional array:

      int array [ 4 ] [5 ] =

      { { -2, 5, -7, 2, 8},

      { 80, 32, 4, 5, 6},

      { -1, 0, 4, 82, 6},

      { 3, -5, 4, 7, 8}

      };

      Send the solution to my email address: emma4ever01@yahoo.ca

    • profile image

      sam 5 years ago

      how to print an element a[3] of array a[10] if values has been entered

    • profile image

      babegirl15 5 years ago

      can you help me with this?

      Assigning values to array through input operation.

      like this.

      content of array

      locaion value

      o 1000

      1 0

      2 0

      3 0

      4 0

      Datay entry using array

      Enter location: 0

      Enter value:

      Accept another value?

      please email me the answer

      ruthalfonso32@yahoo.com

    • profile image

      sruthi 5 years ago

      write a programme for multi dimentional array...........

    • profile image

      sonali ananthre 5 years ago

      please help me to sort

    • profile image

      Shalini 5 years ago

      Write the code for the below, the input which we give looks like this.

      enter the number of element

      2

      enter the number of materials

      1

      enter the material details

      1

      E1,E2,E3,G12,G13,G23,v12,v13,v23,rho

      details of material 1

      thickness angle material

      details of material 2

      thickness angle material

    • profile image

      ishaq 5 years ago

      please make a program which show yout id is vaild or not

    • profile image

      pooja.i 5 years ago

      write a c program to entering a 3 nos and print 3 nos using array

    • profile image

      ash 5 years ago

      how to write up a program that will store five random integer numbers in an array and find the highest and lowest number stored and display the highest and lowest value in c++ programming

    • profile image

      momai 5 years ago

      ...kindly help me to do programming about phonebook using the array program.....

    • profile image

      Saroar Hossain 5 years ago

      Searching one dimensional array:

      Write a program that takes 10 values from the user, stores it in an array and allows the user to look for a value in that array (after the array is created, ask the user for the value they want to search for in the array, write a searching algorithm which looks for that value in the array and if found tells the user about its location [index no.] if not found informs the user that no matching results were found)

      Matrix multiplication using

      I am really very confused. Can you please help me solving this problem? Please

    • profile image

      lemiey 5 years ago

      my question is : how to write a program that can read 10 integer values into an array and find the total for that array in the program,write 3 function:a function to read value, a function to find the total of element values and a function to print value.

    • profile image

      rajib 5 years ago

      plz write a program to find 1st and 2nd largest no using array

    • profile image

      Ana 5 years ago

      please help me

      write a complete program that will ask 10 integer numbers using an array. the program will display the values in revers order.

      thanks

    • profile image

      lavanya 5 years ago

      pls any one send this program answer ,Twenty five numbers are entered through the keyboard into an arry.Write a program to find out how many of them are positive,how many are negative,how many are even and how many are odd.

    • profile image

      alvina 5 years ago

      can any one help me now? i need a program which can read 25 integer values n print it in ascndng ordr

    • profile image

      janu 5 years ago

      how a print a character in a array "multiple character"

    • profile image

      lavanya 5 years ago

      write a program to get marks of 5 different subjects for 10 students and print the same.

    • profile image

      ziaah 5 years ago

      Write a c++ program?

      a.declare an integer array of 5?

    • profile image

      ahmad gill 5 years ago

      who can find solution???????

      #include

      #include

      void mtrx_sort(int ary[][3]); //function prototype

      main()

      { clrscr();

      int x,y=2;

      int ary[3][3];

      cout

    • profile image

      lee 5 years ago

      please help me how to fill matrix arr[i][j] with elements which i could later round to nearest integer.!!

    • profile image

      abhi 5 years ago

      i m facing problem to WAP in c language using Array.2d,multi d.plz give me good idea to understood

    • profile image

      Keya Mondal 5 years ago

      hi, my email id softprog.program5@gmail.com

      please help me about stack

    • profile image

      Jaiyaishri 5 years ago

      good example.it's eassy to lurn & understand.

    • profile image

      kalaiselvi 5 years ago

      i want this program multipulation of array, pls send my mail id my mail id is kalaithangaraj1@gmail.com

    • profile image

      jyoti 5 years ago

      plz give me some program in character an array

    • profile image

      shonah 5 years ago

      pls help a sister out

      Write a C program that displays an array of 10 different double numbers. The numbers should be values that you assign to the array by hard-coding them in the main().

      The numbers should be displayed using a function that you write called DisplayNumbers.

      Write another function called ReverseArrayNumbers that:

      creates a second array of the same size as the first one

      copies the numbers from the first array into the new array in reverse order.

      Display the numbers in the new array by using the DisplayNumbers ...

      email it to: shonahj@gmail.com

      Thank you

    • profile image

      elkan 5 years ago

      Pls help me

      Write a C program that displays an array of 10 different double numbers. The numbers should be values that you assign to the array by hard-coding them in the main().

      The numbers should be displayed using a function that you write called DisplayNumbers.

      Write another function called ReverseArrayNumbers that:

      creates a second array of the same size as the first one

      copies the numbers from the first array into the new array in reverse order.

      Display the numbers in the new array by using the DisplayNumbers ...

      email it to: etandreas@gmail.com

      Thank you

    • profile image

      pooja 5 years ago

      i like thankyou so much

    • profile image

      kavitha 5 years ago

      i want answer this question please.

      write a program to copy the contents of one array into another array in the reverse order.please answer to the question

    • profile image

      deepak vaishnav 5 years ago

      nice programming and any another program,

    • profile image

      vishal gurhale 5 years ago

      i want detail in array

      like as

      how initialize of array.

      and how to work array in program

    • snkhan120 profile image

      snkhan120 5 years ago

      array is a heart of programming

    • profile image

      nikhil 5 years ago

      how to make a programe to find out the greatest element in an array in c languae !!

    • profile image

      michelle 5 years ago

      Ma'am/Sir i need your help. please help me regarding my assignment, I want to know more about programming.here's the formula:

      char a[11]="Boring";

      printf("Index 0 has %c\n",a[0];

      printf("Index 1 has %c\n",a[1];

      printf("index 2 has %c\n",a[3];

      printf("Index 3 has %c\n",a[4];

      printf("Index 4 has %c\n",a[5];

      printf("Index 5 has %c\n",a[6];

      printf("Index 6 has %c\n",a[7];

      printf("Numerically,the a[6] is %d\n",a[6]);

      Question:

      a. How many slots/indexes does this array have?

      b. How many characters, counting the null character at the end,does this array hold?

      c. The number of an index is also called an index. What is the lowest index?

      d. What is the highest index for this array?

      e. Is the number for the highest index the same as that for the number of indexes?

      f. What is stored in the last index as a character,that is, in an index number 5?

      g. What is stored in the last index numerically? All strings should have this null character stored in its last index to signal the end of the string.

      I hope you can help me regarding my problem. This is my email id: michelle_sph2001@yahoo.com

    • profile image

      julie 5 years ago

      good

    • profile image

      mamatha.k 5 years ago

      plese try to explian with some more examples

    • profile image

      anne 5 years ago

      thank u so much,,its such a great help!!

    • profile image

      Bikram anand ,barari 5 years ago

      Sir, this tutorial is nice but i have requested to should add some more eaisy example with output.thanx bikram1023@yahoo.com

    • profile image

      ritesh 5 years ago

      in 1-D array i want access the all array elements to the other functions too other functions and i want to doing actually quick sort so i have change index of array all times. im trying so much but not getng output. so plz tell me as soon as fast possible. i need it.

      my emailid : riteshpatel1989@gmail.com

    • profile image

      bulbul 5 years ago

      sir\madam actually i need ur help to do the programing of multidimensional array.The programme is to evaluate each student total value and final total value.

      plz help me how can i do this program by using which logic

      My emailid is: mail2blogspot@gmail.com

    • profile image

      prashant kedar 5 years ago

      i like

    • profile image

      enzoh_22 6 years ago

      write a program containing a function that will compute and display the answer of various statistical measurement like a. combination and permutation b. mean, median, mode.

      e-mail to lorenz_florendo@yahoo.com thnks

    • profile image

      miles 6 years ago

      Email me the answer please...

      fabulousmiles_13@yahoo.com...

    • profile image

      miles 6 years ago

      hi can u create a program that will read 10 characters(char) and store in an array.And display the count of each character being stored (please see the sample illustration below).

      Sample Illustration:

      char A[5];

      Assume:

      A[0]='y'

      A[1]='o'

      A[2]='l'

      A[3]='l'

      A[4]='y'

      The counter:

      y=2

      o=1

      l=2

    • profile image

      christiyal jansi rani 6 years ago

      Respected sir/Madam! I am christi! plz one help doupt for you sir! array initialazation reading writing program ! Its very urgent! plz Condect My ID Name was anbuchristi@ymail.com

    • profile image

      vinodh 6 years ago

      int a[]={0,1,2,3,4}

      i want to display all this in increment way.. tell me the short and simple way

    • profile image

      kaushik 6 years ago

      i need more tutorial session suggest us more

    • profile image

      prakash bannadabhavi 6 years ago

      #include

      using namespace std;

      int main()

      {

      int x;

      int y;

      int array[8][8]; // Declares an array like a chessboard

      for ( x = 0; x < 8; x++ ) {

      for ( y = 0; y < 8; y++ )

      array[x][y] = x * y; // Set each element to a value

      }

      cout

    • profile image

      madbones 6 years ago

      harigatou gozaimasu!

    • profile image

      an 6 years ago

      these has ben a good help to me! thanks to you! im having a hard time understanding all the cides and terms in our programming subject! our prof gave us an assign. in which we will create a program ussing array!!!!!can u help me? i need it asap thankssssss

    • profile image

      LeAnn 6 years ago

      I really need some help!

      i have to:

      1. create an array of 7 integers

      2. ask user to give the values of its elements and fill the array

      3. find the minimum of values in the array and print it

      please help!

      email it to me @ leann_marie06@yahoo.com

      it is due in 6 hours. I am trying my best to do this, but i'm having a lot of difficulties.

      THANKS SO MUCH! :)

    • profile image

      zeke 6 years ago

      could u create a program which will input 15 car names

      and will get the choice,quantity,payment and change of the costumer..please e-mail it to me: ausa_jay@yahoo.com

      thank you & god bless...

    • profile image

      jhesmer 6 years ago

      plzz create a program that will input 10 numbers and will output the 10 numbers from least to greatest

      example input: example output:

      9 0

      0 1

      8 2

      1 3

      7 4

      2 5

      6 6

      3 7

      5 8

      4 9

      plzz do e-mail it to me: jherobe_zae@yahoo.com

      a big thankzz to everyone!!

    • profile image

      anne khandelwal 7 years ago

      how to write a program to generate prime numbers from 1to 100??with explanation..plz help me..

    • profile image

      alia 7 years ago

      how to write programs using array???can u give some examples

    • profile image

      nataraju 7 years ago

      char a[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3};

      char a1[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3};

      char a2[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3}

      char a3[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3}

      char a4[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3};

      char a5[]={a,a1,a2,a3,a4};

      a5 is correct or not

      how to store a to a4 in to another array

    • profile image

      R.Dhnanalakshmi 7 years ago

      I Have some information in 'c'program,but more detail in array concept please sir.

      Thank Sir

    • profile image

      nicomp 7 years ago

      An array dimensioned as [5] only has elements 0 through 4. It has no element [5]. Your code is incorrect.

    • profile image

      Josh 7 years ago

      You mention only 1-dimensional arrays, which are quite basic and simple to understand.

      Rather, I think that most users would be looking for help concerning 2-dimensional and multi-dimensional arrays, as these are the ones that are a lot harder to wrap your mind around.