Home>Article>Backend Development> How to input array in C language?

How to input array in C language?

little bottle
little bottle Original
2019-05-07 10:42:34 104522browse

How to input array in C language?

In C language, there are two functions that allow users to input array data from the keyboard, they aregets()andscanf(). scanf() inputs an array string through the format control character %s. In addition to strings, it can also input other types of data; gets() inputs an array string directly and can only input strings.

However, there is a difference between scanf() and gets().

scanf()When reading a string, it is separated by spaces. When a space is encountered, the current string is considered to have ended, so a string containing spaces cannot be read.

gets()Considering that spaces are also part of the string, the string input is considered to end only when the Enter key is encountered. Therefore, no matter how many spaces are entered, as long as the Enter key is not pressed The Enter key is a complete string for gets(). In other words, gets() is used to read an entire line of string.
For example:

#include  int main(){ char str1[30] = {0}; char str2[30] = {0}; char str3[30] = {0}; //gets() 用法 printf("Input a string: "); gets(str1); //scanf() 用法 printf("Input a string: "); scanf("%s", str2); scanf("%s", str3); //分别输出 printf("\nstr1: %s\n", str1); printf("str2: %s\n", str2); printf("str3: %s\n", str3); return 0; }

Running result:

Input a string: C C++ Java Python Input a string: PHP JavaScript str1: C C++ Java Python str2: PHP str3: JavaScript

The first input string is fully read by gets() and stored in str1. The first half of the second input string is read by the first scanf() and stored in str2, and the second half is read by the second scanf() and stored in str3.
Note that scanf() needs the address of the data when reading data. This is constant, so for variables of int, char, float and other types, you must add & in front to obtain their addresses. . But in this code, we only gave the name of the string, but did not add & in front. Why is this? Because string names or array names are generally converted into addresses during use, adding & is unnecessary and may even lead to errors.
As far as the knowledge learned so far is concerned, variables of int, char, float and other types must be preceded by & when used in scanf(), while arrays or strings do not need to be added in front when used in scanf(). They itself will be converted into an address. Readers must keep this in mind.
As for the details of the conversion of array names (string names) and addresses, and when array names will be converted to addresses.

The above is the detailed content of How to input array in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn