C ++ string of small details

C ++ string of problems

char character array
when stored into the corresponding characters will be broken down into a number of characters is stored into an array

char A[5] = {"H","e","l","l","o"};
//当想存Hello的时候在字符数组里会拆成一个个的字符生成,5为数组长度

When if you want to keep the string can be used * char
char * array of strings

char* A[3] = {"Hello","the", "same"};
/*当加上星号后,数组里存的不是Hello,the和same,而是这三个字符串
的第一个字符的地址,而这个字符串又是由字符数组组成的。因为在地址里
是连续的,所以能读取整个Hello,the,same,所以可以变相就变成了字
符串数组
*/

C ++ input character issues

scanf input character using a% c,% s,
when the% s

char str[10];
scanf("%s",str);
printf("%s",str);

Enter `

QAQ QAQ QAQ

display

QAQ

As the end of the string is space

Gets_s and discuss the differences of scanf and getchar

#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h>
int main()
{
	char a[10], b[10];
	char c, d;
	scanf("%s", a);
	printf("a中保存的字符串为:%s\n", a);

	c = getchar();
	printf("c中保存的字符为:%c ", c);
	//此行没有换行符检测是否接受了输入缓冲区的

	gets_s(b);
	printf("b中保存的字符串为:%s\n", b);

	d = getchar();
	printf("d中保存的字符为:%c", d);

	return 0;
}

Asdf enter input results obtained as df

Conclusion:
scanf identify a space or enter or tab as the end of the string, and the space is still in the input buffer in
getchar () can accept a single character it received scanf spaces \ n, so it's for the trip
gets_s acceptable space, tab it identifying enter (\ n) as the end flag.

The role of sscanf and sprintf

scanf(screen,"%d",&n);
print(screen,"%d",n);

scanf essentially will get the number of the input from the screen format is converted to% d n
Print essentially from the number n, to get it into a format to a screen of% d

sscanf(str,"%d",&n);
sprint(str,"%d",n);

sprintf screen can become a character array or a string array, the number corresponding to the input string array.
Similarly the number of scanf array of strings to export.

Published 19 original articles · won praise 4 · Views 502

Guess you like

Origin blog.csdn.net/qq_35050438/article/details/103201335