{"id":2600,"date":"2024-05-10T07:30:18","date_gmt":"2024-05-10T07:30:18","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2600"},"modified":"2024-05-10T07:30:18","modified_gmt":"2024-05-10T07:30:18","slug":"c-arrays","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/c-arrays\/","title":{"rendered":"C Arrays"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#c-arrays\">What is an Array in C?<\/a><\/li><li ><a href=\"#what-is-array-in-c\">What is Array in C?<\/a><\/li><li ><a href=\"#c-array-declaration\">C Array Declaration<\/a><\/li><li ><a href=\"#c-array-initialization\">C Array Initialization<\/a><\/li><li ><a href=\"#update-array-element\">Update Array Element<\/a><\/li><li ><a href=\"#c-array-traversal\">C Array Traversal<\/a><\/li><li ><a href=\"#how-to-use-array-in-c\">How to use Array in C?<\/a><\/li><li ><a href=\"#types-of-array\">Types Of Array<\/a><ul><li ><a href=\"#1-one-dimensional-array-in-c\">1. One Dimensional Array in C<\/a><\/li><li ><a href=\"#2-multidimensional-array-in-c\">2. Multidimensional Array in C<\/a><\/li><\/ul><\/li><li ><a href=\"#relationship-between-arrays-and-pointers\">Relationship between Arrays and Pointers<\/a><\/li><li ><a href=\"#passing-an-array-to-a-function-in-c\">Passing an Array to a Function in C<\/a><\/li><li ><a href=\"#return-an-array-from-function-in-c\">Return an Array from function In C<\/a><\/li><li ><a href=\"#properties-of-array-in-c\">Properties of Array in C<\/a><ul><li ><a href=\"#1-fixed-size\">1. Fixed Size<\/a><\/li><li ><a href=\"#2-homogenous-collection\">2. Homogenous Collection<\/a><\/li><li ><a href=\"#3-indexing-in-array\">3. Indexing In Array<\/a><\/li><li ><a href=\"#4-dimensions-of-an-array\">4. Dimensions of an Array<\/a><\/li><li ><a href=\"#5-contiguous-storage\">5. Contiguous Storage<\/a><\/li><li ><a href=\"#6-random-access\">6. Random Access<\/a><\/li><li ><a href=\"#7-no-index-out-of-bounds-checking\">7. No Index Out of Bounds Checking<\/a><\/li><\/ul><\/li><li ><a href=\"#examples-of-array-in-c\">Examples of Array in C<\/a><\/li><li ><a href=\"#example-2-c-program-to-print-the-average-of-the-given-list-of-numbers\">Example 2: C Program to print the average of the given list of numbers<\/a><\/li><li ><a href=\"#example-3-c-program-to-find-the-largest-number-in-the-array\">Example 3: C Program to find the largest number in the array<\/a><\/li><li ><a href=\"#advantages-of-array-in-c\">Advantages Of Array In C<\/a><\/li><li ><a href=\"#disadvantages-of-array-in-c\">Disadvantages of Array in C<\/a><\/li><li ><a href=\"#faq-c-arrays\">FAQ- C Arrays<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-arrays\">C Arrays<\/h2>\n\n\n\n<p>In C programming, arrays are like containers that help us store lots of things of the same type together. Imagine you have a bunch of numbers, characters, or other stuff you want to keep together neatly \u2013 arrays are your go-to tool. They&#8217;re simple to use, and in this guide, we&#8217;ll show you how to use them step by step. Whether you&#8217;re just starting out or you&#8217;re already a pro, you&#8217;ll find this introduction to C arrays helpful and easy to follow. Let&#8217;s get started<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-array-in-c\">What is Array in C?<\/h2>\n\n\n\n<p>An array in C is a collection of data items of the same type, stored in a fixed-size, consecutive block of memory. It&#8217;s versatile and can store various data types, including primitive types like int, char, and float, as well as more complex types like pointers and structures. Arrays are a fundamental data structure in C, enabling efficient storage and retrieval of data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-array-declaration\">C Array Declaration<\/h2>\n\n\n\n<p>In C, you need to declare an array just like any other variable before you can use it. When declaring an array, you specify its name, the data type of its elements, and the size of its dimensions. When the array is declared, the compiler allocates a block of memory of the specified size to hold the elements of the array. This allocation of memory allows you to store and manipulate data efficiently within the array.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data_type array_name &#91;size];\n         or\ndata_type array_name &#91;size1] &#91;size2]...&#91;sizeN];\n<\/code><\/pre>\n\n\n\n<p>N referred as the Number of dimensions<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to illustrate the array declaration\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ declaring array of integers\n    int arr_int&#91;5];\n    \/\/ declaring array of characters\n    char arr_char&#91;5];\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-array-initialization\">C Array Initialization<\/h2>\n\n\n\n<p>Initialization in C involves assigning initial values to variables. When an array is declared or memory is allocated for it, the elements of the array typically contain garbage values. To make the array useful and meaningful, you need to initialize it with specific values. There are multiple methods available for initializing arrays in C, allowing you to set the initial content to values that are relevant to your program&#8217;s logic and requirements.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Array Initialization with Declaration<\/h4>\n\n\n\n<p>In this method, you initialize the array when you declare it. You provide an initializer list, which is a collection of values enclosed within curly braces <code>{}<\/code> and separated by commas. These values are used to set the initial values of the array&#8217;s elements. This approach is convenient and efficient, especially when you know the exact values that your array should contain right from the start.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data_type array_name &#91;size] = {value1, value2, ... valueN};\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Array Initialization with Declaration without Size<\/h4>\n\n\n\n<p>When you initialize an array using an initializer list in C, you can omit specifying the size of the array explicitly because the compiler can automatically determine the size based on the number of elements in the initializer list. This feature simplifies the process of declaring and initializing arrays and ensures that you don&#8217;t have to manually count the elements to determine the array&#8217;s size. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data_type array_name&#91;] = {1,2,3,4,5};\n<\/code><\/pre>\n\n\n\n<p>The size of the above arrays is 5 which is automatically deduced by the compiler.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Array Initialization after Declaration (Using Loops)<\/h4>\n\n\n\n<p>This approach allows you to set specific values for each element of the array and is particularly useful when you need to calculate or retrieve values dynamically. You can use loops like <code>for<\/code>, <code>while<\/code>, or <code>do-while<\/code> to iterate through the array and assign values as needed.&#8217;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 0; i &lt; N; i++) {\n    array_name&#91;i] = valuei;\n}\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C Program to demonstrate array initialization\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ array initialization using initialier list\n    int arr&#91;5] = { 10, 20, 30, 40, 50 };\n \n    \/\/ array initialization using initializer list without\n    \/\/ specifying size\n    int arr1&#91;] = { 1, 2, 3, 4, 5 };\n \n    \/\/ array initialization using for loop\n    float arr2&#91;5];\n    for (int i = 0; i &lt; 5; i++) {\n        arr2&#91;i] = (float)i * 2.1;\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Access Array Elements<\/h4>\n\n\n\n<p>To access a specific element of an array, you use the array subscript operator <code>[]<\/code> followed by the index value of the element you want to access. The index value represents the position of the element in the array, and it starts from 0 for the first element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array_name &#91;index];\n<\/code><\/pre>\n\n\n\n<p>In C, array indexing always starts with 0. This means that the first element of an array is at index 0, and the last element is at index N &#8211; 1, where N is the total number of elements in the array. It&#8217;s a fundamental concept to keep in mind when working with arrays in C, as it ensures that you access the correct elements in the array and avoid off-by-one errors. For example, if you have an array with 5 elements, the indices of the elements will be 0, 1, 2, 3, and 4.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"example-of-accessing-array-elements-using-array-subscript-operator\">Example of Accessing &nbsp;Array Elements using Array Subscript Operator<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C Program to illustrate element access using array\n\/\/ subscript\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ array declaration and initialization\n    int arr&#91;5] = { 15, 25, 35, 45, 55 };\n \n    \/\/ accessing element at index 2 i.e 3rd element\n    printf(\"Element at arr&#91;2]: %d\\n\", arr&#91;2]);\n \n    \/\/ accessing element at index 4 i.e last element\n    printf(\"Element at arr&#91;4]: %d\\n\", arr&#91;4]);\n \n    \/\/ accessing element at index 0 i.e first element\n    printf(\"Element at arr&#91;0]: %d\", arr&#91;0]);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Element at arr&#91;2]: 35\nElement at arr&#91;4]: 55\nElement at arr&#91;0]: 15<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"update-array-element\">Update Array Element<\/h2>\n\n\n\n<p>To update the value of an element at a specific index in an array in C, you use the array subscript operator <code>[]<\/code> to access the element, and then you can use the assignment operator <code>=<\/code> to assign a new value to it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array_name&#91;i] = new_value;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-array-traversal\">C Array Traversal<\/h2>\n\n\n\n<p>Traversal is the process of visiting and inspecting each element of a data structure. In the context of C arrays, traversal involves going through each element of the array one by one. To accomplish this, you can use loops, such as <code>for<\/code>, <code>while<\/code>, or <code>do-while<\/code>, to iterate through the elements of the array and perform actions on them.<\/p>\n\n\n\n<p>Array Traversal using for Loop<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 0; i &lt; N; i++) {\n    array_name&#91;i];\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-array-in-c\">How to use Array in C?<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the use of array\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ array declaration and initialization\n    int arr&#91;5] = { 10, 20, 30, 40, 50 };\n \n    \/\/ modifying element at index 2\n    arr&#91;2] = 100;\n \n    \/\/ traversing array using for loop\n  printf(\"Elements in Array: \");\n    for (int i = 0; i &lt; 5; i++) {\n        printf(\"%d \", arr&#91;i]);\n    }\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Elements in Array: 10 20 100 40 50 \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-array\">Types Of Array<\/h2>\n\n\n\n<p>There are 2 types of arrays based on the dimension<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>One Dimensional Arrays (1D Array)<\/li>\n\n\n\n<li>Multidimensional Arrays<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-one-dimensional-array-in-c\">1. One Dimensional Array in C<\/h3>\n\n\n\n<p>One-dimensional arrays in C, often referred to as 1-D arrays, are arrays that have only one dimension. These arrays store elements in a single line or row, and each element can be accessed using a single index. One-dimensional arrays are the most common type of arrays in C and are used to store collections of elements of the same data type in a linear sequence.<\/p>\n\n\n\n<p>For example, an integer array of size 5 is a one-dimensional array that can hold five integer values in a linear fashion, and you can access each element using indices like array[0], array[1], array[2], array[3], and array[4].<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array_name &#91;size];\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C Program to illustrate the use of 1D array\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ 1d array declaration\n    int arr&#91;5];\n \n    \/\/ 1d array initialization using for loop\n    for (int i = 0; i &lt; 5; i++) {\n        arr&#91;i] = i * i - 2 * i + 1;\n    }\n \n    printf(\"Elements of Array: \");\n    \/\/ printing 1d array by traversing using for loop\n    for (int i = 0; i &lt; 5; i++) {\n        printf(\"%d \", arr&#91;i]);\n    }\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Elements of Array: 1 0 1 4 9 \n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"array-of-characters-strings\">Array of Characters (Strings)<\/h4>\n\n\n\n<p>In C, strings are just words or sentences made up of characters. We store these strings as arrays of characters, where each character is like a letter in a row. At the end of the string, we put a special character called NULL to say, &#8220;This is the end.&#8221; So, in C, strings are really just arrays of characters with a special marker at the end.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C Program to illustrate strings\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ creating array of character\n    char arr&#91;6] = { 'S', 'k', 'i', 'l', 'l', '\\0' };\n \n    \/\/ printing string\n    int i = 0;\n    while (arr&#91;i]) {\n        printf(\"%c\", arr&#91;i++]);\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skill<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-multidimensional-array-in-c\">2. Multidimensional Array in C<\/h3>\n\n\n\n<p>Multi-dimensional arrays in C are arrays that have more than one dimension. Two of the most commonly used multi-dimensional arrays are 2D arrays and 3D arrays. While it&#8217;s possible to declare arrays with more than three dimensions, they tend to become very complex and can occupy a significant amount of memory. As a result, such high-dimensional arrays are often avoided in practice due to their complexity and the resources they require.<\/p>\n\n\n\n<p><strong>A. Two-Dimensional Array in C<\/strong><\/p>\n\n\n\n<p>A two-dimensional array, often referred to as a 2D array in C, is an array that has precisely two dimensions. These arrays can be thought of as a grid or table, where data is organized in rows and columns on a two-dimensional plane. This structure is useful for representing data that naturally fits into a grid-like format, such as matrices, tables, or images.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array_name&#91;size1] &#91;size2];\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to illustrate 2d array\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ declaring and initializing 2d array\n    int arr&#91;2]&#91;3] = { 10, 20, 30, 40, 50, 60 };\n \n  printf(\"2D Array:\\n\");\n    \/\/ printing 2d array\n    for (int i = 0; i &lt; 2; i++) {\n        for (int j = 0; j &lt; 3; j++) {\n            printf(\"%d \",arr&#91;i]&#91;j]);\n        }\n        printf(\"\\n\");\n    }\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2D Array:\n10 20 30 \n40 50 60<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">B. Three-Dimensional Array in C<\/h4>\n\n\n\n<p>A three-dimensional array, commonly known as a 3D array in C, consists of exactly three dimensions. You can imagine it as a collection of 2D arrays stacked on top of each other, creating a third dimension. This structure is valuable for representing data that has three levels of organization or can be thought of as layers within layers. It&#8217;s especially useful in applications like 3D graphics, where you&#8217;re dealing with data that extends in three directions.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array_name &#91;size1] &#91;size2] &#91;size3];\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to illustrate the 3d array\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ 3D array declaration\n    int arr&#91;2]&#91;2]&#91;2] = { 10, 20, 30, 40, 50, 60 };\n \n    \/\/ printing elements\n    for (int i = 0; i &lt; 2; i++) {\n        for (int j = 0; j &lt; 2; j++) {\n            for (int k = 0; k &lt; 2; k++) {\n                printf(\"%d \", arr&#91;i]&#91;j]&#91;k]);\n            }\n            printf(\"\\n\");\n        }\n        printf(\"\\n \\n\");\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 20 \n30 40 \n\n \n50 60 \n0 0 <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"relationship-between-arrays-and-pointers\">Relationship between Arrays and Pointers<\/h2>\n\n\n\n<p>The array name effectively acts as a constant pointer to the first element of the array. You can use this pointer to access and manipulate array elements.<\/p>\n\n\n\n<p>When you pass an array as an argument to a function, it &#8220;decays&#8221; into a pointer to its first element. This means that the function receives a pointer to the array&#8217;s data, not a full copy of the array. This can be more efficient for working with large arrays.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C Program to demonstrate the relation between arrays and\n\/\/ pointers\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    int arr&#91;5] = { 10, 20, 30, 40, 50 };\n    int* ptr = &amp;arr&#91;0];\n \n    \/\/ comparing address of first element and address stored\n    \/\/ inside array name\n    printf(\"Address Stored in Array name: %p\\nAddress of \"\n           \"1st Array Element: %p\\n\",\n           arr, &amp;arr&#91;0]);\n \n    \/\/ printing array elements using pointers\n    printf(\"Array elements using pointer: \");\n    for (int i = 0; i &lt; 5; i++) {\n        printf(\"%d \", *ptr++);\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Address Stored in Array name: 0x7ffce72c2660\nAddress of 1st Array Element: 0x7ffce72c2660\nArray elements using pointer: 10 20 30 40 50 \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"passing-an-array-to-a-function-in-c\">Passing an Array to a Function in C<\/h2>\n\n\n\n<p>In C, when you pass an array to a function, it indeed &#8220;decays&#8221; into a pointer to its first element. This means that the function receives a pointer to the starting memory location of the array, not a full copy of the array. This behavior is why you often see function parameters declared as pointers when they are meant to accept arrays as arguments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C Program to pass an array to a function\n#include &lt;stdio.h&gt;\n \nvoid printArray(int arr&#91;])\n{\n    printf(\"Size of Array in Functions: %d\\n\", sizeof(arr));\n    printf(\"Array Elements: \");\n    for (int i = 0; i &lt; 5; i++) {\n        printf(\"%d \",arr&#91;i]);\n    }\n}\n \n\/\/ driver code\nint main()\n{\n \n    int arr&#91;5] = { 10, 20, 30, 40, 50 };\n \n    printf(\"Size of Array in main(): %d\\n\", sizeof(arr));\n    printArray(arr);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size of Array in main(): 20\nSize of Array in Functions: 8\nArray Elements: 10 20 30 40 50<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"return-an-array-from-function-in-c\">Return an Array from function In C<\/h2>\n\n\n\n<p>In C, a function can indeed return only a single value. However, when you need to return multiple values or elements, you can use pointers effectively. One common approach is to return an array from a function by returning a pointer to its first element. This allows you to effectively pass back a collection of values to the calling code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to return array from a function\n#include &lt;stdio.h&gt;\n \n\/\/ function\nint* func()\n{\n    static int arr&#91;5] = { 1, 2, 3, 4, 5 };\n \n    return arr;\n}\n \n\/\/ driver code\nint main()\n{\n \n    int* ptr = func();\n \n    printf(\"Array Elements: \");\n    for (int i = 0; i &lt; 5; i++) {\n        printf(\"%d \", *ptr++);\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"properties-of-array-in-c\">Properties of Array in C<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-fixed-size\">1. Fixed Size<\/h3>\n\n\n\n<p>In C, arrays are fixed in size and must have their size known at compile time. Once declared, you can&#8217;t change their size during program execution. Arrays are allocated statically, meaning the memory for the entire array is reserved from the start.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-homogenous-collection\">2. Homogenous Collection<\/h3>\n\n\n\n<p>Arrays can only store elements of a single data type. While you can have as many elements as needed, all elements within an array must be of the same data type. This constraint ensures that arrays are homogeneous collections, providing consistent and efficient storage for a specific type of data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-indexing-in-array\">3. Indexing In Array<\/h3>\n\n\n\n<p>In C, the array index always starts from 0. This means that the index of the first element in an array is 0, and the index of the last element is one less than the total number of elements in the array, which is typically represented as N &#8211; 1. Understanding this indexing convention is crucial for correctly accessing and manipulating elements in C arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dimensions-of-an-array\">4. Dimensions of an Array<\/h3>\n\n\n\n<p>In an array, a dimension represents the number of indexes needed to access an individual element within the array. It&#8217;s essentially the number of directions in which you can extend or traverse the array. For example, in a one-dimensional array, you need only one index to access elements. In a two-dimensional array, you require two indices (row and column) to pinpoint an element. Each dimension adds an extra level of organization and access to the array&#8217;s elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-contiguous-storage\">5. Contiguous Storage<\/h3>\n\n\n\n<p>In C, all elements in an array are stored sequentially and continuously in memory. This contiguous storage is a fundamental property of arrays, and it enables efficient random access to individual elements within the array. Because elements are stored one after another, the memory locations for each element can be easily calculated, allowing for direct access using indices. This property makes arrays well-suited for tasks that require quick and direct access to elements, such as searching or sorting algorithms.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-random-access\">6. Random Access<\/h3>\n\n\n\n<p>Arrays in C indeed provide random access to their elements. This means that you can access any element within an array directly by using its index, and the time it takes to access an element is constant, or O(1), complexity. In other words, regardless of the array&#8217;s size, you can access any element with the same efficiency, making arrays an efficient data structure for tasks that require quick and direct access to elements by their positions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-no-index-out-of-bounds-checking\">7. No Index Out of Bounds Checking<\/h3>\n\n\n\n<p>In C and C++, there is no built-in index out-of-bounds checking for arrays. The compilers do not automatically check whether you&#8217;re accessing an element beyond the valid range of the array. While this allows for more efficient code, it also means that if you access an element outside the array&#8217;s bounds, it can lead to undefined behavior, which may manifest as unexpected output, crashes, or memory corruption.<\/p>\n\n\n\n<p>Developers need to be careful when working with arrays in C\/C++ to ensure that they do not access elements outside the defined range of the array to prevent such issues. It&#8217;s a responsibility placed on the programmer to write code that respects the array&#8217;s boundaries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ This C program compiles fine\n\/\/ as index out of bound\n\/\/ is not checked in C.\n \n#include &lt;stdio.h&gt;\n \nint main()\n{\n    int arr&#91;2];\n \n    printf(\"%d \", arr&#91;3]);\n    printf(\"%d \", arr&#91;-2]);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>211343841 4195777 \n<\/code><\/pre>\n\n\n\n<p>Example- The program given below-compiles fine and shows just a Warning<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\nint main()\n{\n \n    \/\/ Array declaration by initializing it \n    \/\/ with more elements than specified size.\n    int arr&#91;2] = { 10, 20, 30, 40, 50 };\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>prog.c: In function 'main':\nprog.c:7:25: warning: excess elements in array initializer\n  int arr&#91;2] = { 10, 20, 30, 40, 50 };\n                         ^\nprog.c:7:25: note: (near initialization for 'arr')\nprog.c:7:29: warning: excess elements in array initializer\n  int arr&#91;2] = { 10, 20, 30, 40, 50 };\n                             ^\nprog.c:7:29: note: (near initialization for 'arr')\nprog.c:7:33: warning: excess elements in array initializer\n  int arr&#91;2] = { 10, 20, 30, 40, 50 };\n                                 ^\nprog.c:7:33: note: (near initialization for 'arr')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"examples-of-array-in-c\">Examples of Array in C<\/h2>\n\n\n\n<p>We will use scanf() and print() functions in order to get the input and print output for the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to perform input and output on array\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ declaring an integer array\n    int arr&#91;5];\n \n    \/\/ taking input to array elements one by one\n    for (int i = 0; i &lt; 5; i++) {\n        scanf(\"%d\", &amp;arr&#91;i]);\n    }\n \n    \/\/ printing array elements\n    printf(\"Array Elements: \");\n    for (int i = 0; i &lt; 5; i++) {\n        printf(\"%d \", arr&#91;i]);\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Input<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5 7 9 1 4\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array Elements: 5 7 9 1 4\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-c-program-to-print-the-average-of-the-given-list-of-numbers\">Example 2: C Program to print the average of the given list of numbers<\/h2>\n\n\n\n<p>To calculate the average of numbers stored in an array through this program<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to the average to two numbers\n#include &lt;stdio.h&gt;\n \n\/\/ function to calculate average of the function\nfloat getAverage(float* arr, int size)\n{\n \n    int sum = 0;\n    \/\/ calculating cumulative sum of all the array elements\n    for (int i = 0; i &lt; size; i++) {\n        sum += arr&#91;i];\n    }\n \n    \/\/ returning average\n    return sum \/ size;\n}\n \n\/\/ driver code\nint main()\n{\n \n    \/\/ declaring and initializing array\n    float arr&#91;5] = { 10, 20, 30, 40, 50 };\n    \/\/ size of array using sizeof operator\n    int n = sizeof(arr) \/ sizeof(float);\n \/\/ printing array elements\n    printf(\"Array Elements: \");\n    for (int i = 0; i &lt; n; i++) {\n        printf(\"%.0f \", arr&#91;i]);\n    }\n \n    \/\/ calling getAverage function and printing average\n    printf(\"\\nAverage: %.2f\", getAverage(arr, n));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array Elements: 10 20 30 40 50 \nAverage: 30.00<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-3-c-program-to-find-the-largest-number-in-the-array\">Example 3: C Program to find the largest number in the array<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to find the largest number in the array.\n#include &lt;stdio.h&gt;\n \n\/\/ function to return max value\nint getMax(int* arr, int size)\n{\n    int max = arr&#91;0];\n    for (int i = 1; i &lt; size; i++) {\n        if (max &lt; arr&#91;i]) {\n            max = arr&#91;i];\n        }\n    }\n    return max;\n}\n \n\/\/ Driver code\nint main()\n{\n \n    int arr&#91;10]\n        = { 135, 165, 1, 16, 511, 65, 654, 654, 169, 4 };\n \n    printf(\"Largest Number in the Array: %d\",\n           getMax(arr, 10));\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Largest Number in the Array: 654\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-of-array-in-c\">Advantages Of Array In C<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Random and Fast Access<\/strong>: Arrays allow for quick and direct access to elements using the array index. This property makes it efficient to access any element by its position within the array.<\/li>\n\n\n\n<li><strong>Concise Code<\/strong>: Arrays enable the storage of multiple elements using just one data structure, leading to more compact and efficient code compared to using individual variables for each element.<\/li>\n\n\n\n<li><strong>Simplified Traversal<\/strong>: You can traverse through all elements of an array conveniently using a single loop, simplifying tasks that involve processing or examining each element.<\/li>\n\n\n\n<li><strong>Ease of Sorting<\/strong>: Arrays make sorting operations more straightforward. You can implement sorting algorithms with fewer lines of code because of the uniform structure of arrays, which makes comparisons and swaps more manageable.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"disadvantages-of-array-in-c\">Disadvantages of Array in C<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Fixed Size<\/strong>: Arrays in C have a fixed size, which means you must decide and declare the number of elements you need at compile time. This lack of dynamic sizing can be a limitation when you need to work with variable amounts of data.<\/li>\n\n\n\n<li><strong>Not Dynamic<\/strong>: Unlike data structures like linked lists, arrays are not dynamic, meaning their size cannot be changed during program execution. Once an array is declared, its size remains constant.<\/li>\n\n\n\n<li><strong>Costly Insertion and Deletion<\/strong>: Inserting or deleting elements in the middle of an array can be costly in terms of time and memory because it often requires shifting elements to accommodate the change. This can lead to performance overhead in certain situations.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-c-arrays\">FAQ- C Arrays<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1696934350622\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What is an array in C with example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. To create an array in C, specify the data type (like <code>int<\/code>) and give it a name followed by square brackets <code>[]<\/code>. You can insert values into it using curly braces <code>{}<\/code> with a comma-separated list. For instance, <code>int myNumbers[] = {25, 50, 75, 100};<\/code> creates an array of four integers with the specified values.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696934359546\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is 1 array in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A one-dimensional array in C is like a list where each element holds a single value, whether it&#8217;s an <code>int<\/code>, <code>char<\/code>, <code>float<\/code>, or another data type. To declare a one-dimensional array, you usually need to specify three things: the data type, the array name, and the number of elements that the array can contain. This combination defines the array&#8217;s type and size, allowing you to work with lists of values efficiently.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696934369045\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the syntax of an array?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. For 1D arrays: <code>int arr[n];<\/code><br \/>For 2D arrays: <code>int arr[m][n];<\/code><br \/>In these syntax examples, <code>int<\/code> represents the data type, <code>arr<\/code> is the name of the array, and <code>n<\/code> and <code>m<\/code> are the sizes or dimensions of the array. This syntax is used to declare and define arrays in C, specifying their data type and size<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>C Arrays In C programming, arrays are like containers that help us store lots of things of the same type together. Imagine you have a bunch of numbers, characters, or other stuff you want to keep together neatly \u2013 arrays are your go-to tool. They&#8217;re simple to use, and in this guide, we&#8217;ll show you &#8230; <a title=\"C Arrays\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/c-arrays\/\" aria-label=\"More on C Arrays\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5362,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[438],"class_list":["post-2600","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-arrays","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2600"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2600\/revisions"}],"predecessor-version":[{"id":10661,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2600\/revisions\/10661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5362"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}