C
PROGRAMMIN
GUNIT-3
ARRAY
Introduction to
Array:
What is an Array?
An array in C is a collection of similar data types stored in contiguous memory locations. It's
a fundamental data structure used to store multiple values under a single name.
Declaring an Array
To declare an array, you specify the data type, array name, and size:
Syntax:
data_type array_name[size];
Example:
int numbers[5];
One Dimentional
Array:
A one-dimensional array is a linear collection of elements of the same data type, stored in
contiguous memory locations. It's like a row of containers, each holding a value of the same
type.
Declaring a One-Dimensional Array
Syntax:
data_type array_name[size];
• data_type: The data type of the elements (e.g., int, float, char).
• array_name: The name of the array.
• size: The number of elements in the array.
Accessing Array Elements:
To access an element, use its index within square brackets:
Syntax:
array_name[index]
• index: The position of the element (starts from 0).
Example:
numbers[2] = 30;
Initializing an Array:
You can initialize an array while declaring it:
Syntax:
data_type array_name[size] = {value1, value2, ..., valueN};
Example:
int marks[5] = {85, 92, 78, 95, 88};
Example Program for one dimentional array:
#include <stdio.h>
int main() {
int numbers[5] = {2, 4, 6, 8, 10};
int i;
// Accessing and printing array elements
for (i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Two Dimentional
Array:
Declaring a Two-Dimensional Array
syntax:
data_type array_name[rows][columns];
Example:
int matrix[3][4];
A two-dimensional array is essentially an array of arrays. It can be visualized as a matrix with
rows and columns. Each element in the array is identified by two indices: one for the row and
one for the column.
Accessing Array Elements:
To access an element, use two indices:
Syntax:
array_name[row][column];
Example:
matrix[1][2] = 10;
Initializing a Two-Dimensional Array
You can initialize a two-dimensional array while declaring it:
Syntax:
data_type array_name[rows][columns] = {{value11, value12, ...},
{value21, value22, ...},
...};
Example:
int numbers[2][3] = {{1, 2, 3}, {4, 5, 6}};
Example Program
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j;
// Accessing and printing array elements
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("n");
}
return 0;
}
Multidimensional
Array:
A multidimensional array is an extension of the concept of a one-dimensional array. It's essentially
an array of arrays, allowing you to store data in a tabular or grid-like structure.
Declaring a Multidimensional Array:
Syntax:
data_type array_name[size1][size2][...][sizeN];
Example:
int matrix[3][4];
Accessing Array Elements
To access an element, use multiple indices separated by commas:
Syntax:
array_name[index1][index2][...][indexN]
Example:
matrix[1][2] = 10;
Initializing a Multidimensional Array
You can initialize a multidimensional array while declaring it:
Syntax:
data_type array_name[size1][size2] = {{value11, value12, ...},
{value21, value22, ...},
Example:
int numbers[2][3] = {{1, 2, 3}, {4, 5, 6}};
...};
Example Program (3D Array)
#include <stdio.h>
int main() {
int cube[2][3][2] = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}};
int i, j, k;
// Accessing and printing array elements
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 2; k++) {
printf("%d ", cube[i][j][k]);
}
printf("n");
}
printf("n");
}
return 0;
}
Character Array and
String:
Character Arrays
A character array in C is simply an array of characters. It's a fundamental building block for
handling text data.
Declaration:
char char_array[size];
• size: The maximum number of characters the array can hold.
Example:
char message[10] = "Hello";
String:
Strings
In C, a string is essentially a character array with a null character ('0') at the end to mark its
termination. This allows functions to determine the length of the string without explicitly
storing it.
Key points:
• Strings are implicitly null-terminated.
• You can use string manipulation functions from the string.h library.
Example:
char greeting[] = "Welcome";
Initializing String Variables in C
In C, strings are essentially character arrays with a null terminator (0) at the end. There are
primarily two methods to initialize a string variable:
1. Direct Initialization
Example:
char str[] = "Hello, world!"
2. Explicit Initialization
Example:
char str[14] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '0'};
Example:
#include <stdio.h>
int main() {
char str1[] = "Hello";
char str2[6] = {'W', 'o', 'r', 'l', 'd', '0'};
printf("%s %sn", str1, str2);
return 0;
}
Reading Strings from the Terminal:
Using scanf()
The scanf() function can be used to read a string from the standard input (terminal). However,
it has limitations: it stops reading at the first whitespace character (space, tab, newline).
Example:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %sn", str);
return 0;
}
Using fgets()
The fgets() function is a safer and more flexible way to read a string, including whitespace
characters. It reads a specified number of characters or until a newline character is
encountered.
Example:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
printf("You entered: %s", str);
return 0;
}
Writing String to Screen:
Using printf()
The most common way to write a string to the screen in C is using the printf() function.
Example:
#include <stdio.h>int main() {
char str[] = "Hello, world!";
printf("%sn", str);
return 0;
}
Using puts()
Another option is the puts() function, which is specifically designed for writing strings to the standard output.
Example:
#include <stdio.h>int main() {
char str[] = "Hello, world!";
puts(str);
return 0;
}
Comparing Strings in C
Using the strcmp() Function
The most common and efficient way to compare strings in C is by using the strcmp() function
from the string.h library.
Syntax:
int strcmp(const char *str1, const char *str2);
Manual String Comparison
While strcmp() is generally preferred, you can also compare strings manually by iterating
through characters and comparing them individually. This approach can be useful for specific
scenarios or learning purposes.
Example Program:
#include <stdio.h>
int compare_strings(const char *str1, const char *str2) {
while (*str1 != '0' && *str2 != '0') {
if (*str1 != *str2) {
return *str1 - *str2; // Return difference in ASCII values
}
str1++;
str2++;
}
return *str1 - *str2; // Compare lengths if strings are equal up to this point
}
int main() {
char str1[] = "hello";
char str2[] = "world";
int result = compare_strings(str1, str2);
if (result == 0) {
printf("Strings are equaln");
} else {
printf("Strings are not equaln");
}
return 0;
}
String Handling Functions in C
C provides a rich set of functions for manipulating strings. These functions are primarily
defined in the string.h header file.
Common String Handling Functions
Here are some of the most commonly used string handling functions in C:
Length of a String
• strlen(str): Returns the length of a string (excluding the null terminator).
Copying Strings
• strcpy(dest, src): Copies the string src to the destination string dest. Be cautious about
buffer overflows.
• strncpy(dest, src, n): Copies at most n characters from src to dest.
Concatenating Strings
• strcat(dest, src): Appends src to the end of dest. Be careful about buffer overflows.
• strncat(dest, src, n): Appends at most n characters from src to dest.
Comparing Strings
• strcmp(str1, str2): Compares two strings lexicographically. Returns 0 if equal, a negative value
if str1 is less than str2, and a positive value if str1 is greater than str2.
• strncmp(str1, str2, n): Compares at most n characters of str1 and str2.
Searching in Strings
• strchr(str, ch): Finds the first occurrence of character ch in string str.
• strrchr(str, ch): Finds the last occurrence of character ch in string str.
• strstr(str1, str2): Finds the first occurrence of string str2 in string str1.
Converting Case
• tolower(ch): Converts a character to lowercase.
• toupper(ch): Converts a character to uppercase.
Other Useful Functions
• strtok(str, delimiters): Breaks a string into tokens based on delimiters.
• memset(str, ch, n): Fills a block of memory with a specific character.
• memcpy(dest, src, n): Copies a block of memory from src to dest.
Example Program
#include <stdio.h>#include <string.h>int main() {
char str1[] = "hello";
char str2[20];
strcpy(str2, str1);
strcat(str2, " world");
printf("%sn", str2);
if (strcmp(str1, "hello") == 0) {
printf("Strings are equaln");
}
char *ptr = strchr(str2, 'o');
if (ptr) {
printf("First occurrence of 'o': %sn", ptr);
}
return 0;
}
THANK YOU
Search . . .

ARRAY's in C Programming Language PPTX.

  • 1.
  • 2.
    Introduction to Array: What isan Array? An array in C is a collection of similar data types stored in contiguous memory locations. It's a fundamental data structure used to store multiple values under a single name. Declaring an Array To declare an array, you specify the data type, array name, and size: Syntax: data_type array_name[size]; Example: int numbers[5];
  • 3.
    One Dimentional Array: A one-dimensionalarray is a linear collection of elements of the same data type, stored in contiguous memory locations. It's like a row of containers, each holding a value of the same type. Declaring a One-Dimensional Array Syntax: data_type array_name[size]; • data_type: The data type of the elements (e.g., int, float, char). • array_name: The name of the array. • size: The number of elements in the array.
  • 4.
    Accessing Array Elements: Toaccess an element, use its index within square brackets: Syntax: array_name[index] • index: The position of the element (starts from 0). Example: numbers[2] = 30; Initializing an Array: You can initialize an array while declaring it: Syntax: data_type array_name[size] = {value1, value2, ..., valueN}; Example: int marks[5] = {85, 92, 78, 95, 88};
  • 5.
    Example Program forone dimentional array: #include <stdio.h> int main() { int numbers[5] = {2, 4, 6, 8, 10}; int i; // Accessing and printing array elements for (i = 0; i < 5; i++) { printf("%d ", numbers[i]); } return 0; }
  • 6.
    Two Dimentional Array: Declaring aTwo-Dimensional Array syntax: data_type array_name[rows][columns]; Example: int matrix[3][4]; A two-dimensional array is essentially an array of arrays. It can be visualized as a matrix with rows and columns. Each element in the array is identified by two indices: one for the row and one for the column.
  • 7.
    Accessing Array Elements: Toaccess an element, use two indices: Syntax: array_name[row][column]; Example: matrix[1][2] = 10; Initializing a Two-Dimensional Array You can initialize a two-dimensional array while declaring it: Syntax: data_type array_name[rows][columns] = {{value11, value12, ...}, {value21, value22, ...}, ...}; Example: int numbers[2][3] = {{1, 2, 3}, {4, 5, 6}};
  • 8.
    Example Program #include <stdio.h> intmain() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int i, j; // Accessing and printing array elements for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%d ", matrix[i][j]); } printf("n"); } return 0; }
  • 9.
    Multidimensional Array: A multidimensional arrayis an extension of the concept of a one-dimensional array. It's essentially an array of arrays, allowing you to store data in a tabular or grid-like structure. Declaring a Multidimensional Array: Syntax: data_type array_name[size1][size2][...][sizeN]; Example: int matrix[3][4]; Accessing Array Elements To access an element, use multiple indices separated by commas: Syntax: array_name[index1][index2][...][indexN] Example: matrix[1][2] = 10;
  • 10.
    Initializing a MultidimensionalArray You can initialize a multidimensional array while declaring it: Syntax: data_type array_name[size1][size2] = {{value11, value12, ...}, {value21, value22, ...}, Example: int numbers[2][3] = {{1, 2, 3}, {4, 5, 6}}; ...};
  • 11.
    Example Program (3DArray) #include <stdio.h> int main() { int cube[2][3][2] = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}}; int i, j, k; // Accessing and printing array elements for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { for (k = 0; k < 2; k++) { printf("%d ", cube[i][j][k]); } printf("n"); } printf("n"); } return 0; }
  • 12.
    Character Array and String: CharacterArrays A character array in C is simply an array of characters. It's a fundamental building block for handling text data. Declaration: char char_array[size]; • size: The maximum number of characters the array can hold. Example: char message[10] = "Hello";
  • 13.
    String: Strings In C, astring is essentially a character array with a null character ('0') at the end to mark its termination. This allows functions to determine the length of the string without explicitly storing it. Key points: • Strings are implicitly null-terminated. • You can use string manipulation functions from the string.h library. Example: char greeting[] = "Welcome";
  • 14.
    Initializing String Variablesin C In C, strings are essentially character arrays with a null terminator (0) at the end. There are primarily two methods to initialize a string variable: 1. Direct Initialization Example: char str[] = "Hello, world!" 2. Explicit Initialization Example: char str[14] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '0'};
  • 15.
    Example: #include <stdio.h> int main(){ char str1[] = "Hello"; char str2[6] = {'W', 'o', 'r', 'l', 'd', '0'}; printf("%s %sn", str1, str2); return 0; }
  • 16.
    Reading Strings fromthe Terminal: Using scanf() The scanf() function can be used to read a string from the standard input (terminal). However, it has limitations: it stops reading at the first whitespace character (space, tab, newline). Example: #include <stdio.h> int main() { char str[100]; printf("Enter a string: "); scanf("%s", str); printf("You entered: %sn", str); return 0; }
  • 17.
    Using fgets() The fgets()function is a safer and more flexible way to read a string, including whitespace characters. It reads a specified number of characters or until a newline character is encountered. Example: #include <stdio.h> int main() { char str[100]; printf("Enter a string: "); fgets(str, 100, stdin); printf("You entered: %s", str); return 0; }
  • 18.
    Writing String toScreen: Using printf() The most common way to write a string to the screen in C is using the printf() function. Example: #include <stdio.h>int main() { char str[] = "Hello, world!"; printf("%sn", str); return 0; } Using puts() Another option is the puts() function, which is specifically designed for writing strings to the standard output. Example: #include <stdio.h>int main() { char str[] = "Hello, world!"; puts(str); return 0; }
  • 19.
    Comparing Strings inC Using the strcmp() Function The most common and efficient way to compare strings in C is by using the strcmp() function from the string.h library. Syntax: int strcmp(const char *str1, const char *str2); Manual String Comparison While strcmp() is generally preferred, you can also compare strings manually by iterating through characters and comparing them individually. This approach can be useful for specific scenarios or learning purposes.
  • 20.
    Example Program: #include <stdio.h> intcompare_strings(const char *str1, const char *str2) { while (*str1 != '0' && *str2 != '0') { if (*str1 != *str2) { return *str1 - *str2; // Return difference in ASCII values } str1++; str2++; } return *str1 - *str2; // Compare lengths if strings are equal up to this point }
  • 21.
    int main() { charstr1[] = "hello"; char str2[] = "world"; int result = compare_strings(str1, str2); if (result == 0) { printf("Strings are equaln"); } else { printf("Strings are not equaln"); } return 0; }
  • 22.
    String Handling Functionsin C C provides a rich set of functions for manipulating strings. These functions are primarily defined in the string.h header file. Common String Handling Functions Here are some of the most commonly used string handling functions in C: Length of a String • strlen(str): Returns the length of a string (excluding the null terminator). Copying Strings • strcpy(dest, src): Copies the string src to the destination string dest. Be cautious about buffer overflows. • strncpy(dest, src, n): Copies at most n characters from src to dest. Concatenating Strings • strcat(dest, src): Appends src to the end of dest. Be careful about buffer overflows. • strncat(dest, src, n): Appends at most n characters from src to dest.
  • 23.
    Comparing Strings • strcmp(str1,str2): Compares two strings lexicographically. Returns 0 if equal, a negative value if str1 is less than str2, and a positive value if str1 is greater than str2. • strncmp(str1, str2, n): Compares at most n characters of str1 and str2. Searching in Strings • strchr(str, ch): Finds the first occurrence of character ch in string str. • strrchr(str, ch): Finds the last occurrence of character ch in string str. • strstr(str1, str2): Finds the first occurrence of string str2 in string str1. Converting Case • tolower(ch): Converts a character to lowercase. • toupper(ch): Converts a character to uppercase. Other Useful Functions • strtok(str, delimiters): Breaks a string into tokens based on delimiters. • memset(str, ch, n): Fills a block of memory with a specific character. • memcpy(dest, src, n): Copies a block of memory from src to dest.
  • 24.
    Example Program #include <stdio.h>#include<string.h>int main() { char str1[] = "hello"; char str2[20]; strcpy(str2, str1); strcat(str2, " world"); printf("%sn", str2); if (strcmp(str1, "hello") == 0) { printf("Strings are equaln"); } char *ptr = strchr(str2, 'o'); if (ptr) { printf("First occurrence of 'o': %sn", ptr); } return 0; }
  • 25.