5 Secrets of Blogger (Service) You Don't Know

Recent Posts

Arrays and Strings in C

We can assign integer, float numbers or digits easily by just putting assignment operator between variable name and required numbers or digits.

But if we want to assign character there has little difference as below.

char ch = 'A';

We are allowed only one character within single quote to assign. If you try to assign more than one character with this method it will show garbage results with warnings.

To check this you can run the following program from your compiler.
#include <stdio.h>
int main()
{
    char ch;
    ch = 'ABCD';

    printf("%c",ch);

    return 0;
}
What is the method to assign multiple characters to variable?

There has a method to assign multiple characters to variable with Array.

Array is a fixed size derived data type that contains the element of same data type.

The declaration of array is:

data_type variable [size];

And the initialization or assigning of array is:
data_type variable [size] = {Element of array by separating with comma};

Example:

int numbers[6] = {1, 4, 2, 88, -90, 3};

This array declaration means, numbers is the name of array that contains only integer data type numbers and the maximum 6 numbers can contain as element of array.

Arrays and Strings in C

For the character type data the array will be:

char variable_name[size] = {characters are separated by comma and enclose with single quote};

N.B. Size must be integer type.

Example:

char blog[11]= {'C', 'O', 'M', 'P', 'R', 'O', 'M', 'A', 'T', 'H', '\0'};

Observations:

  • This is called array declaration and assignment.
  • blog is the name of array and the rules of naming array is similar as variable.
  • Number between [] is the length indication of number of characters to assign.
  • The above statement declares that COMPROMATH will be assigned to a variable called blog.
  • There has additional '\0' character at the last of this series of characters. It is called null character. This null character declares the end of assigning characters to array.

Now run the following program.
#include <stdio.h>
int main()
{
    /*ARRAY DECLARATION AND ASSIGNMENT*/
    char blog[11]= {'C', 'O', 'M', 'P', 'R', 'O', 'M', 'A', 'T', 'H', '\0'};

    /*PRINT THE RESULT*/
    printf("%s\n",blog);

    return 0;
}
Do you think the method of assigning multiple characters together to a variable is quite difficult and bored?

Yes. Don't be upset. There has another simple method to assign multiple characters together. If we want to assign the COMPROMATH then we can do it as below:

char ch[11]= "COMPROMATH";

But remember the number of size will be greater than number of characters to assign and put double quotation within String (sequence of characters).

Now run the following program:
#include <stdio.h>
int main()
{
    char ch[11]= "COMPROMATH";
    
    printf("%s\n",blog);

    return 0;
}
N.B. %s for string.

Question: What is String in C?

Ans: String in C is the sequence of characters. String may be a word or sentence with whitespace.
Example: Hello, Hello World etc.


Before discussing about array types we should to know how array works, how the element of array are stored in memory and what is the addressed of memory for array and its element.

Looking for more effecive Course?

C Programming For Beginners by Udemy icon
$15 (Original $145)
30-Day Money-Back Guarantee
Buy
Includes:
  • 6 hours on-demand video
  • 1 Article
  • 2 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
We know when we declare a variable in program, computer offers specific space for this variable. We can call this as the location of variable in memory and it has unique address.

The address of variable can be found from programming if we type

printf("%u", &variable_name);

Appending an ampersand (&) before the variable name define the address of variable.  If we execute the above statement it will return as 100979797. It is the address of variable. Remember, address of variable depends on system. So address of variable in one computer may be different from the address of same variable with same data type. And addresses of different variables in one system can't be same.
#include <stdio.h>
int main()
{
    int variable, variables[4] = {1, 2, 3, 4};

    printf("Address of variable is %u\n",&variable);
    printf("Address of variables is %u\n",variables);
    printf("Address of variables[0] is %u\n",&variables[0]);
    printf("Address of variables[1] is %u\n",&variables[1]);
    printf("Address of variables[2] is %u\n",&variables[2]);
    printf("Address of variables[3] is %u\n",&variables[3]);

    return 0;

}

One dimensional array

One dimensional Array in C is a list of item with one subscript.

Declaration of One Dimensional Array: The declaration of one dimensional array is:
data_type variable_name[size];
Example:int numbers[10];

Initialization of One Dimensional Array: The initialization of one dimensional array can be in two ways at compile time and run time.

Initialization at Compile Time: We can initial one dimensional array at compile time by assigning the values as below:
data_type variable_name [size] = {values with seperation};
Example: float numbers[10] = {1.05, 2.06, 33.5, 77.4, 66.2, 2.09, 3.058, 8.9, 6.48, 55.5};

You can skip the size if you declare the elements of array. As a result, we can rewrite the above statement as:
float numbers[] = {1.05, 2.06, 33.5, 77.4, 66.2, 2.09, 3.058, 8.9, 6.48, 55.5};

Initialization at Run Time: Sometime it is required to input data in array from user to implement calculation and result and it can also do it by initializing at run time.

Suppose, the declared array is int digits[10]; and it isn't initialized at compile time. We want to initial elements of array at run time then we can do it with any loop, if else condition etc. Now run and observe the following program:
#include <stdio.h>
int main()
{
    int i, digits[10];

    printf("Input the elements:\n");
    /*Hit Enter after each input*/

    for(i=0; i<=9; i++)
    {
        scanf("%d",&digits[i]);
    }
    //Print the result
    printf("The output:\n");

    for(i=0; i<=9; i++)
    {
        printf("%d\n",digits[i]);
    }
    return 0;
}

Two dimensional array

If we want to work with data of table with array then we can do it with two or multidimensional array. The basic concept of two dimensional and multidimensional array is same, we will see about two dimensional array.
Suppose, here is a list. And we want to work with the red marked table.  Look there has 4 rows and 3 columns.

So the declaration of two dimensional array will be:
int list_pro[2][4];
Here has 2 size. The first one defines the number of row and second defines the number of column.
1 2 3 4
5 6 7 8
How can do initialization of two dimensional array?

Similarly we can initial at compile time and run time.

Initialization at Compile Time: The initialization of two dimensional at compile time is as below:
int list_pro[2][4] = {
{1,2,3,4},{5,6,7,8}
};
Initialization at Run Time: The initialization of two dimensional at run time is as below:

We can do it in nested For Loop.
#include <stdio.h>
int main()
{
    int list_pro[2][4];
    int i, j;

    for(i=0; i<2; i++)
    {
        for(j=0; j<4; j++)
        {
            scanf("%d", &list_pro[i][j]);
        }
    }
    //PRINT THE RESULT

    for(i=0; i<2; i++)
    {
        for(j=0; j<4; j++)
        {
            printf("%d\t", list_pro[i][j]);
        }
        printf("\n");
    }

    return 0;
}
Row/Column COL-1 COL-2 COL-3 COL-4
ROW-1 list_pro[0][0] list_pro[0][1] list_pro[0][2] list_pro[0][3]
ROW-2 list_pro[1][0] list_pro[1][1] list_pro[1][2] list_pro[1][3]
If we separate the initialization of two dimensional at run time, then it will look as.

The outer loop increment works for rows and inner loop increment works for columns.

String operation


Have got this article helpful or not working? Put your comment below.