Sample


[br]8 [br]There are a lot of <a href="https://itszephyro.com/blog/learn-c/data-types/" rel="noopener" target="_blank">data types</a> in C, but we've only used int up to now. Another important data type in C is <strong>char</strong>. <code>char</code> is a <strong>1-byte integral type that represents a character</strong>. That means that a <strong>char variable can memorise only whole numbers</strong> from -127 to 127 and this numbers are then <strong>turned into characters</strong> using the <a href="http://www.asciitable.com/" rel="noopener" target="_blank">ASCII table</a>. As when you put more letters together you can make a word, when you put more characters together you make a string. <strong>A string is an array of characters that ends with a null terminator.</strong> A <strong>null terminator</strong> is a special character (<code>'\0'</code>) which marks the end of a string. So, this is a character array AND a string: [crayon-5d589f01bf7aa110038182/] while this is a character array BUT NOT a string: [crayon-5d589f01bf7b3256288463/] Another way to initialise strings - <em>which doesn't require to specify the null terminator, because it's added automayically</em> - is this: [crayon-5d589f01bf7b6883473715/] However, also in this case you must take into account the null terminator when you set the number of elements in the array - <em>as you can see I've written 4 and not 3</em>. <strong>Reading and writing strings is a bit more complex than for other data types</strong>, so if you want to learn some of the different ways you can do it, from the easiest to the most recommended one, click the link below. In this guide, we're going to use <code>printf</code> and <code>scanf</code> with the <code>%s</code> format specifier - <em>that I called special character in one of the previous sections, but special characters are the ones which begin with <code>\</code></em>. <i>Printing and reading character is the same as integers, there are no special rules as for strings. Just make sure to use <code>%c</code>, otherwise you'll get the character's numerical value.</i> <strong>To print a string</strong>, write: [crayon-5d589f01bf7b9416712450/] <strong>To read a string</strong>, use scanf <strong>without</strong> the <code>&</code> character: [crayon-5d589f01bf7bc364622086/] <i>Note: <code>scanf</code> can only read one word. To learn how to read a sentence, click the link below.</i> [br]https://itszephyro.com/blog/learn-c/strings-and-characters/[br]#include <stdio.h> int main() { char word[10]; printf("Enter a word (max 9 characters)"); scanf("%s", word); printf("You've entered: %s", word); } [br]

[br]8
[br]There are a lot of data types in C, but we’ve only used int up to now.

Another important data type in C is char.

char is a 1-byte integral type that represents a character.

That means that a char variable can memorise only whole numbers from -127 to 127 and this numbers are then turned into characters using the ASCII table.

As when you put more letters together you can make a word, when you put more characters together you make a string.

A string is an array of characters that ends with a null terminator.

A null terminator is a special character ('\0') which marks the end of a string.

So, this is a character array AND a string:

while this is a character array BUT NOT a string:

Another way to initialise strings – which doesn’t require to specify the null terminator, because it’s added automayically – is this:

However, also in this case you must take into account the null terminator when you set the number of elements in the array – as you can see I’ve written 4 and not 3.

Reading and writing strings is a bit more complex than for other data types, so if you want to learn some of the different ways you can do it, from the easiest to the most recommended one, click the link below.

In this guide, we’re going to use printf and scanf with the %s format specifier – that I called special character in one of the previous sections, but special characters are the ones which begin with \.

Printing and reading character is the same as integers, there are no special rules as for strings. Just make sure to use %c, otherwise you’ll get the character’s numerical value.

To print a string, write:

To read a string, use scanf without the & character:

Note: scanf can only read one word. To learn how to read a sentence, click the link below.

[br]https://itszephyro.com/blog/learn-c/strings-and-characters/[br]#include

int main() {
char word[10];
printf(“Enter a word (max 9 characters)”);
scanf(“%s”, word);
printf(“You’ve entered: %s”, word);
}
[br]

To learn more, visit: