Find a String in an Array This program is used to get names and numbers of N person, print out the numbers of a person. Please help me improve this code.

Additional Question:

To avoid a very long name that exceeds char's size, I limit it to 255. Is there a better way?

To avoid buffer, I included a space before % sign when using scanf and not use getchar(). Is there a better way?

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

// Limiting string input
const unsigned int STRINGCONSTRAINT = 255;

// Person struct to store name and numbers
typedef struct {
  char name[STRINGCONSTRAINT];
  char numbers[STRINGCONSTRAINT];
} person;

void getNameNumber(person people[], int size);
void printNumber(person people[], int size, char personFind[]);

// Asked for array's size and a person's name to find
int main(void) {
  unsigned int size = 0;
  printf("Enter the amount of people: ");
  scanf(" %d", &amp;size);

  person people[size];
  getNameNumber(people, size);

  char personFind[STRINGCONSTRAINT];
  puts("Who do you want to find?");
  printf("Person to find: ");
  scanf(" %[^\n]s", personFind);

  printNumber(people, size, personFind);

  return 0;
}

// Get name and numbers for the array
void getNameNumber(person people[], int size) {
  unsigned int i;
  for (i = 0; i &lt; size; i++) {
    printf("Name: ");
    scanf(" %[^\n]s", people[i].name);
    printf("Numbers: ");
    scanf(" %[^\n]s", people[i].numbers);
  }
}

// Find the person and print the numbers
void printNumber(person people[], int size, char personFind[]) {
  unsigned int i;
  int flag = 1;

  for (i = 0; i &lt; size; i++) {
    flag = strcmp(people[i].name, personFind);
    if (flag == 0) {
      puts("Found!");
      printf("You can call %s at %s.\n", personFind, people[i].numbers);
    }
  }
}
static functions

Both of your getNameNumber and printNumber should be made static, because I doubt that you plan to export them.

Simpler functions

printf("Enter the amount of people: ");


can simply use puts, which doesn't have any formatting code in it. You use puts below, so this would also help with consistency.

Stack allocation

person people[size];


is not available in all versions of C, since size is not a constant. For best portability you'd want to malloc this, or perhaps alloca if you feel strongly that the memory should be in the stack and not on the heap.

const

printNumber's array arguments should be const, since you don't mutate them.

Your questions


  To avoid a very long name that exceeds char's size, I limit it to 255. Is there a better way?


For your use case, not really. A fixed array is the most appropriate. However, your code is vulnerable to overflows. You should be putting a width in your format specifier.


  To avoid buffer, I included a space before % sign and not use getchar(). Is there a better way?


I'm not sure which usage of % you're referring to, but they all look fairly sane, so no; there's not really a better way.