C language content

In C programming, character arrays and strings are fundamental concepts that allow you to store and manipulate sequences of characters. Here’s a detailed overview:

Character Arrays

A character array is a sequence of characters stored in contiguous memory locations. In C, a character array can be used to store a string, but it can also store any other characters.

Declaration and Initialization

1. Declaration:

				
					char arrayName[arraySize];

				
			

2. Initialization:

  • Without specifying the size:
				
					char name[] = {'H', 'e', 'l', 'l', 'o'};

				
			
  • With size and initialization:
				
					char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Note the null terminator

				
			

String Functions

The C standard library provides a set of functions to manipulate strings. Here are some commonly used functions:

  1. strlen: Returns the length of the string.
				
					size_t strlen(const char *str);

				
			

2. strlen: Returns the length of the string.

				
					char *strcpy(char *dest, const char *src);

				
			

3. strncpy: Copies a specified number of characters from one string to another.

				
					char *strncpy(char *dest, const char *src, size_t n);

				
			

4. strcat: Concatenates two strings.

				
					char *strcat(char *dest, const char *src);

				
			

5. strncat: Concatenates a specified number of characters from one string to another.

				
					char *strncat(char *dest, const char *src, size_t n);

				
			

6. strcmp: Compares two strings.

				
					int strcmp(const char *str1, const char *str2);

				
			

7. strncmp: Compares a specified number of characters of two strings.

				
					int strncmp(const char *str1, const char *str2, size_t n);

				
			

8. strchr: Finds the first occurrence of a character in a string.

				
					char *strchr(const char *str, int c);

				
			

9. strrchr: Finds the last occurrence of a character in a string

				
					char *strrchr(const char *str, int c);

				
			

10. strstr: Finds the first occurrence of a substring in a string.

				
					char *strstr(const char *haystack, const char *needle);

				
			

Examples

  1. Character Array:
				
					#include <stdio.h>

int main() {
    char name[] = {'H', 'e', 'l', 'l', 'o', '\0'};
    printf("Name: %s\n", name);
    return 0;
}

				
			

2. String:

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

int main() {
    char str1[20] = "Hello";
    char str2[] = "World";
    
    // Concatenate str2 to str1
    strcat(str1, str2);
    
    printf("Concatenated String: %s\n", str1);
    printf("Length of String: %lu\n", strlen(str1));
    
    return 0;
}

				
			

Memory Considerations

  • When using character arrays, ensure that there is enough space to accommodate the string plus the null terminator.
  • Strings created using pointers to string literals are read-only. Modifying them can lead to undefined behavior.

Conclusion

Character arrays and strings are vital in C programming for handling text. Proper understanding and use of these constructs, along with string manipulation functions, are essential for efficient and error-free coding.

Scroll to Top