Q:

getchar() function is not working

#include <stdio.h>

int main(int argc, const char * argv[])
{
    long nc;

    nc=0;
    while (getchar()!=EOF) {
        ++nc;
        printf("%ld\n", nc);
    }
}

This is the code, and when I typed a character, it will print 1 then print 2, even if I have only typed once.
I am using Xcode.

A:

This is the code, and when I typed a character, it will print 1 then
  print 2, even if I have only typed once.

But you did press return, didn't you ? Try pressing CTRL+D or watch out for the newline in the loop. Maybe something like this ?
int ch;
while ((ch = getchar()) != EOF) {
    if (ch == '\n')
        continue;
    ++nc;
    printf("%ld\n", nc);
}

