Assign String in C: Methods & Examples

In the C language, strings are typically represented using character arrays. Assigning a string can be done in several ways:

  1. Assign values using an array.
char str[10];
strcpy(str, "hello");
  1. Assign values using pointers.
char *str;
str = "hello";
  1. Assign values directly using a character array.
char str[] = "hello";

Please note that in C language, strings are terminated by a null character ‘\0’, so the size of the character array should be large enough to accommodate the string content as well as the terminating null character.

bannerAds