0

I came across the following code in Ritchie and kernighan C,for counting no. of words ..

#include<stdio.h>
#define IN 1
#define OUT 0
main()
{
    int c,n1,nw,nc,state;
    state = OUT;
    n1 =nw = nc = 0;
    while((c = getchar())!=EOF)
    {
        ++nc;
        if(c == '\n')
            ++n1;
        if(c == ' '||c == '\n' ||c == '\t')
            state = OUT;
        else if(state == OUT)
        {
            state = IN;
            ++nw;
        }
    }

    printf("%d %d %d\n",n1,nw,nc);
}

I guess here c == ' ' and c == '\t' are doing the same job.

Can someone explain me difference between tab, space, whitespace, blank, form feed and vertical tab?

alk
  • 69,737
  • 10
  • 105
  • 255
lazarus
  • 677
  • 1
  • 13
  • 27
  • 1
    Hint: tabs, spaces and blanks in C are used for code formatting :-) – Sergey Kalinichenko Jun 27 '14 at 16:11
  • VT (vertical tab) is explained in great detail [right here](http://stackoverflow.com/questions/3380538/what-is-a-vertical-tab) – fvu Jun 27 '14 at 16:24
  • Don't you use tabs ...! ;-) @dasblinkenlight – alk Jun 27 '14 at 17:03
  • The formal and essential difference is all those characters are represented by a different integer value, the so called ASCII code. An exception are "space" and "blank" which are the same. – alk Jun 27 '14 at 17:06

3 Answers3

3

Spaces and tabs have different representations in ASCII. <space> is 0x20, while <tab> is 0x09. When the program checks the current character, both possibilities need to be tested.

Also worth noting is that the newline character they are using is '\n', which is "Line Feed", the conventional newline character for Unix/Linux/BSD. On Windows, the typical newline is represented by "\r\n" or CRLF ("Carriage Return" and "Line Feed").

I don't know that characters like "vertical tab" are used much. Many of those "control characters" go back to the days when they were used to give printers instructions on how to move the head.

mathdan
  • 181
  • 1
  • 6
1

They have different internal codes and meanings. For example '\t' has internal code equal to 9 while space ' ' has internal code 32 in ASCII and 64 in EBCDIC. Some programs can substitute tab for some number of spaces. Try for example the following code

#include <stdio.h>

int main()
{
    printf( "From here" " " "to here\n" );
    printf( "From here" "\t" "to here\n" );
}

and compare the output of the two calls of printf.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Both Space and whitespace have the same meaning. Vertical(dec 11) and horizontal(dec 9) tab depicts their meaning itself. These are the characters are used most while file formatting. Consider the code below.

#include<stdio.h>

int main()
{
 int i =0;
 int a[] = {97,32,98,9,65,10,66,11,67};
 while(i<9)
 printf("%c",a[i++]);
 return 0;
}

Above code will give you some rough idea.

However, it's good to check the condition for horizontal tab while checking for space.

vpatial
  • 33
  • 3