Joseph Sanoyo
8/13/2011 7:28:00 AM
On Aug 13, 3:03 am, "io_x" <a...@b.c.invalid> wrote:
> "Joseph Santoyo" <josephsant...@gmail.com> ha scritto nel messaggionews:4a72458c-aad4-4fa5-b3d5-9692eb2e470a@w18g2000yqc.googlegroups.com...
>
>
>
>
>
>
>
>
>
> > Is there any way to write this code without an else if? If so how..and
> > can you explain it line by line please :/ I'm really new to C or
> > programming.
>
> > #include <stdio.h>
>
> > /* count blanks, tabs and newlines in input */
> > main()
> > {
> > int c, bl, t, nl;
>
> > c = 0;
> > bl = 0;
> > t = 0;
> > nl = 0;
> > while ((c = getchar()) != EOF)
> > if (c == ' ')
> > ++bl;
> > else if (c == '\t')
> > ++t;
> > else if (c == '\n')
> > nl;
> > printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
> > }
>
> below it is the need to add "{}" because the many if
>
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> int main(void)
> {int c, bl, t, nl;
>
> bl=0; t=0; nl=0;
> while((c=getchar())!=EOF)
> {if(c== ' ' ) ++bl;
> if(c== '\t') ++t;
> if(c== '\n') ++nl;
> }
> printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);}
>
> ---------------------------
> or
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> int main(void)
> {int c, bl, t, nl;
>
> bl=0; t=0; nl=0;
> while((c=getchar())!=EOF)
> {bl+=(c== ' ' );
> t +=(c== '\t');
> nl+=(c== '\n');
> }
> printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
>
> }
>
> don't know if is garantee that if c==' ' it return 1 and not 90
> it seems yes because i seen it here
Is it just as valid to put the ++bl, ++t, ++nl below the ifs?