O'Reilly logo

Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise 11. Arrays and Strings

This exercise shows you that C stores its strings simply as an array of bytes, terminated with the '\0' (nul) byte. You probably clued in to this in the last exercise since we did it manually. Here’s how we do it in another way to make it even clearer by comparing it to an array of numbers:

ex11.c

 1   #include <stdio.h>  2  3   int main(int argc, char *argv[])  4   {  5       int numbers[4] = { 0 };  6       char name[4] = { 'a' };  7  8       // first, print them out raw  9       printf("numbers: %d %d %d %d\n", 10               numbers[0], numbers[1], numbers[2], numbers[3]); 11 12       printf("name each: %c %c %c %c\n", 13               name[0], name[1], name[2], name[3]); 14 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required