Basics of Arrays: C Program

Lets look at basics of arrays in C programming language. We’ve already covered a lot of stuffs about arrays in Introduction To Arrays: C Programming Language. In this video tutorial we’ll look at some specific things about arrays which we use often.

Related Read:
For Loop In C Programming Language
Sizeof Operator in C Programming Language

Declaring Array Variable

Syntax:

Data_type variable_name[array_size];

Ex: int a[5];

Here array variable is a, it can hold upto 5 integer values.

Index of array starts from zero, and it ends at N-1. N being the size of the array.

For Ex:
int a[N];
Here the first element of array a is at a[0] and the last element is at a[N-1].

Definition of Array

An array is a collection of data items, of same data type, accessed using a common name.

Video Tutorial: Basics of Arrays: C Program


[youtube https://www.youtube.com/watch?v=cwMN7ZKiEuc]

YouTube Link: https://www.youtube.com/watch?v=cwMN7ZKiEuc [Watch the Video In Full Screen.]

Source Code: Basics of Arrays: C Program

Printing all the elements of an array

#include<stdio.h>

int main()
{
    int a[5] = {1, 2, 3, 4, 5}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
1
2
3
4
5

This prints all the elements of an array. In this program we’re declaring and initializing array variable simultaneously.

Empty curly braces for Array Variable

#include<stdio.h>

int main()
{
    int a[5] = {}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
0
0
0
0
0

Compiler will insert zero in all the empty spots.

Un-initialized Array Variable

#include<stdio.h>

int main()
{
    int a[5], i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
6356864
4200750
4200656
46
8

If array variable is left un-initialized it’ll have garbage values inside it.

Array Variable Not fully initialized Manually

#include<stdio.h>

int main()
{
    int a[5] = {3, 2}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
3
2
0
0
0

Whenever we assign less values than the array size, the remaining elements will get assigned to 0.

Expression As Array Size

#include<stdio.h>

int main()
{
    int a[2+3] = {3, 2, 1, 0, 5}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
3
2
1
0
5

Any valid expression which ultimately resolves to a positive integer is valid inside square brackets.

Negative Integer Number As Array Size

#include<stdio.h>

int main()
{
    int a[-5] = {3, 2, 1, 0, 5}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
error: size of array ‘a’ is negative
warning: excess elements in array initializer

You can only have positive integer as size of an array and nothing else.

Trying To Assign More Values Than The Array Size

#include<stdio.h>

int main()
{
    int a[5] = {1, 2, 3, 4, 5, 6, 7}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
warning: excess elements in array initializer

You can’t assign more values than the array size.

Overwrite values present at an index: Array

#include<stdio.h>

int main()
{
    int a[5] = {1, 2, 3, 4, 5}, i;

    a[3]  = 100;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
1
2
3
100
5

Here the previous value present at index 3(which can be access using a[3]), is overwritten by value 100.

Garbage Values In Empty Spots: Array

#include<stdio.h>

int main()
{
    int a[5], i;

    a[3]  = 100;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
6356864
4200766
4200672
100
8

As you can see, the element at index 3 has 100, and all other elements value is just garbage values.

Array Size In Memory

#include<stdio.h>

int main()
{
    int    a[5];
    float  b[5];
    char   c[5];
    double d[5];

    printf("Int    Array: %d\n", 5 * sizeof(int));
    printf("Float  Array: %d\n", 5 * sizeof(float));
    printf("Char   Array: %d\n", 5 * sizeof(char));
    printf("Double Array: %d\n", 5 * sizeof(double));

    return 0;
}

Output:
Int Array: 20
Float Array: 20
Char Array: 5
Double Array: 40

Each cell in array occupies memory space depending upon its data type. Int and float occupies 4 bytes. Char occupies 1 byte. Double occupies 8 bytes. Also remember, size of data type is machine dependent.

Initializing and Accessing Array Elements

#include<stdio.h>

int main()
{
    int a[5], i;

    printf("Enter 5 integer numbers\n");
    for(i = 0; i < 5; i++)
        scanf("%d", &a[i]);

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
1
2
3
4
5

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Size of Pointer Variables

Lets write a C program to find out the size or the number of bytes occupied by pointer variables of different data type in your computers memory.

Related Read:
Sizeof Operator in C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Find Size of Pointer Variables


[youtube https://www.youtube.com/watch?v=_EaAZYSvBog]

YouTube Link: https://www.youtube.com/watch?v=_EaAZYSvBog [Watch the Video In Full Screen.]


Source Code: C Program To Find Size of Pointer Variables

#include<stdio.h>

int main()
{
    printf("Size of int pointer = %d bytes.\n", sizeof(int*));
    printf("Size of char pointer = %d bytes.\n", sizeof(char*));
    printf("Size of float pointer = %d bytes.\n", sizeof(float*));
    printf("Size of double pointer = %d bytes.\n", sizeof(double*));
    printf("Size of long int pointer = %d bytes.\n", sizeof(long*));
    printf("Size of short int pointer = %d bytes.\n", sizeof(short*));

    return 0;
}

Output:
Size of int pointer = 4 bytes.
Size of char pointer = 4 bytes.
Size of float pointer = 4 bytes.
Size of double pointer = 4 bytes.
Size of long int pointer = 4 bytes.
Size of short int pointer = 4 bytes.

If you observe the output of above C program you can see that pointer variables, irrespective of their data type, consume 4 bytes of data in the memory.

Note: “But the number of bytes allocated for different data types and pointer variables are machine dependent. 16-bit, 32-bit, 64-bit computers allocate different bytes of memory. But pointer variables of any data type will always have same number of bytes occupied in the memory. For Example, if the computer allocates 2 bytes for pointer variable, then all the pointer variables, irrespective of their data type, will occupy 2 bytes in the memory.”

A pointer variable of type float holds only the address of floating point variable. A char pointer variable holds only the address of char type variable. But still the address of all these type of pointer variable is number. Addresses are always numbers and it can’t be a character, a string or real/floating/double numbers.

Also note that addresses are unique. There can’t be 2 location in your computers memory with same address.

Note: Observe that I’m using %d as format specifier while printing the size of data type, that’s because sizeof() function/method returns integer type data i.e., number of bytes occupied in the computers memory.

Note: sizeof() method takes a single argument and it is not a function whose value is determined at run time, but rather an operator whose value is determined by compiler – so it’s called as compile time unary operator.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Sizeof Operator in C Programming Language

Lets write a C program to see how to use sizeof() method or function in C programming language.

sizeof() is a builtin method/function present in C programming. It is used to calculate the size(in bytes) that a datatype occupies in the computers memory.

sizeof() method takes a single argument and it is not a function whose value is determined at run time, but rather an operator whose value is determined by compiler – so it’s called as compile time unary operator.

sizeof() method can be used with primitive data type like int, float, char or user defined data types like structures, unions etc.

Video Tutorial: Sizeof Operator in C Programming Language


[youtube https://www.youtube.com/watch?v=Mlw4jJ4WX8A]

YouTube Link: https://www.youtube.com/watch?v=Mlw4jJ4WX8A [Watch the Video In Full Screen.]

Source Code: Sizeof Operator in C Programming Language

#include < stdio.h >

int main()
{
    int a, a1[10];
    char s;
    char s1[10];
    int b = 10, c = 5;

    printf("Size of a single character is %d byes\n", sizeof(s));
    printf("Size of a character array s1[10] is %d byes\n", sizeof(s1));
    printf("Size of a integer is %d byes\n", sizeof(a));
    printf("Size of a long integer is %d byes\n", sizeof(long int));
    printf("Size of a long long integer is %d byes\n", sizeof(long long int));
    printf("Size of a integer array a1[10] is %d byes\n", sizeof(a1));
    printf("Size of a float is %d byes\n", sizeof(float));
    printf("Size of a double is %d byes\n", sizeof(double));
    printf("Size of a long double is %d byes\n", sizeof(long double));
    printf("Size of a b+c is %d byes\n", sizeof(b+c));

    return 0;
}

Output:
Size of a single character is 1 byes
Size of a character array s1[10] is 10 byes
Size of a integer is 4 byes
Size of a long integer is 4 byes
Size of a long long integer is 8 byes
Size of a integer array a1[10] is 40 byes
Size of a float is 4 byes
Size of a double is 8 byes
Size of a long double is 12 byes
Size of a b+c is 4 byes

short, int, long int, long long int

#include < stdio.h >

int main()
{
    printf("Size of short data type is %d\n", sizeof(short));
    printf("Size of integer data type is %d\n", sizeof(int));
    printf("Size of long integer data type is %d\n", sizeof(long int));
    printf("Size of long long integer data type is %d\n", sizeof(long long int));

    return 0;
}

Output:
Size of short data type is 2
Size of integer data type is 4
Size of long integer data type is 4
Size of long long integer data type is 8

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert