C-Programming : Arrays and Strings

Redmen Ishab
3 min readMay 24, 2022

Arrays

Array is a sequence of datatypes i.e. int, float, char, etc. stored within square bracket such that each item is separated by ‘,’ (comma) operator. Each item of array are stored in the memory in continuous order.

Declaration syntax:

data_type array_name[ size_of_array ];
Eg:
int primeNumbers[10];

Ways of declaring arrays :

  1. Predefined size of an array:
    data_type array_name [ size ]
    eg: int primeNumber [100]
  2. Dynamic size of an array:
    data_type array_name[] = { data1, data2, data3, data4 }
    eg: int primeNumber [] = {1, 2, 3, 5, 7 }

Types of arrays on the basis of dimension:

Similar to matrix representation the array can be represented in single or multiple dimensional form.

  1. One dimension Array: The array having single square bracket is 1-D array.
    eg: int primeNumber [] = {1, 2, 3, 5, 7 }
  2. Multi dimension Array:
    Array within an array are known as multi dimensional array. Example of 2-D array is given below.
Initialization of 2-D array: int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};

int c[][3] = {{1, 3, 0}, {-1, 5, 9}};

int c[2][3] = {1, 3, 0, -1, 5, 9};

Strings

Strings is also array in c programming that datatype is always ‘char’ i.e :

char name [5];
char name2 [] = {'H','O','M','E'}

P.S: For efficiency and readability strings can be declared using syntactic sugar supported by c i.e char name[] = ‘HOME’; Also the end character of string is always ‘/0’.

strings stored in memory

String Handling Functions:

‘string.h’ library supports large number of string handling functions that can be operated in strings . The most commonly used are :

1. strcat()It is used to concatenate(combine) two strings.

2. strlen()It is used to show the length of a string.

3. strrev()It is used to show the reverse of a string.

4. strcpy()Copies one string into another.

5. strcmp()It is used to compare two string.

This wraps up the concept of arrays and strings in c-programming.

Practice Questions:

  1. WAP to store name of a user, and print the first and last letter of the name.
  2. WAP to print the memory address of the individual character of question no.1 .
  3. WAP to rotate the array of first 5 prime number to the left by 2 position.
  4. WAP to sort the array in ascending number where array has randomly arranged numbers provided by user.

--

--

Redmen Ishab

Software Engineer. Failed startup “Software Factory”. Working experience as CTO, SSE, Full Stack Dev, Mobile App Engineer, and Lecturer.