Gets() function reads a string and stores them at the address pointed to by its argument. - C String

C examples for String:String Console Input

Introduction

You can type characters until a carriage return.

The carriage return does not become part of the string. A null terminator is placed at the end.

To read a carriage return use getchar().

By using gets_s() you can correct typing mistakes by using the backspace key before pressing ENTER.

The prototype for gets_s() is

char *gets(char *str);

where str is a pointer to a character array. gets_s() also returns str.

The following program reads a string into the array str and prints its length:

Demo Code

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

int main(void)
{
   char str[80];//ww w  . j a  va2 s .  c  o  m

   gets_s(str);
   printf("Length is %d", strlen(str));

   return 0;
}

Result


Related Tutorials