MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

How to handle strings

Let's make it an array.
In the previous section, the handling of character variables was explained.
Simply put, if you collect many of these character variables, you should end up with a string.
Collecting many variables of the same type...where have you heard that before?

That's right. If you create an array of character variables, that is, a string.
Arrays of character variables can be used as string variables in C.

However, one question arises here.
That is, how to store the number of characters in a string.
Without knowing the number of characters, it is difficult to retrieve the string.

In C, the number of characters is determined by storing a special value at the end of the string.
Such a character is sometimes specifically referred to as EOS.
In C, '\0' is treated as EOS.' \ɑ0' is equivalent to 0 as a number.

Keywords.
EOS

A symbol indicating the end of a string. Also called the terminating character.
The first letter of End of String.


Once a string can be created, it is easy to display it.
The printf function can be used to display strings using the %s specifier.

Once you know this much, you can work with 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 result of executing this program is as follows

Execution Result
MARIO.

As you can see in this program, the string ends with EOS, so
The number of elements in the array must be one more than the actual number of characters to be stored.
Of course, specifying a larger number of elements is not a problem.

If there is no EOS in the string, it will be treated as a string to the end of the array.
Until by chance we find 0 (EOS) somewhere.
The string will continue to be displayed endlessly, which is very annoying.

Note that if you omit an element when initializing the array, the remainder will be filled with zeros.

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

If the rest is 0, then there is no problem, because it is EOS.
However, in this case, I dared to write it to show that there is an EOS at the end.
Note that if EOS is written, the number of elements can be omitted.
String initialization
In the previous section, we explained that arrays of type char can handle strings.
However, the method of assigning strings performed in the previous section is very cumbersome.
Each character must be separated and written one by one.

In C, there is a more intuitive way to initialize strings.
Until now, strings have been enclosed in "" characters, but this method can be used.
Strings enclosed in "" are sometimes referred to as string literals.

String literals can be used to easily initialize strings.
The following program is an example of rewriting the program in the previous section using string literals.

Source Code
 #include <stdio.h>

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

The result of executing this program is as follows

Execution Result
MARIO.

In this method, you can simply write the string normally and omit the number of elements.
In this case, the number of elements is reserved one more than the length of the string, but
If you want to reserve more space, you can specify it.

The problem with this method is that it can only be used during initialization.
If you want to assign strings later, you have to assign them to the elements one by one.
The following program is an example of assigning one element at a time.

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 execution result will be the same as before.


About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box.