- 1.1
头文件名可以为
.h
.hpp
等
源文件可以为.cpp
.cc
等
#include <iostream>
int main()
{
return 0;
}
- 1.2
#include <iostream>
int main()
{
return -1;
}
编译结果...
--------
- 错误: 0
- 警告: 0
- 输出文件名: E:\C++\一\1.2.exe
- 输出大小: 1.81951808929443 MiB
- 编译时间: 1.05s
运行结果...
--------------------------------
Process exited after 0.04694 seconds with return value 4294967295
请按任意键继续. . .
- 1.3
#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}
hello world
- 1.4
#include <iostream>
int main()
{
int num1 = 3;
int num2 = 5;
std::cout << "num1+num2=" << num1+num2 << std::endl;
std::cout << "num1*num2=" << num1*num2 << std::endl;
return 0;
}
num1+num2=8
num1*num2=15
- 1.5
#include <iostream>
int main()
{
std::cout << "Enter tow nubers" << std::endl;
int num1, num2;
std::cin >> num1;
std::cin >> num2;
std::cout << "num1+num2=";
std::cout << num1+num2;
std::cout << std::endl;
return 0;
}
Enter tow nubers
1 2
num1+num2=3
- 1.6
这段程序代码不合法。
第1,2行末尾有分号,表示这段代码包含三条语句。<<为二元运算符,第二三条语句缺少左操作数。因此不合法。需要将2,3行开头加上std::cout
即可。
- 1.7
#include <iostream>
int main()
{
/*
/*this is a wrong annotations*/
*/
return 0;
}
In function 'int main()':
6 3 [Error] expected primary-expression before '/' token
7 2 [Error] expected primary-expression before 'return'
7 2 [Error] expected ';' before 'return'
- 1.8
#include <iostream>
int main()
{
std::cout << "/*";
std::cout << "*/";
//wrong std::cout << /* "*/" */;
std::cout << /* "*/" /* " /*" */;
return 0;
}
- 1.9
#include <iostream>
int main()
{
int sum = 0, num = 50;
while (num <= 100)
{
sum += num;
num++;
}
std::cout << "the sum of 50 to 100 is " << sum << std::endl;
return 0;
}
the sum of 50 to 100 is 3825
- 1.10
#include <iostream>
int main()
{
int num = 10;
while (num >= 0)
{
std::cout << num << " ";
num --;
}
return 0;
}
10 9 8 7 6 5 4 3 2 1 0