我的cpp程序没有要求输入。 [英] My cpp program is not being asking for input.

查看:102
本文介绍了我的cpp程序没有要求输入。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题,无法理解为什么它不要求输入。该代码的
输出为cin0。
请解释为什么。

i have come across this question and could not get why it is not asking for input. output of the code is cin0. please explain it why so.

#include<iostream>
using namespace std;
int main ()
{
       int cin;
       cin >> cin;
       cout << "cin" << cin;
       return 0;
}


推荐答案

让我们逐行看一下

int cin;

此行声明一个名为 cin 的局部变量。从现在开始,每当您编写 cin 时,编译器始终认为您的意思是此局部变量,而不是输入流对象 std :: cin

This line declare a local variable named cin. From now on, whenever you write cin, the compiler always believe you mean this local variable, not the input stream object std::cin.

cin >> cin;

此行读取局部变量并执行位移。当>> 运算符的两端都是整数时,它不再表示输入;这意味着现在移位。

This line read the local variable and perform bit shifting. When both sides of >> operator are integers, it no longer means input; it means bit shifting now.

但这不是重点。

重点是局部变量 cin 未初始化并被读取。行为未定义。程序中的一种未定义行为会使整个程序的行为未定义。

The point is, local variable cin is uninitialized and it is read. The behaviour is undefined. One undefined behaviour in the program can make the behaviour of the entire program undefined.

还请注意,如果我们忽略以下问题:

Also note that, if we ignore the problem of undefined behaviour, the result of the bit shifting is not assigned to anything and therefore lost.

cout << "cin" << cin;

再次读取局部变量 cin 初始化。这是另一行未定义的行为。

Local variable cin is once again read without initialization. This is another line of undefined behaviour.

由于未定义的行为,说出为什么输出 cin0不再有意义。

Because of undefined behaviour, it is no longer meaningful to say why it output cin0. But we can reasonably imagine the memory of local variable cin happens to contain zero.

这篇关于我的cpp程序没有要求输入。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆