Strlen

From aldeid
Jump to: navigation, search

Syntax

size_t strlen ( const char * str );

Description

Returns the length of the C string str.

The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

This should not be confused with the size of the array that holds the string. For example:

char mystr[100]="test string";

defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.

In C++, char_traits::length implements the same behavior.

Parameters

str
C string.

Return Value

The length of string.

Example

0x400536 <main>:	push   rbp
0x400537 <main+1>:	mov    rbp,rsp
0x40053a <main+4>:	sub    rsp,0x20
0x40053e <main+8>:	movabs rax,0x6f6c6c6548          ; 'Hello'
0x400548 <main+18>:	mov    QWORD PTR [rbp-0x20],rax
0x40054c <main+22>:	mov    QWORD PTR [rbp-0x18],0x0
0x400554 <main+30>:	mov    DWORD PTR [rbp-0x10],0x0
0x40055b <main+37>:	lea    rax,[rbp-0x20]
0x40055f <main+41>:	mov    rdi,rax
0x400562 <main+44>:	call   0x400400 <[email protected]>     ; strlen('Hello')
0x400567 <main+49>:	mov    rdx,rax                   ; return 5
0x40056a <main+52>:	lea    rax,[rbp-0x20]
0x40056e <main+56>:	mov    rsi,rax
0x400571 <main+59>:	mov    edi,0x400614
0x400576 <main+64>:	mov    eax,0x0
0x40057b <main+69>:	call   0x400410 <[email protected]>     ; "%s is %d chars long\n", s, strlen(s)
0x400580 <main+74>:	mov    eax,0x0
0x400585 <main+79>:	leave  
0x400586 <main+80>:	ret