Espaços nominais
Variantes
Ações

strlen

De cppreference.com
< c | string | byte

<metanoindex/>

 
 
 
Cordas de terminação nula de bytes
Funções
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Manipulação personagem
Original:
Character manipulation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Conversões para formatos numéricos
Original:
Conversions to numeric formats
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Manipulação de cadeia
Original:
String manipulation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Exame String
Original:
String examination
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Manipulação de memória
Original:
Memory manipulation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Diversos
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
<tbody> </tbody>
Definido no cabeçalho <string.h>
size_t strlen( char *str );
Retorna o tamanho do string sem contar o carácter '\0' que termina o string.
Original:
Returns the length of the given byte string.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Parâmetros

str -
ponteiro para um string terminado com o carácter '\0' (nulo).
Original:
pointer to the null-terminated byte string to be examined
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Valor de retorno

O comprimento do string terminado com o carácter nulo.
Original:
The length of the null-terminated string str.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Exemplo

#include <stdio.h>
#include <string.h>

int main(void) {
    char mensagem[] = "Eu amo programar em C";
    size_t tamanho = strlen(mensagem);

    printf("A mensagem \"%s\" tem %lu letras.\n", mensagem, tamanho);

    // A função strlen() inicia a leitura no endereço de memória onde está o ponteiro 
    // e termina a leitura quando encontra o carácter nulo (NULL), que é representado 
    // por '\0'. Se nós movermos o ponteiro, que no momento aponta para o inicio da 
    // mensagem, três 'casas' para a direita, ele irá apontar para a letra 'a' da palavra 
    // amo. Quando pedirmos para a função strlen() retornar o tamanho do string, ela irá
    // retornar a contagem da letra 'a' até o caracter '\0' (este não é contado).
    printf("Agora a mensagem é: \"%s\" e tem %lu letras.\n", 
           mensagem+3, strlen(mensagem+3));
}

Saída:

A mensagem "Eu amo programar em C" tem 21 letras.
Agora a mensagem é: "amo programar em C" e tem 18 letras.

Veja também