scanf("here",%d,s)
时间: 2023-07-18 10:25:16 浏览: 48
这段代码是错误的,应该改为 `scanf("%d", &s)`。
`scanf` 函数的第一个参数是格式字符串,用于指定输入数据的类型和格式。其中,`%d` 表示要读入一个十进制整数,而字符串 `"here"` 不是合法的格式字符串,会导致编译错误。
第二个参数 `&s` 表示要将读入的整数存储到变量 `s` 中。需要注意的是,`scanf` 函数中使用变量名时必须加上取地址符 `&`,表示将变量的地址传递给函数,以便函数可以修改变量的值。
因此,正确的代码应该是:
```c
int s;
scanf("%d", &s);
```
相关问题
define B booboo define X 10 main(int) int age; char name; printf("Please enter your first name,"); scanf("%s",name); printf("All right, %c,what's your age?\n", name); scanf("%f",age); Xp=age +X; printf("That's a%s! You must be at least d,\n",B,xp); rerun 0;
I'm sorry, but your code contains several errors.
First, you need to declare the `main` function as `int main(void)` instead of just `main(int)`.
Second, when you are reading in the user's name with `scanf`, you should use `%c` instead of `%s` since you are only reading in one character at a time.
Third, when you are reading in the user's age with `scanf`, you should use `%d` instead of `%f` since age is an integer.
Fourth, you have not properly defined `B` in your code.
Finally, the line `Xp=age +X` should be `int xp = age + X;`.
Here is the corrected code:
```
#include <stdio.h>
#define B "mistake"
#define X 10
int main(void) {
int age;
char name;
printf("Please enter your first name: ");
scanf("%c", &name);
printf("All right, %c, what's your age?\n", name);
scanf("%d", &age);
int xp = age + X;
printf("That's a %s! You must be at least %d.\n", B, xp);
return 0;
}
```
The program includes the following main function: int main() { char str1[20], str2[80]; scanf("%s%s", str1, str2); printf("%d\n", occurrence1(str1, str2)); printf("%d\n", occurrence2(str1, str2)); return 0; } The program also includes two occurrence functions: occurrence1 and occurrence2. int occurrence#(char *sub, char *str)/* # is 1 or 2*/ is the function that finds number of occurrences of sub in str. For example, a)occurrence1("ab","abc") returns 1; b)occurrence1("ab","bac") returns 0; c)occurrence1("aba","ababa") returns 2; Occurrence1 is a recursive function, occurrence2 is non-recursive function.
Based on the given requirements, here's the implementation of the two occurrence functions (`occurrence1` and `occurrence2`) in C:
```c
#include <stdio.h>
#include <string.h>
int occurrence1(char *sub, char *str) {
if (*str == '\0')
return 0;
else if (strncmp(sub, str, strlen(sub)) == 0)
return 1 + occurrence1(sub, str + strlen(sub));
else
return occurrence1(sub, str + 1);
}
int occurrence2(char *sub, char *str) {
int count = 0;
int sub_len = strlen(sub);
int str_len = strlen(str);
for (int i = 0; i <= str_len - sub_len; i++) {
if (strncmp(sub, str + i, sub_len) == 0)
count++;
}
return count;
}
int main() {
char str1[20], str2[80];
scanf("%s%s", str1, str2);
printf("%d\n", occurrence1(str1, str2));
printf("%d\n", occurrence2(str1, str2));
return 0;
}
```
In this program, the `occurrence1` function uses recursion to find the number of occurrences of `sub` in `str`. It checks if the current substring of `str` matches the `sub` string. If there is a match, it increases the count by 1 and recursively calls `occurrence1` on the remaining part of `str`. If there is no match, it recursively calls `occurrence1` on `str` excluding the first character.
The `occurrence2` function is a non-recursive alternative that uses a loop to iterate over `str` and compare substrings of length `sub_len` with `sub`. It increases the count whenever a match is found.
The main function reads two strings from input using `scanf` and then calls `occurrence1` and `occurrence2` functions respectively to find the occurrences of `str1` in `str2`, printing the results using `printf`.
Please note that the program assumes the input strings (`str1` and `str2`) are null-terminated strings.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)