character counting

문자를 입력받았을 경우 개수를 새는 프로그램

방법 1.
#include <stdio.h>

main()
{
    long nc;

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

long 은 int보다 크거나 같다. (32bit 이상)
prinft에는 %ld로 표시해 줘야 한다. (LD)
++nc는 prefix로 먼저 1 증가 시킨다. 반대는 nc++로 postfix다.

방법 2.
#include <stdio.h>

main()
{
    double nc;

    for (nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%.0f\n", nc);
}

for loop에서 아무것도 하지 않는다면 ; 만 넣어주면 된다.
%.0f 는 float와 double을 표현할때 사용할 수 있고, 정수와 소수를 다룰 수 있다. 0f는 소수부분을 표현하지 않는것이다. 따라서 정수만 표현된다.

저작자 표시 비영리 변경 금지
Tag //
|  1  | ...  98  |  99  |  100  |  101  |  102  |  103  |  104  |  105  |  106  | ...  108  |