Course Content
C Basics
C Basics
Char and String
In the conclusion of our last lesson, we hinted at the idea of using arrays to handle text.
In C, there's no specific data type designed solely for strings (or text). Instead, we utilize an array of the char type, where each element in the array holds a single character. So, the text "c<>definity"
would be represented as:
You might have realized that the text "c<>definity"
contains only 11 characters, yet the array boasts 12 elements.
Note
Think about it: every word you know is just a combination of letters.
The \0 character
This character, \0
, signals the end of a string in C. Don't confuse it with the numeral zero; it's a unique, non-printable character akin to \n
. In the ASCII chart, this character has the code 0.
An additional array cell is reserved for this character.
Whenever you're working with text, ensure that the array holding the text is sized one element more than the actual text length.
Note
Remember, even a space is a character and it requires its own spot in the array. For example:
char array[10] = { 'u','s','e',' ','s','p','a','c','e','\0' }
.
Char vs. String
It's crucial to recognize that 'x'
and "x"
aren't the same thing.
A string consists of characters. Always remember to cap off a string with the \0
character.
'x'
is simply a character constant of the char type;"x"
is a string, an array of char values, or, in this case, {'x', '\0'}.
Note
Make sure you don't mix up
'\0'
,'0'
, and"0"
. The first is a character literal representing the character with ASCII code 0 (NULL
). The second is a character literal for the digit 0, with an ASCII code of 48. The third is a string consisting of two characters: the digit 0 and the string terminator,\0
.
String Output
In C, when you want to display strings (arrays of char values), you use the %s
specifier. This specifier guides the printf()
function on what type of information it should expect. By using %s
, we inform the printf()
function that it will be receiving a string.
main
#include <stdio.h> int main() { char array[] = { 'c','<','>','d','e','f','i','n','i','t','y','\0' }; printf("%s\n", array); // use %s format specifiers return 0; }
The C language includes a standard library for string manipulation, <string.h>
.
Within this library, there's a function that counts the number of characters in a string:
main
#include <stdio.h> #include <string.h> int main() { char array[] = { 'c','<','>','d','e','f','i','n','i','t','y','\0' }; printf("%d\n", strlen(array)); return 0; }
Let's dive into an illustrative example:
main
#include <stdio.h> int main() { char str[] = "c<>definity"; printf("%c\n", str[0] ); return 0; }
Interestingly, by leveraging the index, we access the array's initial element. Essentially, at their core, strings are just char-type arrays.
Thanks to array properties, you can alter specific string elements using indices:
Main
#include <stdio.h> int main() { char str[] = "c<>definity"; str[5] = '#'; // change the sixth character `f` to `#` printf("%s\n", str); return 0; }
Thanks for your feedback!