Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的字符计数器_C_Syntax - Fatal编程技术网

C语言中的字符计数器

C语言中的字符计数器,c,syntax,C,Syntax,这段用C语言编写的代码应该计算表中等于A的所有字符。但是它不起作用,我需要帮助才能找到问题~~ 我首先读取用户使用for循环给出的表中的字符,然后使用另一个for循环来计算等于a的字符数~~ 有什么建议吗 #include <stdio.h> #include <stdlib.h> int main() { char T[100],A; int i,N,b=0; printf("give the number of your

这段用C语言编写的代码应该计算表中等于A的所有字符。但是它不起作用,我需要帮助才能找到问题~~ 我首先读取用户使用for循环给出的表中的字符,然后使用另一个for循环来计算等于a的字符数~~

有什么建议吗

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char T[100],A;
    int i,N,b=0;
    
    printf("give the number of your table's columns \n");
    scanf("%d", &N);

    if (N > 0 && N <= 100) {
        for (i; i < N; i++) {
            scanf("%c", &T[i]);

            printf("give the character of the column number %d \n", i);
            scanf("%c",&T[i]);
        }
     
        for (i; i < N; i++) {
            if (T[i] = 'A') b++; 

            printf("the number of characters equal to A is %d",b);
        }
        return 0;
    }
}
#包括
#包括
int main()
{
chart[100],A;
int i,N,b=0;
printf(“给出表的列数\n”);
scanf(“%d”和“&N”);

如果(N>0&&N你必须明确解释为什么它不工作:什么错误消息,你如何编译,你需要数字100等等


在MikeCAT注释的顶部,您必须查看(N>0&&N您是否必须明确解释它为什么不工作:什么错误消息、如何编译、是否需要数字100等

在MikeCAT注释的顶部,您必须查看(N>0&&N) “本应计算表中等于A的所有字符。但这不起作用。我需要帮助才能找到问题!!”

编译器警告将帮助您找到问题。
如果编译时未看到警告,请将编译器设置为显示警告。(例如,使用
-Wall

在启用警告的情况下进行编译时,您应该在编译时看到类似的信息:

Build Status (so.prj - Debug)
 s0_15.c - 3 warnings
  22, 22    warning: using the result of an assignment as a condition without parentheses 
  22, 22    note: place parentheses around the assignment to silence this warning
  22, 22    note: use '==' to turn this assignment into an equality comparison
  7, 17     warning: unused variable 'A' 
  14, 14    warning: variable 'i' is uninitialized when used here 
  8, 10     note: initialize the variable 'i' to silence this warning
此外,当下拉到第二个
for()
循环中时,
i
再次未初始化,而是保留完成第一个循环时的值。需要将其重新初始化为零,第二个循环才能工作。。。(注意,我的编译器没有标记这一点,幸运的是有人告诉我在这里标记了这一点,允许我在运行时发现
I
的值

通过遵循上述警告提供的指导,进行了修改,以允许代码运行,并执行您描述的操作

有关对上述警告的响应,请参见在线评论:

int main(void)//added void to match accepted prototype of main
{
    char T[100] = {0},A;//A is not used, should be deleted
                        //initialize array to all zeros.
    int i=0,N=0,b=0;//initialized i (and N)
    
    printf("give the number of your table's columns \n");
    scanf("%d", &N);

    if (N > 0 && N <= 100) {
        for (i; i < N; i++) {
            scanf("%c", &T[i]);

            printf("give the character of the column number %d \n", i);
            scanf("%c",&T[i]);
        }
        // `i` is already equal to N here.  it will never enter the loop
        //for (i; i < N; i++) {         
        for (i = 0; i < N; i++) {//initialized i to zero
          //if (T[i] = 'A') b++;//this is not what you want
            if (T[i] == 'A') b++; //replace assignment '=' 
                                  //with    comparison '==' operator
            //Note, optionally move the following printf() statement to below 
            //the loop so it is called only once after counting is done.
            printf("the number of characters equal to A is %d\n",b);//added '\n' for readability of output.
        }
        //return 0;// not here...
    }
    return 0;// ...but here
}
int main(void)//添加了void以匹配main的可接受原型
{
char T[100]={0},A;//A未使用,应删除
//将数组初始化为全零。
int i=0,N=0,b=0;//初始化i(和N)
printf(“给出表的列数\n”);
scanf(“%d”和“&N”);
如果(N>0&&N)
“本应计算表中等于A的所有字符。但这不起作用。我需要帮助才能找到问题!!”

编译器警告将帮助您找到问题。
如果编译时未看到警告,请将编译器设置为显示警告。(例如,使用
-Wall

在启用警告的情况下进行编译时,您应该在编译时看到类似的信息:

Build Status (so.prj - Debug)
 s0_15.c - 3 warnings
  22, 22    warning: using the result of an assignment as a condition without parentheses 
  22, 22    note: place parentheses around the assignment to silence this warning
  22, 22    note: use '==' to turn this assignment into an equality comparison
  7, 17     warning: unused variable 'A' 
  14, 14    warning: variable 'i' is uninitialized when used here 
  8, 10     note: initialize the variable 'i' to silence this warning
此外,当下拉到第二个
for()
循环中时,
i
再次未初始化,而是保留完成第一个循环时的值。需要将其重新初始化为零,第二个循环才能工作。。。(注意,我的编译器没有标记这一点,幸运的是有人告诉我在这里标记了这一点,允许我在运行时发现
I
的值

通过遵循上述警告提供的指导,进行了修改,以允许代码运行,并执行您描述的操作

有关对上述警告的响应,请参见在线评论:

int main(void)//added void to match accepted prototype of main
{
    char T[100] = {0},A;//A is not used, should be deleted
                        //initialize array to all zeros.
    int i=0,N=0,b=0;//initialized i (and N)
    
    printf("give the number of your table's columns \n");
    scanf("%d", &N);

    if (N > 0 && N <= 100) {
        for (i; i < N; i++) {
            scanf("%c", &T[i]);

            printf("give the character of the column number %d \n", i);
            scanf("%c",&T[i]);
        }
        // `i` is already equal to N here.  it will never enter the loop
        //for (i; i < N; i++) {         
        for (i = 0; i < N; i++) {//initialized i to zero
          //if (T[i] = 'A') b++;//this is not what you want
            if (T[i] == 'A') b++; //replace assignment '=' 
                                  //with    comparison '==' operator
            //Note, optionally move the following printf() statement to below 
            //the loop so it is called only once after counting is done.
            printf("the number of characters equal to A is %d\n",b);//added '\n' for readability of output.
        }
        //return 0;// not here...
    }
    return 0;// ...but here
}
int main(void)//添加了void以匹配main的可接受原型
{
char T[100]={0},A;//A未使用,应删除
//将数组初始化为全零。
int i=0,N=0,b=0;//初始化i(和N)
printf(“给出表的列数\n”);
scanf(“%d”和“&N”);
如果(N>0&&NC具有语言功能(或缺陷),则可以在通常不需要赋值运算符的上下文中使用赋值运算符

 if (T[i] = 'A') b++;
 
正在更改
T[i]
而未将其与
A
进行比较。这意味着如果更改的值不是
0
,则
if
语句正在注册

使用
=
,您将再次进行比较。

C具有语言功能(或缺陷)。您可以在通常不需要赋值运算符的上下文中使用赋值运算符

 if (T[i] = 'A') b++;
 
正在更改
T[i]
而未将其与
A
进行比较。这意味着如果更改的值不是
0
,则
if
语句正在注册


使用
=
,您将再次进行比较。

1.正确设置代码格式。添加缩进并删除多余的空行。2.在使用前初始化
i
。3.
如果(T[i]='A')b++;
在这里做作业看起来很奇怪。你必须更好地说明为什么它不起作用:什么错误消息等?在MikeCAT注释的顶部,你必须查看
if(N>0&&N@AntoninGAVREL
if)(N>0&&N另一个问题是,如果您的输入由多行组成,则您的程序可能无法按预期运行。@MikeCAT indeed1.正确设置代码格式。添加缩进并删除额外的空行。2.在使用前初始化
i
。3.
if(T[i]=“A”)b++;
在这里做作业看起来很奇怪。你必须更好地说明为什么它不起作用:什么错误消息等?在MikeCAT注释的顶部,你必须查看
if(N>0&&N@AntoninGAVREL
if)(N>0&&N另一个问题是,如果您的输入由多行组成,则您的程序可能无法按预期运行。@MikeCAT的确……在gcc中使用
-Wall
,这将是一个非常好的主意,因为它实际上会对此发出警告。@ilkkachu是一个非常好的建议;Ryker已经介绍了它……这将是一个非常好的建议将
-Wall
与gcc一起使用的想法,因为它实际上警告了这一点。@ilkkachu是一个很好的建议;而且,Ryker已经涵盖了它。如果我不会写两次scanf(“%c”、&t[I]),我应该用什么来替换它?因为当写一次它时,它会做另一件不需要的事情,实际上我不明白你为什么这么做