首页 > 解决方案 > why doesn't this program in C language print anything?

问题描述

why doesn't this program print anything?

int main(){
    int a = getchar()-'0';
    getchar();
    int b = getchar()-'0';
    int vsota = 0;

    vs = (a+b)%10;
    putchar(vs);
    printf("\n");
}

I put in the numbers 7 and 9 and it was supossed to outprint 6 but it does not.

标签: cchar

解决方案


putchar(vs); writes the character whose code is the value in vs. The value in vs is 6. So it writes the character with code 6. That character is not the digit “6”. You do not see anything because it is a “control character” with no visible appearance. To write the character for the digit whose value is in vs, use putchar('0' + vs);.

Also, fix this:

    int vsota = 0;

    vs = (a+b)%10;

That would not have compiled, so presumably you made a mistake when entering code into Stack Overflow. Use the same name in both places.


推荐阅读