strlen(c)输出替什么是7

strlen(c)输出为什么是7

#include<string.h>
#include<stdio.h>
int main()
{
char c[] = {'a', 'b'};
//int c[] = {1, 2, 3};
printf("%d\n", sizeof(c));
printf("%d", strlen(c));
}

------解决方案--------------------
引用:

#include<string.h>
#include<stdio.h>
int main()
{
char c[] = {'a', 'b'};
//int c[] = {1, 2, 3};
printf("%d\n", sizeof(c));
printf("%d", strlen(c));
}

没有结束符,即‘\0’
------解决方案--------------------
格strlen是以遇到'\0'为结束符。你的字符数组没有定义这个'\0'值,所以它会一直找下去,直到找到或者访问了不该访问的内存,造成内存溢出报错。而你的代码刚好是第七个的内存值就是'\0'。仅此而已
------解决方案--------------------
运气好第七个就是‘\0’
------解决方案--------------------
C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC\STRLEN.C
/***
*strlen.c - contains strlen() routine
*
*       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
*       strlen returns the length of a null-terminated string,
*       not including the null byte itself.
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>

#ifdef _MSC_VER
#pragma function(strlen)
#endif  /* _MSC_VER */

/***
*strlen - return the length of a null-terminated string
*
*Purpose:
*       Finds the length in bytes of the given string, not including
*       the final null character.
*
*Entry:
*       const char * str - string whose length is to be computed
*
*Exit:
*       length of the string "str", exclusive of the final null byte
*
*Exceptions:
*
*******************************************************************************/

size_t __cdecl strlen (
        const char * str
        )
{
        const char *eos = str;

        while( *eos++ ) ;

        return( (int)(eos - str - 1) );
}