C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C

Array, Declaring Arrays and Array Initialization

What is an Array in C?

Array is a data structure provided by the C programming language. It is a data structure which is able and used to store a collection of data. In other words, it can be said that an array is a collection of data / variables of same type.

Instead of specifying separate or individual variables such as number1, number..., the user creates a single array variable such as numbers and uses numbers [0],], numbers [30, and..., numbers 97] to denote them.

To access a specific element in an array, an index is used.

Array1020304050
Location01234
The above array is declared as int q [5]; 
q [0] = 10; q [1] = 20; q [2] = 30; q [3] = 40; q [4] = 50;

 In the above figure 10,20,30,40,50 are actual data items and 0, 1, 2, 3, 4 are index variables.

Similarly,

If we declare an array as int w [10];
q [0] = 1; q [1] = 8; q [2] = 3; q [3] = 40; q [4] = 5;q [5] = 79; q [6] = 22; q [7] = 6; q [8] = 4; q [9] = 59;
Array18340579226459
Location0123456789

Array declarations in C

While Declaring Arrays in C, a programmer declares an array by specifying the type of elements and the number of elements required by the array as follows:

[arraySize] type arrayName;

A single-dimensional array is what this is called. arraySize must be a positive integer constant, and type can be any valid C data type. For example, to declare a 30-element double-type array called amount, use the following phrase:

[30] double amount

Amount is now a variable array that can hold up to 30 double numbers.

Another example is:

#include<stdio.h>
int main(){      
int i=0;    
int q[5];//declaration of array       
q[0]=10;//initialization of array    
q[1]=20;    
q[2]=30;    
q[3]=40;    
q[4]=50;    
//traversal of array    
for(i=0;i<5;i++){      
printf("%d \n",q[i]);    
}//end of for loop     
return 0;  
}
Array Declaring Arrays And Array Initialization
Array Declaring Arrays And Array Initialization

Array Initialization

In C, you can create an array one by one or with a single statement like this:

Double amount[5]={10.0,32.1,52.1,69.32,32.30}

The number of values between {} braces cannot exceed the number of members in the array declared between square brackets [].

If you don't specify the array size, a small array is generated to hold the initialization. As a result, if you write:

Double amount[]={10.0,32.1,52.1,69.32,32.30}

The array you build will be identical to the one you created in the previous example. Here's an example of how to declare an array and find the average of all of its elements:

#include <stdio.h>
int main()
{
    int average = 0;
    int sum =0;
    int p=0;


    /* Array- declaration – length 4*/
    int number[4];


    /* We are using a for loop to traverse through the array
     * While also storing the entered values in the array
     */
    for (p=0; p<4;p++)
    {
printf("Enter number %d \n", (p+1));
scanf("%d", &number[p]);
    }
    for (p=0; p<4;p++)
    {
        sum = sum+number[p];
    }


    average = sum/4;
printf("Average of entered number is: %d", average);
    return 0;
}
Array Declaring Arrays And Array Initialization
Array Declaring Arrays And Array Initialization
#include <stdio.h>
int main()
{
    int sum =0;
    int p=0;


    /* Array- declaration – length 5*/
    int numbers[4];


    /* We are using a for loop to traverse through the array
     * Whilealso storing the entered values in the array
     */
    for (p=0; p<4;p++)
    {
Printf("\n Enter number no. %d = ", (p+1));
scanf("%d", &numbers[p]);
    }
    for (p=0; p<4;p++)
    {
        sum = sum+numbers[p];
    }


printf("Sum of entered number is: %d", sum);
    return 0;
}
Array Declaring Arrays And Array Initialization
Array Declaring Arrays And Array Initialization



ADVERTISEMENT
ADVERTISEMENT