Skip to content

kmajhi/character_Arrays_and_strings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

Character Arrays & Strings

In C, character arrays are commonly used to represent strings. A string in C is an array of characters terminated by a null character (`'\0'`). Here's an example that demonstrates the usage of character arrays as strings:

#include <stdio.h>

int main() {
    char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    char str2[] = "world";

    printf("str1: %s\n", str1);
    printf("str2: %s\n", str2);

    return 0;
}

In this example, we declare two character arrays str1 and str2 to represent strings.

  • str1 is initialized explicitly with individual characters and a null character at the end. The size of the array is explicitly specified as 6 to accommodate the characters and the null character.
  • str2 is initialized using a string literal enclosed in double quotes. In this case, the size of the array is automatically determined based on the number of characters in the string literal (including the null character).

Both str1 and str2 can be treated as strings and used with various string manipulation functions in C, such as strlen, strcpy, strcat, etc.

About

Character Arrays and Strings

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages