Initialize part of an array of elements of type char with a string - C String

C examples for String:char array

Description

Initialize part of an array of elements of type char with a string

Demo Code

#include <stdio.h>

int main(void)
{
  char str[40] = "To be";

  printf("\nThe message is: %s", str);
  
  printf("The character \0 is used to terminate a string.");
  return 0;/*ww w  . ja  v  a 2s .  c  om*/
}

Result

The compiler will initialize the first five elements, str[0] to str[4], with the characters of the string constant, and str[5] will contain the null character, '\0'.

Space is allocated for all 40 elements of the array.


Related Tutorials