When handling a character string with the scanf function

Thank you for reading this article. I write it as a memorandum of my own, who started programming in earnest at the end of last year. I will write it in an easy-to-understand manner so that it can help other people as much as possible. If you make a mistake, please comment.


What to talk about in this article

  1. This is an overview of scanf.
  2. Explain why the "&" is unnecessary for the string (% s) in the scanf function.

1. Overview of scanf

Scanf is an important function that everyone goes through first when learning C language, and it is a "function that lets the user input characters and numbers". I'll leave the basic writing to other pages.

For example, the following code prompts the user for an integer and returns the integer entered.

scanf_1.c


#include <stdio.h>
int main(void) {
    int n;
    printf("Please enter the integer n\n");
    scanf("%d",&n);

    printf("The integer entered is%It is d.\n",n);
    return 0;
}

By the way, since we are dealing with integers now, the conversion specification is "% d", and "&" is added before the argument n.

If you want to enter a character string, set the conversion specification to "% s". However, do not put "&" before the argument n. Why is this?

2. Why you don't need "&" in string scanf

To think about this, let's first look at what the role of "&" is in the first place.

As anyone who knows pointers understands, "&" is an address operator. By prefixing the variable with "&", you can know the address of the memory where the value of the variable is stored.

tumblr_inline_nzji3wI8W21t83x5s_540.png

For example, if you typed "13" in the code above, of course the variable n will contain 13. Then, the address operator answers the question, "Where is the address (location) where 13 is stored?" (The memory address of the image is a 3-digit number for simplicity)

I'm not happy to know the name of the variable when I want to store the value in scanf. What you want to know is ** the location of the address to store **. So scanf requires &, right?

Now, I will explain why you don't need "&" when you use "% s".

The string is in the form of an array. I have prepared the following code to show this.

scanf_2.c


#include <stdio.h>
int main(void) {
    char text[50];
    printf("Please enter a string\n");
    scanf("%s",text);
     
    printf("The first character of the entered character string is%is c",text[0]);
    return 0;
}

A code that returns the first character (text [0]) of the entered character string. Let's make it correspond as before. The variable text [0] contains the first character. So how do you represent the address where this is stored?

& text [0] is also correct, but in this case there is another answer, and that is very important ...

In fact, the address of ** text [0] is ** pointed to by text!

…Hmm? Don't you know what it means? In other words, this is what it is!

tumblr_inline_nzji3wdyqQ1t83x5s_540.png

** The array name of the string refers to the address of the first character. ** Because of this property, "&" is unnecessary when "% s" is specified. Also, I often use an asterisk in the image, which is called "** indirect operator **", which is an operator for specifying an address and using its contents as a variable. In short, it is the opposite of the address operator.

If you understand this essence, you can see that the following code also holds.

scanf_3.c


#include <stdio.h>
int main(void) {
    char text[50];
    printf("Please enter a string\n");
    scanf("%s",text);
     
    printf("The first character of the entered character string is%is c",*text);
    return 0;
}

At printf, I'm calling the first character using an indirect operator. This also gives the same result as the previous code (both codes are buggy if you enter Japanese!)

3. Summary

  1. Since scanf needs to pass an address, it usually requires "&"
  2. When handling a character string with scanf, "&" is unnecessary because the array name points to the address of the first character.

We are looking for mistakes and opinions. Well then.

Recommended Posts