String in C Definition, Syntax and Working

Strings are mainly defined as an array of characters. In simple words it is one dimensional array of characters which is terminated by special character called the null ('/0') character. 

In simple words string is a line of characters, like a word or a sentence. Each character in the string is stored in a specific memory location, and the null character marks the end of the string.



  Syntax:

datatype array_name[size of array];
{
       //block of code
}




  How it works?

char str[]="Hello";

String in C  definition, syntax and working, String in C programming


Above image shows the memory representation of string with their index and memory address.




  Example program of string:



#include<stdio.h>
void main()
{
char str[]="Hello";

printf("%s",str);
}


Above program shows the following output:

String in C  definition, syntax and working, Strings in C programming
 

To explore the exercises and examples of String click Here...

C Program to Print Multiplication of Two Matrices using Array

Ex: Write a C program to print multiplication of two matrices. How to write a C program to print multiplication of two matrices. C program to print multiplication of two matrices.

Input from user:

Enter elements in first matrix of size 3x3:
1 3 5
2 4 6
3 5 7

Enter elements in second matrix of size 3x3: 
1 3 5
2 4 6
3 5 7

Expected output:

Multiplication of two matrices=
22 40 58
28 52 76
34 64 94



  • Quick links:
  1. What is array?
  2. For loop



   Logic to multiply two matrices:


To print multiplication of two matrices we need to multiply it into below(row*column) format:


C program to print multiplication of two matrices using array
C program to print multiplication of two matrices using array




  Step by step logic of the given program:



1. Accept two matrices from user declare varible say a and b(use 2 dimensional array to store elements).

2. Store elements one by one in matrix a using: 

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
            scanf("%d", &a[row][col]);
        }
    }
Follow same these  steps for matrix b.

3. Multiply two matrices a&b element wise using:

for(i=0;i<size;i++)
{
sum+=a[row][i]*b[i][col];

}

4. Print multiplication of two matrices on the output screen.




  Program to print multiplication of two matrices using array:


#include<stdio.h>
#define size 3//size of matrix

int main()
{
int a[size][size],
b[size][size],
c[size][size];

int row,col,i,sum=0;

//accept elements in first matrix
printf("Enter elements in first matrix of size 3x3:\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d",&a[row][col]);
}
}

//accept elements in second matrix
printf("Enter elements in second matrix of size 3x3:\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d",&b[row][col]);
}
}
//multiply both matrices a&b
for(row=0;row<size;row++)
{
     for(col=0;col<size;col++)
     {
    for(i=0;i<size;i++)
{
sum+=a[row][i]*b[i][col];
}
c[row][col]=sum;
sum=0;
  }
}

//print multiplication of matrices
printf("Multiplication of matrices=\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
printf("%d  ",c[row][col]);
}   
  printf("\n");
}
return 0;
}


Above program shows the following output:


C program to print multiplication of two matrices using array


Share your thoughts below in the comment section....

C Program to Print Subtraction of Two Matrices using Array

Ex: Write a C program to subtract two matrices using array. How to write a C program to subtract two matrices using array. C program to subtract two matrices using array.

Input from user: 


Enter elements in 1st matrix of size 3x3: 

1 2 3
4 5 6
7 8 9

Enter elements in 2nd matrix of size 3x3:

1 2 3
4 5 6
7 8 9


Expected output:


Subtraction of matrices=

0 0 0
0 0 0
0 0 0



  • Quick links:
  1. What is array?
  2. For loop


  • Logic of matrix Subtraction:

Matrix Subtraction is done element wise. Means Subtraction of matrices is defined by A-B= A↓ij - B↓ij.

C program to print subtraction of two matrices using array
C program to print subtraction of two matrices using array




  Step by Step logic of the given program:


1. Accept two matrices from user declare varible say arr1 and arr2(use 2 dimensional array to store elements).

2. Store elements one by one in arr1 using: 

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
            scanf("%d", &arr1[row][col]);
        }
    }
Follow same these  steps for arr2.

3. Subtract two matrices arr1-arr2 element wise using:

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
          arr3[row][col] = arr1[row][col] - arr2[row][col];
        }
    }

4. Last print Subtraction of two matrices means resultant matrix arr3.





  C program to print subtraction of two matrices using array:


#include<stdio.h>
#define size 3 // size of the matrix

int main()
{

    int arr1[size][size]; // 1st matrix
int arr2[size][size]; // 2nd Matrix
int arr3[size][size]; // Resultant matrix


    int row, col;

/* input elements in first matrix*/
printf("Enter elements in 1st matrix of size 3x3:\n");

    for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr1[row][col]);
}
}

/* input elements in second matrix */

    printf("\nEnter elements in 2nd matrix  of size 3x3: \n");

    for(row=0; row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr2[row][col]);
}
}


    /* subtract both matrices arr1 and arr2 and store result in matrix arr3*/
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
    {
  arr3[row][col] = arr1[row][col] - arr2[row][col];
}
}


 /* Print the value of resultant matrix arr3 */ 
printf("\nSubtraction of matrices = \n");

    for(row=0; row<size;row++)
{
for(col=0; col<size;col++)
{
printf("%d ",arr3[row][col]);
}
printf("\n");
}

    return 0;

}



Above program shows the following output:


C program to print subtraction of two matrices using array


Thanks for visiting. If you have any doubts, Feel free to share in the comment section. 
To Explore more C Programming Examples Click Here.

C Program to Add Two Matrices using Array

Ex: Write a C program to add two matrices using array. How to write a C program to add two matrices using array. C program to add two matrices using array.

Input from user: 

Enter elements in 1st matrix of size 3x3: 

1 1 1
2 2 2
3 3 3

Enter elements in 2nd matrix of size 3x3:

4 4 4
3 3 3
2 2 2


Expected output:


Sum of matrices=

5 5 5
5 5 5
5 5 5



  • Quick links:
  1. What is array?
  2. For loop



  • Logic of matrix addition:

Matrix addition is done element wise. Means sum of matrices is defined by A+B= A↓ij + B↓ij.



C program to add two matrices using array,  Matrix addition using C programming language
C program to add two matrices using array




  Step by Step logic of the given program:


1. Accept two matrices from user declare varible say arr1 and arr2(use 2 dimensional array to store elements).


2. Store elements one by one in arr1 using: 

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
            scanf("%d", &arr1[row][col]);
        }
    }
Follow same these  steps for arr2.

3. add two matrices arr1+arr2 element wise using:

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
          arr3[row][col] = arr1[row][col] + arr2[row][col];
        }
    }

4. Last print sum of two matrices means resultant matrix arr3.






  C program to add two matrices using array:


#include<stdio.h>
#define size 3 // size of the matrix

int main()
{

    int arr1[size][size]; // 1st matrix
int arr2[size][size]; // 2nd Matrix
int arr3[size][size]; // Resultant matrix


    int row, col;

/* input elements in first matrix*/
printf("Enter elements in 1st matrix of size 3x3:\n");

    for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr1[row][col]);
}
}

/* input elements in second matrix */

    printf("\nEnter elements in 2nd matrix  of size 3x3: \n");

    for(row=0; row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr2[row][col]);
}
}


    /* Add two matrices arr1 and arr2 and store result in matrix arr3*/
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
    {
  arr3[row][col] = arr1[row][col] + arr2[row][col];
}
}


 /* Print the value of resultant matrix arr3 */ 
    printf("\nSum of matrices = \n");

    for(row=0; row<size;row++)
{
for(col=0; col<size;col++)
{
printf("%d ",arr3[row][col]);
}
printf("\n");
}

    return 0;

}


Above program shows the following output:


C program to add two matrices using array

Array's in C Exercises and Solutions

Array is collection of similar data types. You can store group of data of same data types using array. In array different data types elements are not allowed that simply means if we declare an integer array then we can store only integer type of elements in it.


There are two types of arrays in C:

1. One dimensional array.

2. Multi-dimensional array:

  • Two dimensional array.
  • Three dimensional array.
  • Four dimensional array etc...

C Program to Merge Two Arrays Sorted in Descending Order

Ex: Write a C program to merge two arrays and sort in descending order. How to write a C program to merge two arrays and sort in descending order. C program to merge two arrays and sort in descending order.

Input from user:

Enter the limit of first array: 3
Enter elements: 
1
2
3

Enter the limit of second array: 3
Enter elements:
1
2
3

C Program to Copy the Elements of one Array into Another Array

Ex: Write a C program to copy the elements of one array into another array. How to write a C program to copy the elements of one array into another array. C program to copy the elements of one array into another array.


Input from user:

Enter the limit: 5

Enter elements: 
1
3
5
7
9

Expected output:

Elements in 1st array are: 
arr[0]=1
arr[1]=3
arr[2]=5
arr[3]=7
arr[4]=9

C Program to Print Array Elements in Reverse Order

Ex: Write a C program to print given array elements in reverse order. How to write a C program to print array elements in reverse order. C program to print array elements in reverse order.

Input from user:

Enter the limit: 5

Enter the elements:
10
20
30
40
50

Expected output:

Given elements in reverse order:
50 40 30 20 10

What is Break Statement in C

Break statement is very useful to exit from any loop  such as for loop, while loop and do while loop. When the break statement is encountered in the loop then the loop will stop running the statements and immediately exit from the loop.



  Syntax of Break Statement in C :

Continue Statement in C

Continue statement skips some lines of code inside the loop and continues with the next iteration. 
When the continue statement is encounters in a loop, then program will skip the all the statements after the continue statement and the loop continues with the next iteration.
It is mainly used for a code which we want to skip.


  Syntax of Continue Statement in C :