hugo27 said:
(e-mail address removed) (Mazen S. Alzogbi) wrote in message
First note that OP's code did not initialize 'nc',
but assigned to it later.
"Initialize nc to an int" doesn't mean anything.
The statement:
double nc = 0;
initializes 'nc' with a 'double precision' floating point value of zero.
The statement;
nc = 0;
assigns a 'double precision' floating point value of zero to 'nc'.
The increment and decrement operators are valid for all
numeric types. They add and subtract the value one
from their operand.
I may be mistaken, but I think that's intentional.
The program will report how many characters were
input before EOF. Otherwise it would simply output
a sequence of numbers beginning with 1. Since OP
didn't state the code's intent, of course I'm only
guessing.
Depends what you mean by "OK". It's perfectly valid syntax,
but often not what the coder intended. I'm still waiting for
a mind-reading compiler.
Depends upon the intention. Sometimes all the desired
processing is done within the parentheses, e.g a function
to discard 'extraneous' input:
int c = 0;
while((c = getchar()) != EOF && c != '\n');
I do that sometimes, but I'll put the semicolon on a separate
line with a comment for clarity:
while((c = getchar()) != EOF && c != '\n')
; /* null statement */
Also note that statements (except preprocessor directives)
are not required to be separated by newlines, e.g. this is valid:
int main() { return 0; }
Also not all of a statement must appear on the same line:
for(
i = 0;
i <
10;
++i
)
{ printf("%d\n",
i)
;}
C's syntax is considered 'free form'.
It will execute if/when 'getchar()' returns EOF.
'nc' will be incremented every time 'getchar()' extracts
character from the standard input. So at 'for' loop
termination, it may be zero, or some greater value.
Try compiling and running the program. At the input prompt,
type in e.g. "Hello", and see what it does.
-Mike