变量与数据类型
1 分析错误
以下代码有语法错误吗? 如有错误,请指出其中的错误。
cpp
#include <iostream>
using namespace std;
int main() {
cout << a;
int a = 100;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
int b;
cout << b;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
int c = 10;
double c = 3.14;
cout << c;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
int d = "123";
cout << d;
return 0;
}答案(同学们也可以编译运行,进行验证)
2 分析运行结果
观察以下代码,它的运行结果会是怎么样呢?
cpp
#include <iostream>
using namespace std;
int main() {
int a = 10.5;
cout << a ;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
int a = 5;
{
int a = 10;
cout << a;
}
cout << a;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
bool b1 = true;
bool b2 = false;
cout << b1 << " " << b2;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
double a = 100, b = 200 , c = a + b;
cout << c;
return 0;
}输出示例
3 分析错误
以下代码有语法错误吗? 如有错误,请指出其中的错误。
cpp
#include <iostream>
using namespace std;
int main() {
const int x;
x = 100;
cout << x;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
auto y = 3.14;
y = "hello";
cout << y;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {{{{{{{{{
int a = 100;
cout << a;
}}}}}}}}}cpp
#include <iostream>
using namespace std;
int main() {
{
int a = 100;
int b = 200;
}
{
int a = 100;
int b = 200;
}
cout << a + b;
}答案(同学们也可以编译运行,进行验证)
4 分析运行结果
观察以下代码,它的运行结果会是怎么样呢?
cpp
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
{
int a = 30;
cout << a+b << endl;
{
int b = 100;
cout << a+b << endl;
}
}
cout << a+b << endl;
return 0;
}cpp
#include <iostream>
using namespace std;
int a = 100, b = 100;
int main() {
int a = 200;
cout << a << b;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
int a = 2147483647;
int b = 2147483648;
int c = 2147483649;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}cpp
#include <iostream>
#include <climits>
using namespace std;
int main() {
cout << INT_MAX << endl;
cout << INT_MIN << endl;
return 0;
}输出示例
5 分析错误
以下代码有语法错误吗? 如有错误,请指出其中的错误。
cpp
#include <iostream>
using namespace std;
int main() {
char __c__ = 'c';
int hello1= 200;
short xxx = 66;
string ____________str1 = "hello";
double dfajhgkljhaglkhbisihbvhsbvsgfb = 123.213;
float vjhjg42334gjhg234gjkh__n234SJDKFG = 234.435;
cout << dfajhgkljhaglkhbisihbvhsbvsgfb << endl;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
bool www = false;
int 2H = 100;
long long a = 12321313123LL;
int #@abc = 435;
int a+b = 23;
return 0;
}cpp
#include <iostream>
using namespace std;
int main() {
int cout = 100;
int for = 200;
int if = 300;
cout << cout << endl;
return 0;
}cpp
#include <iostream>
int main() {
int cout = 100;
std::cout << cout << std::endl;
return 0;
}cpp
#include <iostream>
int main() {
const int var = 100;
var = 200;
cout << var << endl;
return 0;
}