~/ String

19 August 2017

If you just follow this post the way I explain it, you won’t be confused.

But if you don’t, then you’re a true geek and you’ll be a better programmer than me in the end.

Array of chars or “string” is nothing but several letters followed by a ‘\0’ or a NUL terminator.

+---+---+---+----+
| A | B | C | \0 |
+---+---+---+----+

For defining or representing a char(acter), we put single-quotes around our letter but remember, if you’re using double-quotes instead, they’re no longer called char or letter and in fact, they’re already a string!

The following code will display a single letter on the screen:

#include <stdio.h>

int main(int argc, char *argv[]){

  printf("%c\n", 'A');

  return 0;
}

Now let’s replace the single quotes with the double quotes:

#include <stdio.h>

int main(int argc, char *argv[]){

  printf("%c\n", "A");

  return 0;
}

If you try to compile this code, depends on your compiler and your environment, you MUST get an error(well, a warning actually) about what you just did there.

(Always keep that in mind, strings are MADE OF chars but it does NOT mean that they’re interchangeable without any additional steps.)

In case of using single-quotes and defining a char:

+---+
| A |
+---+

And in case of using double-quotes and actually creating a string :

+---+----+
| A | \0 |
+---+----+

Oops!

Let’s create a string variable:

#include <stdio.h>

int main(int argc, char *argv[]){

  char my_string[] = "ABC";

  printf("%s\n", my_string);

  return 0;
}

Congrats! You’ve just created your very first(second) string in C!

Before we finish this post, I’d like to tell you that the length of a string is NOT, I repeat, IS NOT the same as its size!

Length is what YOU(as a human) can see and the size is amount of bytes that your string is occupying from your computer’s memory, or RAM.

To make it even more simpler, size(of a string) is the length+1 and that’s it.

To find the length of a string however, you can use the strlen function which is part of the <string.h> library but before doing so, I’d like to tell you that the very first line of your C codes(that “studio” dude) was nothing but “loading” the library for “Standard Input and Output” operations, which is, reading the data from a source(usually keyboard input, or stdin or displaying the stuff on the screen(terminal) which is usually, stdout.

Let’s find out the length of our string:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){

  char my_string[] = "ABC";

  printf("My string has %d characters in it.\n", strlen(my_string) );

  return 0;
}

And the size of your string, since that NUL terminator(or ‘\0’) occupies 1 byte is length + 1 or simply 4 bytes in this very case.

We’re done for now and if you’re interested in knowing more and feeling geek, find out what’s the difference between defining a string using square-brackets versus using an asterisk:

#include <stdio.h>

int main(int argc, char *argv[]){

  char *my_string = "ABC";

  printf("%s\n", my_string);

  return 0;
}

Also it’s good to try printing a specific character within your string, let’s say ‘B’ or any other “letters”.

Be happy