Get size of a vector of characters?

2

Vector example: char nome[10] = {'b', 'r', 'a', 's', 'i','l'};

First of all, although my vector is 10, I'm only using six, and I want to know how I get this length, which in the case would be 6. In summary how do I get the number of characters present in a string?

    
asked by anonymous 08.10.2015 / 05:41

1 answer

6

The definition

char nome[10] = {'b', 'r', 'a', 's', 'i', 'l'};

is equal to

char nome[10] = {'b', 'r', 'a', 's', 'i', 'l', 0, 0, 0, 0};

So you can use the function strlen()

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

int main(void) {
    char nome[10] = {'b', 'r', 'a', 's', 'i', 'l'};
    printf("%d elementos.\n", (int)strlen(nome));
    return 0;
}
    
08.10.2015 / 10:53