Get size from a character array?

Asked

Viewed 2,462 times

2

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

First of all, although my vector has 10 position I’m only using six, and I want to know how exactly I picked up this length, which in this case would be 6. In short how I get the number of characters present in a string?

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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.