Working with Arrays and Strings in C

Markdown

View as Markdown

Arrays and strings are fundamental data structures in C programming. Arrays allow you to store multiple values of the same data type in a single variable, while strings enable programs to work with text efficiently.

Learning Objectives

  • Understand the concept of arrays

  • Create one-dimensional and two-dimensional arrays

  • Work with multidimensional arrays

  • Store and display strings

  • Use common string handling functions

  • Apply arrays and strings in practical programs

What is an Array?

An array is a collection of elements of the same data type stored in contiguous memory locations.

Instead of declaring multiple variables, an array stores similar values under a single name.

Example:

int marks[5];

This creates an integer array capable of storing five values.

One-Dimensional Array

A one-dimensional array stores data in a single row.

Example:

#include <stdio.h>

int main()

{

    int marks[5] = {80, 75, 90, 85, 88};

 

    int i;

    for(i = 0; i < 5; i++)

    {

        printf("%d\n", marks[i]);

    }

    return 0;

}

Output

80

75

90

85

88

Two-Dimensional Array

A two-dimensional array stores data in rows and columns.

Example:

#include <stdio.h>

int main()

{

    int marks[2][3] =

    {

        {80, 90, 85},

        {75, 88, 92}

    };

    int i, j;

    for(i = 0; i < 2; i++)

    {

        for(j = 0; j < 3; j++)

        {

            printf("%d ", marks[i][j]);

        }

        printf("\n");

    }

    return 0;

}

Output

80 90 85

75 88 92

Multidimensional Arrays

Multidimensional arrays extend beyond two dimensions and are commonly used for storing matrices, tables, and scientific data.

Example:

int data[2][3][4];

Introduction to Strings

A string is a sequence of characters terminated by the null character (\0).

Example:

char name[] = "Team Academy";

Strings are internally stored as character arrays.

Reading and Displaying Strings

Example:

#include <stdio.h>

int main()

{

    char name[30];

    printf("Enter your name: ");

    scanf("%s", name);

    printf("Hello %s", name);

    return 0;

}

String Handling Functions

The <string.h> library provides several useful string functions.

Function

Purpose

strlen()

Returns string length

strcpy()

Copies one string to another

strcat()

Concatenates strings

strcmp()

Compares two strings

Example:

#include <stdio.h>

#include <string.h>

int main()

{

    char name[] = "Programming";

 

    printf("Length = %lu", strlen(name));

    return 0;

}

Output

Length = 11

Dynamic Arrays

Dynamic arrays are created during program execution using dynamic memory allocation functions such as malloc() and calloc().

These concepts are covered in detail later under Dynamic Memory Allocation.

Best Practices

  • Initialize arrays before using them.

  • Avoid accessing elements outside the array bounds.

  • Allocate sufficient memory for strings.

  • Use appropriate string functions from <string.h>.

  • Validate user input before storing it.

Common Mistakes

  • Accessing invalid array indexes.

  • Forgetting the null character in strings.

  • Using = instead of strcmp() for string comparison.

  • Declaring insufficient array size.

 

 

 

 

 

 

 

 

 

 

 

Was this article helpful?

Still need help?

Contact us