1、在windows下,写出运行打印结果。 #include <iostream.h> #include <string.h> void func(char str[100]) { } void main(void) { char str[ ]= “Hello”; char *p=str; int n=10; cout<<sizeof(str)<<endl; cout<<sizeof(p)<<endl; cout<<sizeof(n)<<endl; cout<<sizeof(func)<<endl; return; } 2、设变量a是整型,f是字符型,i是双精度型,则表达式10+a+i*f值的数据类型为什么? 3、请写出以下代码的打印结果 #include<stdio.h> void main( void ) { int a[5]={1, 8, 3, 9, 6}; printf("%d",*(a+3)); } 4.下面程序的输出结果是 。 char b[]=“ABCD”; void main() { char *chp; for(chp=b; *chp; chp+=2) printf(“%s”, chp); printf(“\n”); }
时间: 2024-03-17 19:41:58 浏览: 17
1、在Windows下,代码如下:
```
#include <iostream>
#include <string.h>
using namespace std;
void func(char str[100]) {}
int main() {
char str[] = "Hello";
char *p = str;
int n = 10;
cout << sizeof(str) << endl;
cout << sizeof(p) << endl;
cout << sizeof(n) << endl;
cout << sizeof(func) << endl;
return 0;
}
```
运行结果如下:
```
6
4
4
1
```
2、表达式10+a+i*f的值的数据类型为双精度型,因为i是双精度型,f是字符型,字符型会自动转换为整型,整型和双精度型的运算结果是双精度型。
3、代码的打印结果是:
```
9
```
4、程序的输出结果是:
```
AC
```
相关问题
在windows下,写出运行打印结果。 #include <iostream.h> #include <string.h> void func(char str[100]) { } void main(void) { char str[ ]= “Hello”; char *p=str; int n=10; cout<<sizeof(str)<<endl; cout<<sizeof(p)<<endl; cout<<sizeof(n)<<endl; cout<<sizeof(func)<<endl; return; }
在 Windows 系统下运行这段代码将会得到以下的输出结果:
100
4
4
1
让我解释一下代码的执行过程:
首先,我们包含了 `<iostream.h>` 和 `<string.h>` 头文件。
然后,我们定义了一个函数 `func`,接受一个长度为100的字符数组作为参数。
接下来,在 `main` 函数中,我们声明了一个名为 `str` 的字符数组,并初始化为 "Hello"。
然后,我们声明了一个指向字符的指针变量 `p`,并将其指向 `str`。
接着,我们声明了一个整型变量 `n`,并赋值为 10。
接下来,我们使用 `sizeof` 运算符来输出不同变量的大小。
- `sizeof(str)` 输出的是 `str` 数组的大小,即100个字符(每个字符占1字节),所以输出结果为 100。
- `sizeof(p)` 输出的是指针变量 `p` 的大小,在32位系统下,指针占用4个字节,所以输出结果为 4。
- `sizeof(n)` 输出的是整型变量 `n` 的大小,整型变量在32位系统下占用4个字节,所以输出结果为 4。
- `sizeof(func)` 输出的是函数 `func` 的大小,函数不占用任何存储空间,所以输出结果为 1(实际上在某些编译器中可能会输出其他值)。
最后,我们使用 `cout` 对象来输出上述结果。
因此,代码的输出结果为:
100
4
4
1
为什么以下程序会出错?#include <iostream> #include <string> using namespace std; class A { int a; public: virtual void func() = 0; }; class B: public A { public: void func(){ cout<<"Class B"<<endl; } }; int main(int argc, char const *argv[]) { A a; a.func(); return 0; }
这段程序会出错,因为类 A 是一个抽象类,其中包含一个纯虚函数 func(),该函数没有任何实现,因此无法创建 A 的对象。在 main 函数中创建了一个 A 类型的对象 a,然后调用了它的 func() 函数,这会导致编译错误。正确的做法是创建 A 的派生类的对象,然后调用该派生类的 func() 函数。在本例中,可以创建一个 B 类型的对象,并调用它的 func() 函数,如下所示:
```
int main(int argc, char const *argv[]) {
B b;
b.func();
return 0;
}
```
这样就可以输出 "Class B"。