Using getchar() to input strings. - C Data Type

C examples for Data Type:char

Description

Using getchar() to input strings.

Demo Code

#include <stdio.h>

#define MAX 80//w  w  w  .j av  a 2  s  . c o  m

int main( void )
{
    char ch, buffer[MAX+1];
    int x = 0;

    while ((ch = getchar()) != '\n' && x < MAX)
        buffer[x++] = ch;

    buffer[x] = '\0';

    printf("%s\n", buffer);

    return 0;
}

Result


Related Tutorials