改正#include"stdio.h" #include <cstring> int main() { char s1[80],s2[80]; gets (s1); gets (s2); printf ("lengthsf:%d %d \n",strlen(s1),strlen(s2)); if (!strcmp(s1,s2)); printf ("the strings areequal \n"); stract(s1,s2); printf("%s\n",s1); }
时间: 2024-03-03 16:47:55 浏览: 23
改正:
```c++
#include <cstdio>
#include <cstring>
int main() {
char s1[80], s2[80];
fgets(s1, 80, stdin);
fgets(s2, 80, stdin);
printf("lengths: %ld %ld\n", strlen(s1), strlen(s2));
if (!strcmp(s1, s2))
printf("the strings are equal\n");
strcat(s1, s2);
printf("%s\n", s1);
return 0;
}
```
改正说明:
1. 将头文件 `stdio.h` 改为 `<cstdio>`,头文件 `cstring` 应该已经包含在 `<string>` 中了,所以可以省略。
2. 使用 `fgets` 代替 `gets`,因为 `gets` 存在缓冲区溢出的风险。
3. 在输出字符串长度时,使用 `%ld` 代替 `%d`,因为 `strlen` 函数返回的是 `size_t` 类型,而不是 `int` 类型。
4. 将分号从 `if` 语句的末尾移除,否则 `if` 语句后面的代码总会被执行。
5. 将函数名 `stract` 改为 `strcat`。
6. 在程序末尾加上 `return 0;`,表示程序正常退出。
相关问题
#include <stdio.h> #include <math.h> int main() { int a
#include <stdio.h> 是一个C语言的头文件,它包含了一些输入输出函数的声明,比如printf和scanf等。而#include <math.h> 是一个数学库的头文件,它包含了一些数学函数的声明,比如sqrt和sin等。
在你给出的代码中,int main() 是程序的入口函数,它表示程序从这里开始执行。在main函数中,定义了一个整型变量a,但是没有给它赋初值。
如果你想要使用printf函数来输出一些信息,可以在main函数中添加代码,比如:
int main()
{
int a = 10;
printf("a的值是:%d\n", a);
return 0;
}
这样就可以输出a的值为10。你可以根据需要修改a的值和输出的格式。
#include <STC8H.h>#include <intrins.h> #include <math.h>#include <stdio.h>typedef unsigned char ucha
这段代码是一个C语言程序的头文件引用部分。具体解释如下:
1. `#include <STC8H.h>`:这是引用了一个名为STC8H的头文件,该头文件可能包含了一些与STC8H芯片相关的定义和函数声明。
2. `#include <intrins.h>`:这是引用了一个名为intrins.h的头文件,该头文件可能包含了一些与嵌入式系统中的特殊指令和操作相关的定义和函数声明。
3. `#include <math.h>`:这是引用了一个名为math.h的头文件,该头文件包含了一些与数学运算相关的函数声明和宏定义,例如数学函数sin、cos等。
4. `#include <stdio.h>`:这是引用了一个名为stdio.h的头文件,该头文件包含了一些与输入输出操作相关的函数声明和宏定义,例如printf、scanf等。
5. `typedef unsigned char ucha`:这是一个类型定义语句,将unsigned char类型重命名为ucha,方便在代码中使用ucha作为变量类型。