当前位置:网站首页>C language removes line breaks (or other characters) at the end of strings

C language removes line breaks (or other characters) at the end of strings

2022-06-23 06:34:00 I have a magic box

void main()
{
    
	char now_mac[100];
	strcpy(now_mac, "abcd\n"); // \0 End of string (strlen Not counting length ),\n For newline (strlen count 1 Length )
	printf("%d\n", strlen(now_mac)); //  Output 5
	// now_mac[strlen(now_mac) - 1] = 0; //  take \n Directly replace with 0 The effect is the same 
	now_mac[strlen(now_mac) - 1] = '\0'; //  take \n Replace with \0
	printf("%d\n", strlen(now_mac)); //  Output 4
}

Reference link :
C Remove end of string newline ( Remove the last character of the string )

原网站

版权声明
本文为[I have a magic box]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230440469969.html