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

Why do I get different output in c++?

本文关键字:输出 c++ 为什么      更新时间:2023-10-16
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);
    }
}

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

enter a number: 15
the number is odd : 96

为什么会这样?

谢谢

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

ASCII 中的"(" 和"(" 的值为 40 和 41。它们被提升为 int 并添加它们,这就是为什么您的输出为 96 (40 + 15 + 41 == 96(。