MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Ways to handle strings

Let's make it an array.
The preceding section described the handling of character variables.
Simply put, if you gather a lot of these character variables together, you should get a string.
"Don't you ever collect a bunch of variables of the same type...?"

That's right.Creating an array of character variables essentially creates a string.
In C, an array of characters can be used as a string variable.

However, a question arises here.
It's about how to store the number of characters in a string.
It's difficult to extract the string when you don't know the number of characters.

In C, the length of a string is determined by storing a special value at the end of the string.
Sometimes these characters are specifically called EOS.
In C, '\0' is treated as the End Of String (EOS).'\0' represents the null character, which has a numerical value of 0.

キーワード
【EOS】

文字列の終わりを表すsymbol。終端文字とも呼ばれる。
なお、End of String の頭文字をとっている。


If you can create a string, it's easy to display it.
The `printf` function can display strings using the `%s` specifier.

With this understanding, you can handle strings.
The following program is an example of storing and displaying a string in an array of character variables.

Source code
#include <stdio.h>

int main(void)
{
    char str[6] = { 'M', 'A', 'R', 'I', 'O', '\0' };
    printf("%s\n", str);
    return 0;
}

The output of this program is as follows.

Execution results
MARIO

As you can see from this program, an EOS is placed at the end of each string.
The number of elements in the array needs to be one more than the number of characters you intend to store.
Of course, specifying a larger number of elements is fine.

If the string doesn't have an EOS, it will be treated as a string up to the end of the array.
Until an EOS (end-of-sequence) token is encountered somewhere.
I'm really frustrated with the string just endlessly displaying.

If elements are omitted during array initialization, the remaining positions will be filled with zeros.

Source code
char str[6] = { 'M', 'A', 'R', 'I', 'O' };

Even so, a zero will be inserted, and that will be the EOS, so it's fine.
However, I wrote it this time with the deliberate inclusion of an EOS at the end.
"Note that you can omit the number of elements when writing EOS."
String initialization
In the previous section, we explained how to handle strings using a char array.
However, the method of assigning strings as done in the previous section is quite cumbersome.
I have to write it out character by character.

"In C, there's a more intuitive way to initialize strings."
"We've been enclosing strings in double quotes so far, but here's how to use them."
Sometimes, strings enclosed in "" are specifically referred to as string literals.

String literals make it easy to initialize strings.
The following program is an example of rewriting the previous program as a string literal.

Source code
#include <stdio.h>

int main(void)
{
    char str[] = "MARIO";
    printf("%s\n", str);
    return 0;
}

The output of this program is as follows.

Execution results
MARIO

With this method, you only need to write the string directly, and you can omit the element count.
In this case, the number of elements is allocated one more than the length of the string.
You can also specify it if you want to reserve extra space.

The problem with this approach is that it can only be used during initialization.
If you want to assign strings later, you have no choice but to assign them one by one.
The following program demonstrates an example of assigning values to elements one by one.

Source code
#include <stdio.h>

int main(void)
{
    char str[6];
    str[0] = 'M';
    str[1] = 'A';
    str[2] = 'R';
    str[3] = 'I';
    str[4] = 'O';
    str[5] = '\0';
    printf("%s\n", str);
    return 0;
}

The result will be the same as before.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19