Name: Anonymous 2010-09-02 0:50
should i read k&r
'\n' is a char, which in C is just an integer-type, and therefore yields an integer-value when being evaluated.'\n' is "on the other hand a string constant that happens to contain only one character." is just wrong. the only string-constant that only contains one charactar in C is "", which contains '\0' as its only element of type char.
'\0', not /n (whatever that means, anyway).char is not guaranteed to be one byte long (the only thing the standard guarantees is that it can take at least 255 different values), and nobody is saying it's ``equal to an int''.chars with a terminating '\0' (which I assume is what you meant when you said ``/n'').#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c = '\t') // Should be c == '\t'
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}char is the smallest addressable datatype in C (on your CPU, in fact) and therefore always equal to a byte!! What that means is, that a byte does not neccessarily contain 8 bits.sizeof (char) is defined to be 1?