SlideShare a Scribd company logo
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 . . .

More Related Content

PPTX
C Programming Unit-3
PPTX
introduction to strings in c programming
PPTX
C-Arrays & Strings (computer programming).pptx
PPTX
fundamentals of c programming_String.pptx
PPTX
Arrays & Strings.pptx
DOC
Arrays and Strings
PDF
Unit 2
C Programming Unit-3
introduction to strings in c programming
C-Arrays & Strings (computer programming).pptx
fundamentals of c programming_String.pptx
Arrays & Strings.pptx
Arrays and Strings
Unit 2

Similar to ARRAY's in C Programming Language PPTX. (20)

PPTX
C (PPS)Programming for problem solving.pptx
DOCX
Unitii classnotes
PPTX
Introduction to Arrays and Strings.pptx
PPTX
Chapter 1 Introduction to Arrays and Strings
PPT
PPTX
Lecture 15_Strings and Dynamic Memory Allocation.pptx
DOCX
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
PDF
Introduction to Arrays in C
PPTX
Array , Structure and Basic Algorithms.pptx
PPTX
C++ lecture 04
PPT
Strings
PPTX
programming for problem solving using C-STRINGSc
PDF
Arrays-Computer programming
PDF
Arrays and library functions
PPTX
3.ArraysandPointers.pptx
PPTX
Lecture_on_string_manipulation_functions.pptx
PDF
Unit ii data structure-converted
DOCX
Array &strings
PPS
C programming session 04
C (PPS)Programming for problem solving.pptx
Unitii classnotes
Introduction to Arrays and Strings.pptx
Chapter 1 Introduction to Arrays and Strings
Lecture 15_Strings and Dynamic Memory Allocation.pptx
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
Introduction to Arrays in C
Array , Structure and Basic Algorithms.pptx
C++ lecture 04
Strings
programming for problem solving using C-STRINGSc
Arrays-Computer programming
Arrays and library functions
3.ArraysandPointers.pptx
Lecture_on_string_manipulation_functions.pptx
Unit ii data structure-converted
Array &strings
C programming session 04
Ad

More from MSridhar18 (13)

PDF
Linked List - Part - 2 Linked List - Part - 2
PDF
Singly Linked List - Singly Linked List - Part -1
PDF
CONCEPT OF ARRAY IN DATA STRUCTURES CONCEPT OF ARRAY IN DATA STRUCTURES
PDF
unit 1 ds.INTRODUCTION TO DATA STRUCTURES
PPT
Data Warehouse Introduction to Data Warehouse
PPT
Cluster Analysis K-Means Clustering Typically measured by Euclidean distance
PPT
Classification and Cluster 2BCasic Concepts
PPT
Linked Lists: A Comprehensive Guide Advantages, various types, and fundamenta...
PPT
Data Structures: A Foundation for Efficient Programming
PPTX
DECISION MAKING AND BRANCHING - C Programming
PPTX
Fundamentals of computers - C Programming
PPTX
POINTERS AND FILE HANDLING - C Programming
PPTX
USER DEFINE FUNCTION AND STRUCTURE AND UNION
Linked List - Part - 2 Linked List - Part - 2
Singly Linked List - Singly Linked List - Part -1
CONCEPT OF ARRAY IN DATA STRUCTURES CONCEPT OF ARRAY IN DATA STRUCTURES
unit 1 ds.INTRODUCTION TO DATA STRUCTURES
Data Warehouse Introduction to Data Warehouse
Cluster Analysis K-Means Clustering Typically measured by Euclidean distance
Classification and Cluster 2BCasic Concepts
Linked Lists: A Comprehensive Guide Advantages, various types, and fundamenta...
Data Structures: A Foundation for Efficient Programming
DECISION MAKING AND BRANCHING - C Programming
Fundamentals of computers - C Programming
POINTERS AND FILE HANDLING - C Programming
USER DEFINE FUNCTION AND STRUCTURE AND UNION
Ad

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PPTX
Pharma ospi slides which help in ospi learning
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Trump Administration's workforce development strategy
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
Pharma ospi slides which help in ospi learning
Weekly quiz Compilation Jan -July 25.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Chinmaya Tiranga quiz Grand Finale.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Trump Administration's workforce development strategy
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study

ARRAY's in C Programming Language PPTX.

  • 2. 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];
  • 3. 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.
  • 4. 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};
  • 5. 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; }
  • 6. 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.
  • 7. 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}};
  • 8. 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; }
  • 9. 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;
  • 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}}; ...};
  • 11. 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; }
  • 12. 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";
  • 13. 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";
  • 14. 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'};
  • 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 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; }
  • 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 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; }
  • 19. 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.
  • 20. 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 }
  • 21. 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; }
  • 22. 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.
  • 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; }