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 ...

Get Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.