C 我得到运行时错误,我不能得到两个字符值a&;B

C 我得到运行时错误,我不能得到两个字符值a&;B,c,C,如果我为变量s输入“hello”,为变量a输入“hello”,我将无法输入 #include<stdio.h> #include<string.h> int main(){ char s[100],a,b; //i am not able to get this value,please help me how to get three variables s,a,b at runtime scanf("%c",s); scanf("%c"

如果我为变量
s
输入“hello”,为变量
a
输入“hello”,我将无法输入

#include<stdio.h>
#include<string.h>
int main(){
    char s[100],a,b;
    //i am not able to get this value,please help me how to get three variables s,a,b at runtime
    scanf("%c",s);
    scanf("%c",a);
    scanf("%c",b);
    int n=strlen(s),count=0;
    for(int i=0;i<n;i++){
        if(s[i]==a && s[i+1]== b)
            count++;
    }
    printf("%d",count);
    return 0;
}
#包括
#包括
int main(){
chars[100],a,b;
//我无法获取此值,请帮助我如何在运行时获取三个变量s、a、b
scanf(“%c”,s);
scanf(“%c”,a);
scanf(“%c”,b);
int n=strlen,count=0;

对于(int i=0;i首先尝试使用
scanf(“%c”和&a)
。 然后仅使用一个
scanf
读取三个变量。 尝试此程序,它将解决您的问题:

#include <stdio.h>
#include <string.h>
int main()
{
    char s[100], a, b;
    //i am not able to get this value,please help me how to get three variables s,a,b at runtime
    scanf("%s %c %c", s, &a, &b);
    int n = strlen(s), count = 0;
    for(int i = 0; i < (n - 1); i++){
        if(s[i] == a && s[i+1] == b)
            count++;
    }
    printf("%d",count);
    return 0;
}
#包括
#包括
int main()
{
chars[100],a,b;
//我无法获取此值,请帮助我如何在运行时获取三个变量s、a、b
scanf(“%s%c%c”,s,&a,&b);
int n=strlen,count=0;
对于(int i=0;i<(n-1);i++){
如果(s[i]==a和&s[i+1]==b)
计数++;
}
printf(“%d”,计数);
返回0;
}

扫描字符时,在字符修饰符之前使用空格,并将字符作为指针传递,而不是作为值传递。要扫描整个字符串,请使用修改后的
%s
。在这种情况下,您不需要编写
&s
,因为
s
本身已经包含数组的内存地址。如果您仍然要使用
&
前面,请不要使用当使用
&s[0]
时,因为
&s[0]==s

char s[100], a, b;
scanf("%s", s);
scanf(" %c", &a);
scanf(" %c", &b);

%c
需要一个地址。您可以给它传递一个字符。使用:
scanf(%c,&a);
。请注意,
s
可以作为数组衰减到指向其第一个成员的指针。若要读取字符串,请使用:
scanf(%s,&s)
您能告诉我们这个程序到底应该做什么吗?请告诉我们
%c
格式和换行符。输入格式:变量s的字符串值,第二行获取变量“a”的字符,第三行获取变量“b”的另一个字符,但我无法以正确的方式输入