一行代码实现strlen

1 一行代码实现strlen

当然是通过递归实现,代码如下:

#include 
#include 

int strlen(const char* str)
{
    return assert(str), (*str ? strlen(str + 1) : 0);
}

int main()
{
    printf("len : %d\n", strlen("Hello"));
    return 0;
}

当传进去的指针为空时,运行结果如下:

Assertion failed!

Program: C:\Users\Administrator\Documents\build-strlen-Desktop_Qt_5_9_4_MinGW_32bit-Debug\debug\strlen.exe
File: ../strlen/main.c, Line 6

Expression: str

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

你可能感兴趣的:(C)