본문 바로가기
C 언어

C언어 getline 함수(개행문자 제외)

by treeCoder 2021. 12. 4.

 

#include <stdio.h>
#define ReadLine( x ) (getline((x), sizeof((x))))

/* getline:  get line into s, return length */
int getline(char s[], int lim) {

    int c, i;

    i = 0;
    while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
        s[i++] = c;
    if (c == '\n')
        s[i] = 0x00; //s[i++] = c;
    s[i] = '\0';

    return i;
}

int main() {

    char s[256];

    puts("What's your name?");

    int i = ReadLine(s); //getline(s, sizeof(s));
    printf("Hi, %s\n", s);
    printf("%d letters long.", i);

    return 0;
}

 

댓글