Skip to Main Content
Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C)
book

Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C)

by Zed A. Shaw
July 2015
Intermediate to advanced content levelIntermediate to advanced
380 pages
10h 15m
English
Addison-Wesley Professional
Content preview from Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C)

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern C for Absolute Beginners: A Friendly Introduction to the C Programming Language

Modern C for Absolute Beginners: A Friendly Introduction to the C Programming Language

Slobodan Dmitrović

Publisher Resources

ISBN: 9780133124385Purchase Link