0% found this document useful (0 votes)
3 views7 pages

C Arrays and String Basics Guide

The document provides an overview of arrays, two-dimensional arrays, and strings in C programming. It covers their declaration, initialization, accessing elements, and various string manipulation functions such as strlen(), strcpy(), strcmp(), and strcat(). Key points include the use of arrays for storing multiple values, 2D arrays for tabular data, and the importance of handling strings properly.

Uploaded by

Fatima Amin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

C Arrays and String Basics Guide

The document provides an overview of arrays, two-dimensional arrays, and strings in C programming. It covers their declaration, initialization, accessing elements, and various string manipulation functions such as strlen(), strcpy(), strcmp(), and strcat(). Key points include the use of arrays for storing multiple values, 2D arrays for tabular data, and the importance of handling strings properly.

Uploaded by

Fatima Amin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Arrays in C

An array is a collection of similar data types stored in contiguous memory locations. It allows storing
multiple values using a single variable.

1.1 Declaration and Initialization

Syntax:

data_type array_name[size];

Example:

int arr[5]; // Declaring an integer array of size 5

Initialization:

Arrays can be initialized in multiple ways.

1. At the time of declaration:

int arr[5] = {10, 20, 30, 40, 50};

2. Partial initialization (remaining elements are set to 0):

int arr[5] = {1, 2}; // {1, 2, 0, 0, 0}

3. Without specifying size:

int arr[] = {5, 10, 15, 20};

1.2 Accessing Array Elements

Array elements can be accessed using their index (0-based indexing).

printf("%d", arr[2]); // Outputs 30

1.3 Updating Array Elements

arr[1] = 100; // Updates second element to 100

2. Two-Dimensional (2D) Arrays

A 2D array is an array of arrays, used to store data in rows and columns.

2.1 Declaration and Initialization

Syntax:

data_type array_name[rows][columns];

Example:

int matrix[3][3]; // Declaring a 3x3 matrix


Initialization:

int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

or without curly braces:

int matrix[2][3] = {1, 2, 3, 4, 5, 6};

2.2 Accessing Elements

printf("%d", matrix[1][2]); // Accesses element at row 1, column 2 (outputs 6)

2.3 Taking Input and Displaying Output

#include <stdio.h>

int main() {

int matrix[2][2];

// Taking input

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

for(int j = 0; j < 2; j++) {

printf("Enter element at [%d][%d]: ", i, j);

scanf("%d", &matrix[i][j]);

// Printing elements

printf("Matrix:\n");

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

for(int j = 0; j < 2; j++) {

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

printf("\n");

return 0;

}
3. Strings in C

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

3.1 Declaring and Initializing Strings

Declaration:

char str[20]; // Can hold up to 19 characters + null character

Initialization Methods:

1. Character array with null character:

char str1[] = {'H', 'e', 'l', 'l', 'o', '\0'};

2. String literal:

char str2[] = "Hello";

3. Pointer to string:

char *str3 = "Hello";

4. String Functions in C

C provides several functions for string handling.

4.1 gets() - Input a String

Reads a string from standard input (deprecated due to buffer overflow risk).

Syntax:

char str[50];

gets(str);

Example:

char name[30];

printf("Enter your name: ");

gets(name);

printf("Hello, %s", name);

🛑 Note: gets() is unsafe; use fgets() instead.

4.2 puts() - Output a String

Prints a string and moves to the next line.


Syntax:

puts(str);

Example:

char str[] = "Welcome";

puts(str);

4.3 getc() - Read a Character

Reads a single character from standard input.

Syntax:

char ch = getc(stdin);

Example:

char ch;

printf("Enter a character: ");

ch = getc(stdin);

printf("You entered: %c", ch);

4.4 putc() - Print a Character

Writes a single character to standard output.

Syntax:

putc(ch, stdout);

Example:

char ch = 'A';

putc(ch, stdout); // Prints 'A'

5. String Manipulation Functions

5.1 strlen() - String Length

Returns the number of characters in a string (excluding \0).

Syntax:

int length = strlen(str);

Example:

#include <stdio.h>
#include <string.h>

int main() {

char str[] = "Hello";

printf("Length: %d", strlen(str)); // Outputs 5

return 0;

5.2 strcpy() - Copy a String

Copies one string into another.

Syntax:

strcpy(destination, source);

Example:

#include <stdio.h>

#include <string.h>

int main() {

char source[] = "Hello";

char destination[20];

strcpy(destination, source);

printf("Copied String: %s", destination);

return 0;

5.3 strcmp() - Compare Two Strings

Compares two strings and returns:

 0 if both are equal

 <0 if first string is smaller

 >0 if first string is greater

Syntax:

int result = strcmp(str1, str2);

Example:

#include <stdio.h>
#include <string.h>

int main() {

char str1[] = "Apple";

char str2[] = "Banana";

if(strcmp(str1, str2) == 0)

printf("Strings are equal");

else

printf("Strings are not equal");

return 0;

5.4 strcat() - Concatenate (Join) Two Strings

Appends source to destination.

Syntax:

strcat(destination, source);

Example:

#include <stdio.h>

#include <string.h>

int main() {

char str1[20] = "Hello ";

char str2[] = "World!";

strcat(str1, str2);

printf("Concatenated String: %s", str1);

return 0;

Conclusion

 Arrays allow storing multiple values of the same type.

 2D arrays help store tabular data.


 Strings are character arrays ending with \0.

 String functions like strlen(), strcpy(), strcmp(), and strcat() help in string manipulation.

You might also like