C - Fill char array by fgets() function

Introduction

In the following code the char array firstname can hold 15 characters, plus 1 for the \0 at the end of the string.

Demo

#include <stdio.h> 

int main() //from  w  w w . ja  va 2  s  .  com
{ 
      char firstname[16]; 

      printf("What is your name? "); 
      fgets(firstname,16,stdin); 
      printf("Pleased to meet you, %s\n",firstname); 
      return(0); 
}

Result

fgets() function reads in data for the firstname string.

The maximum input size is set to 16 characters, which already accounts for the null character.

The text is read from stdin, or standard input.

Related Topic