User:Fabien Labeyrie/C loops

From XPUB & Lens-Based wiki
< User:Fabien Labeyrie
Revision as of 00:39, 8 November 2010 by Fabien Labeyrie (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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;
}