I'm practicing my C coding and I wanted to implement my own version of the getline function in C for learning purposes. I'd like a review on coding style, correctness, improvements I can make for performance, and overall quality of the code. My main concerns are correctness and performance (in that order).
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_LINE_LENGTH 255
/* Retrieves a line of text from the stream provided
* and places it into @buf until a new line character is
* reached or the number of characters read is > @size - 1.
* This function will null-terminate the provided buffer.
*
* @param[in] -- stream -- the stream
* @param[in] -- buf -- a buffer big enough for @size chars.
* @param[in] -- size -- the maximum number of chars to read (must
* include room for a null terminator
* @return -- the number of characters read from the stream.
*/
size_t getline(FILE *stream, char *buf, size_t size)
{
size_t count = 0;
char c;
while ((c = (char)getc(stream)) != '\n' && count < size - 1) {
buf[count++] = c;
}
buf[count] = '\0';
return count;
}
int main()
{
char line[MAX_LINE_LENGTH];
while (true) {
size_t count = getline(stdin, line, MAX_LINE_LENGTH);
printf("The line gotten was \"%s\" and was %zu chars long.\n", line, count);
}
}