为什么我在c ++中获得不同的输出?

-1 c++ compilation

int main(){

    int x;
    cout<<"enter a number: ";
    cin>>x;
    cout<<endl;
    odd(x);


    return 0;
}

void odd(int a){

if(a%2 != 0){

    cout<<"the number is odd : "<< '(' +a+ ')';

    }else{

    even(a);

    }
}
Run Code Online (Sandbox Code Playgroud)

我执行了上面的程序,得到了不同的输出:

enter a number: 15

the number is odd : 96
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

谢谢

Mik*_*zyk 12

试试这个: cout<<"the number is odd : "<< '('<< a << ')';

ASCII中的"("和")"具有值40和41.它们被提升为int并添加它们,这就是输出为96(40 + 15 + 41 == 96)的原因.