Read a line and stop at \n - C String

C examples for String:String Console Input

Description

Read a line and stop at \n

Demo Code

#include <stdio.h>

#define MAXLINE 1000/*from w  w  w.ja  v  a  2  s . com*/

int main(void)
{
    char line[MAXLINE];
    int c, i;

    i = 0;
    while (i < MAXLINE - 1) {
        if ((c=getchar()) == EOF)
            break;
        line[i++] = c;
        if (c == '\n')
            break;
    }
    line[i] = '\0';

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

    return 0;
}

Related Tutorials