1、编写一个程序,使用 sizeof 运算符打印基本类型、一些指针类型和你选择的一些枚举类型的大小。
以下是一个满足需求的 C++ 程序示例:
#include <iostream>
// 定义一个枚举类型
enum Color { RED, GREEN, BLUE };
int main() {
// 打印基本类型的大小
std::cout << "Size of char: " << sizeof(char) << " bytes" << std::endl;
std::cout << "Size of short: " << sizeof(short) << " bytes" << std::endl;
std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
std::cout << "Size of long: " << sizeof(long) << " bytes" << std::endl;
std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
std::cout << "Size of long double: " << sizeof(long double) << " bytes" << std::endl;
std::cout << "Size of bool: " << sizeof(bool) << " bytes" << std::endl;
// 打印指针类型的大小
std::cout << "Size of int*: " << sizeof(int*) << " bytes" << std::endl;
std::cout << "Size of char*: " << sizeof(char*) << " bytes" << std::endl;
std::cout << "Size of double*: " << sizeof(double*) << " bytes" << std::endl;
// 打印枚举类型的大小
std::cout << "Size of Color: " << sizeof(Color) << " bytes" << std::endl;
return 0;
}
该程序使用 sizeof 运算符打印了常见基本类型、指针类型和自定义枚举类型的大小。
2、typedef int (&rifi) (int, int); 是什么意思?有什么用?
此语句将 rifi 定义为一个引用类型,该引用指向一个接受两个 int 类型参数并返回 int 类型值的函数。它的用途在于简化代码、提高可读性,可用于传递函数引用,避免直接使用复杂的函数类型声明。
3、编写一个类似“Hello, world!”的程序,它将一个名字作为命令行参数,并输出“Hello, name!”。修改这个程序,使其可以接受任意数量的名字作为参数,并向每个名字打招呼。
以下是实现该功能的 C++ 代码:
#include <iostream>
int main(int argc, char* argv[]) {
if (argc == 1) {
std::cout << "请提供至少一个名字作为命令行参数。" << std::endl;
return 1;
}
for (int i = 1; i < argc; ++i) {
std::cout << "Hello, " << argv[i] << "!" << std::endl;
}
return 0;
}
代码解释:
- 检查参数数量 :
argc表示命令行参数的数量,argc == 1意味着没有提供额外的参数(除了程序名本身),此时程序会输出提示信息并返回错误码 1。 - 遍历参数 :使用
for循环从

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



