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 12. Sizes and Arrays

In the last exercise, you did math but with, a '\0' (nul) character. This may seem odd if you’re coming from other languages, since they try to treat strings and byte arrays as different beasts. C treats strings as just arrays of bytes, and it’s only the different printing functions that recognize a difference.

Before I can really explain the significance of this, I have to introduce a couple more concepts: sizeof and arrays. Here’s the code we’ll be talking about:

ex12.c

 1   #include <stdio.h>  2  3   int main(int argc, char *argv[])  4   {  5       int areas[] = { 10, 12, 13, 14, 20 };  6       char name[] = "Zed";  7       char full_name[] = {  8           'Z', 'e', 'd',  9            ...

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