User:Fabien Labeyrie/C loops: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "Category:prototyping Category:2011_P1.01 __NOTOC__ ==C loops== ====fgets function==== The following code output the length of each line, but it actually outputs lines t...")
(No difference)

Revision as of 00:39, 8 November 2010


C loops

fgets function

The following code output the length of each line, but it actually outputs lines that are one character longer. It might be that the hard return character is taken into account.

#include "stdio.h"
#include "string.h"
#define MAXLINE 1000

int main () {
    int i=0;
    int linelen;
    char line[MAXLINE];
    while (fgets(line, MAXLINE, stdin)) {
        linelen = strlen(line);
        printf("line is %d characters long\n", linelen);
    }
    return 0;
}