Skip to content

Instantly share code, notes, and snippets.

@freetstar
Created March 26, 2013 02:49
Show Gist options
  • Save freetstar/5242742 to your computer and use it in GitHub Desktop.
Save freetstar/5242742 to your computer and use it in GitHub Desktop.
test c string '\0'
/*
* =====================================================================================
*
* Filename: test.c
*
* Description:
*
* Version: 1.0
* Created: 03/26/2013 10:39:03 AM
* Revision: none
* Compiler: gcc
*
* Author: freetstar (http://www.freetstar.com), [email protected]
* Company:
*
* =====================================================================================
*/
#include <string.h>
int main(int argc, const char *argv[])
{
char str1[6]="123456";
char str2[2]={'2','1'};
char str3[7]="123456";
char str4[3]={'2','1','\0'};
printf("str1 %s\n",str1);
printf("str2 %s\n",str2);
printf("str3 %s\n",str3);
printf("str4 %s\n",str4);
return 0;
}
//output
str1 123456 „[乱码][乱码]
str2 21123456 „[乱码][乱码]
str3 123456
str4 21
//explanation
如果c语言里的一个字符串数组末尾没有'\0',这个可能是由于数组长度限制而导致的,会导致输出乱码,或者没有错误,或者诡异的段错误
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment