ECE 1620 - Etter - Section 6.6



6.6 Character Arrays (Strings)

String constants use double quotes, compiler supplies terminating zero byte:

  printf( "abc");
"abc" is an array of 4 characters: 'a', 'b', 'c', 0

Equivalent printf statement using a character array variable:

  printf( msg);
msg could be declared and initialized various ways:
      char msg[] = "abc";
or:
      char msg[4] = "abc";
or:
      char msg[] = { 'a', 'b', 'c', 0 };
or:
      char msg[4];
      ...
      strcpy( msg, "abc");    /* for strcpy(), #include <string.h> */
Other useful functions from string.h:
  strlen                n = strlen( str);
  strcat                strcat( s1, s2) ;    /* append s2 to s1 */
  strcmp                n = strcmp( s1, s2); /* compare, n=0 if equal */
Simple implementation of strlen:
  int my_strlen( const char s[])
  {
    int len = 0;

    while( s[len] != 0)
      ++len;

    return len; /* s[len] == 0, for any string */
  }
chapter6_6.c - testing strlen(), strcpy(), strcat()