Getchar : Reading or Accepting String Character by Character

January 14, 2010 No Comments » Hits : 200





getchar : Reading or Accepting String From User in C

  • Reads String Character by Character

Syntax for Accepting String :

  • ch = getchar();   // getchar accepts character & stores in ch

Example :

  1. ch = getchar() ;
  2. name[0] = getchar();

Live Example : : For Accepting String (Use One of the Loop)

#include<stdio.h>

void main()
{
int i = 0;
char name[20]; 

printf("\nEnter the Name : "); 

while((name[i] = getchar())!='\n')
        i++ ;

getch();
}

Rules / Facts : 

  1. getchar accepts string Character by character
  2. Terminating Condition Should be specified
  3. Here Terminating Condition is : n (i.e Loop will be continue until new line character is entered )

Incoming search terms: